锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
2 #Runtime: 67 ms (Beats 16.67%)
3 #Memory: 13.6 MB (Beats 83.33%)
4
5 class Solution(object):
6 def makeEqual(self, words):
7 """
8 :type words: List[str]
9 :rtype: bool
10 """
11 total = ''.join(words)
12 chars = set(total)
13 for ch in chars:
14 if total.count(ch) % len(words) != 0:
15 return False
16 return True
]]>
#Runtime: 25 ms (Beats 79.41%)
#Memory: 13.4 MB (Beats 88.24)
class Solution(object):
def minOperations(self, s):
"""
:type s: str
:rtype: int
"""
ans1, ans2 = 0, 0
for i in xrange(0, len(s)):
if i % 2:
if s[i] == '0':
ans1 += 1
else:
ans2 += 1
else:
if s[i] == '1':
ans1 += 1
else:
ans2 += 1
return min(ans1, ans2)
]]>
2 #Runtime: 16 ms (Beats 59.29%)
3 #Memory: 13.5 MB (Beats 76.11%)
4
5 class Solution(object):
6 def isPathCrossing(self, path):
7 """
8 :type path: str
9 :rtype: bool
10 """
11 x, y = 0, 0
12 cor = {(x, y)}
13 for ch in path:
14 if ch == 'N':
15 y += 1
16 elif ch == 'S':
17 y -= 1
18 elif ch == 'E':
19 x += 1
20 else:
21 x -= 1
22 if (x, y) in cor:
23 return True
24 cor.add((x, y))
25 return False
]]>
2 #Runtime: 13 ms (Beats 90.28%)
3 #Memory: 13.3 MB (Beats 56.94%)
4
5 class Solution(object):
6 def maxScore(self, s):
7 """
8 :type s: str
9 :rtype: int
10 """
11 ones = s[1:].count('1')
12 if s[0] == '0':
13 zeros = 1
14 else:
15 zeros = 0
16 ans = ones + zeros
17 for i in xrange(1, len(s) - 1):
18 if s[i] == '0':
19 zeros += 1
20 else:
21 ones -= 1
22 ans = max(ans, ones + zeros)
23 return ans
]]>
2 #Runtime: 121 ms (Beats 70.97%)
3 #Memory: 13.6 MB (Beats 35.48%)
4
5 class Solution(object):
6 def numSpecial(self, mat):
7 """
8 :type mat: List[List[int]]
9 :rtype: int
10 """
11 ans = 0
12 for i in xrange(len(mat)):
13 if mat[i].count(1) == 1:
14 x = mat[i].index(1)
15 col = [r[x] for r in mat]
16 if col.count(1) == 1:
17 ans += 1
18 return ans
]]>
2 #Runtime: 27 ms (Beats 98.59%)
3 #Memory: 15.3 MB (Beats 81.69%)
4
5 class Solution(object):
6 def largestOddNumber(self, num):
7 """
8 :type num: str
9 :rtype: str
10 """
11 p = -1
12 for i in xrange(len(num) - 1, -1, -1):
13 if (ord(num[i]) - ord('0')) % 2:
14 p = i
15 break
16 return num[0:p+1]
17
]]>
2 #Runtime: 17 ms (Beats 71.79%)
3 #Memory: 13.4 MB (Beats 46.15%)
4
5 class Solution(object):
6 def largestGoodInteger(self, num):
7 """
8 :type num: str
9 :rtype: str
10 """
11 x = '/'
12 t = 1
13 for i in xrange(1, len(num)):
14 if num[i] == num[i - 1]:
15 t += 1
16 if t >= 3:
17 x = max(x, num[i])
18 print(x)
19 else:
20 t = 1
21 if x >= '0':
22 return x * 3
23 return ""
]]>
2 #Runtime: 71 ms (Beats 71.98%)
3 #Memory: 14.4 MB (Beats 7.69%)
4
5 class Solution(object):
6 def countCharacters(self, words, chars):
7 """
8 :type words: List[str]
9 :type chars: str
10 :rtype: int
11 """
12 ans = ''
13 for w in words:
14 for ch in w:
15 if chars.count(ch) < w.count(ch):
16 break
17 else:
18 ans += w
19 return len(ans)
]]>
2 #Runtime: 38 ms (Beats 86.93%)
3 #Memory: 13.4 MB (Beats 63.83%)
4
5 class Solution(object):
6 def sortByBits(self, arr):
7 """
8 :type arr: List[int]
9 :rtype: List[int]
10 """
11 arr.sort(key=lambda x: (bin(x).count("1"), x))
12 return arr
]]>
2 #Runtime: 621 ms (Beats 43.23%)
3 #Memory: 33.2 MB (Beats 7.74%)
4
5 class Solution(object):
6 def findArray(self, pref):
7 """
8 :type pref: List[int]
9 :rtype: List[int]
10 """
11 ans = [pref[0]]
12 for i in xrange(1, len(pref)):
13 ans.append(pref[i] ^ pref[i - 1])
14 return ans
]]>
鍐欐硶涓
2 #Runtime: 16 ms (Beats 49.50%)
3 #Memory: 13.4 MB (Beats 12.41%)
4
5 class Solution(object):
6 def isPowerOfFour(self, n):
7 """
8 :type n: int
9 :rtype: bool
10 """
11 return n > 0 and math.ceil(log10(n) / log10(4)) == int(log10(n) / log10(4))
鍐欐硶浜?br />
2 #Runtime: 14 ms (Beats 68.30%)
3 #Memory: 13.1 MB (Beats 88.85%)
4
5 class Solution(object):
6 def isPowerOfFour(self, n):
7 """
8 :type n: int
9 :rtype: bool
10 """
11 if n <= 0:
12 return False
13 while n > 1:
14 if n % 4 != 0:
15 return False
16 n = n // 4
17 return True
]]>
2 #Runtime: 21 ms (Beats 23.47%)
3 #Memory: 13.3 MB (Beats 40.52%)
4
5 class Solution(object):
6 def numIdenticalPairs(self, nums):
7 """
8 :type nums: List[int]
9 :rtype: int
10 """
11 return sum(i * (i - 1) // 2 for i in Counter(nums).values())
]]>
2 #Runtime: 777 ms (Beats 50.28%)
3 #Memory: 23.3 MB (Beats 100%)
4
5 class Solution(object):
6 def isMonotonic(self, nums):
7 """
8 :type nums: List[int]
9 :rtype: bool
10 """
11 if len(nums) == 1:
12 return True
13 i = 0
14 while i < len(nums) - 1 and nums[i] == nums[i + 1]:
15 i += 1
16 if i < len(nums) - 1:
17 if nums[i] < nums[i + 1]:
18 while i < len(nums) - 1:
19 if nums[i] > nums[i + 1]:
20 return False
21 i += 1
22 else:
23 while i < len(nums) - 1:
24 if nums[i] < nums[i + 1]:
25 return False
26 i += 1
27 return True
]]>
2 #Runtime: 8 ms (Beats 94.61%)
3 #Memory: 13.3 MB (Beats 96%)
4
5 class Solution(object):
6 def isSubsequence(self, s, t):
7 """
8 :type s: str
9 :type t: str
10 :rtype: bool
11 """
12 p = 0
13 for ch in s:
14 while p < len(t) and t[p] != ch:
15 p += 1
16 if p >= len(t):
17 return False
18 if t[p] == ch:
19 p += 1
20 return True
]]>
python鐗?br />
2 #Runtime: 7 ms (Beats 99.45%)
3 #Memory: 13.3 MB (Beats 21.94%)
4
5 class Solution(object):
6 def generate(self, numRows):
7 """
8 :type numRows: int
9 :rtype: List[List[int]]
10 """
11 ans = [[1]]
12 for i in range(1, numRows):
13 t = [1]
14 for j in range(len(ans[i - 1]) - 1):
15 t.append(ans[i - 1][j] + ans[i - 1][j + 1])
16 t.append(1)
17 ans.append(t)
18 return ans
C++鐗?br />
2 //Runtime: 12 ms (Beats 11.67%)
3
4
5 class Solution {
6 public:
7 vector<vector<int> > generate(int numRows) {
8 int dp[2][10010];
9 vector<vector<int> > res;
10 for(int i = 1; i <= numRows; ++i) {
11 vector<int> tp;
12 for(int j = 1; j <= i; ++j) {
13 if(j == 1 || j == i) dp[i & 1][j] = 1;
14 else
15 dp[i & 1][j] = dp[(i - 1) & 1][j - 1] + dp[(i - 1) & 1][j];
16 tp.push_back(dp[i & 1][j]);
17 }
18 res.push_back(tp);
19 }
20 return res;
21 }
22 };
]]>
鍐欐硶涓錛氱敤list瀛楢~Z鎵懼搴?br />
2 #Runtime: 24 ms (Beats 5.91%)
3 #Memory: 13.4 MB (Beats 24.19%)
4
5 class Solution(object):
6 def convertToTitle(self, n):
7 """
8 :type n: int
9 :rtype: str
10 """
11 s = ""
12 lst = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
13 while n > 0:
14 t = n % 26
15 if t == 0:
16 t = 26
17 s = s + lst[t - 1]
18 n = (n - 1) // 26
19 #print(n)
20 return s[::-1]
鍐欐硶浜岋細鐢╬ython鑷甫鍑芥暟chr
2 #Runtime: 11 ms (Beats 82.44%)
3 #Memory: 13.2 MB (Beats 84.59%)
4
5 class Solution(object):
6 def convertToTitle(self, columnNumber):
7 """
8 :type columnNumber: int
9 :rtype: str
10 """
11 s = ""
12 while columnNumber > 0:
13 t = (columnNumber - 1) % 26
14 s = s + chr(t + 65)
15 columnNumber = (columnNumber - 1) // 26
16 return s[::-1]
]]>
2 #Runtime: 26 ms (Beats 22.90%)
3 #Memory: 13.3 MB (Beats 78.32%)
4
5 class Solution(object):
6 def largestAltitude(self, gain):
7 """
8 :type gain: List[int]
9 :rtype: int
10 """
11 ans = 0
12 for i in range(0, len(gain)):
13 if i > 0:
14 gain[i] = gain[i - 1] + gain[i]
15 ans = max(ans, gain[i])
16 return ans
]]>
2 #Runtime: 667 ms (Beats 25%)
3 #Memory: 18 MB (Beats 22.98%)
4
5 class Solution(object):
6 def equalPairs(self, grid):
7 """
8 :type grid: List[List[int]]
9 :rtype: int
10 """
11 l = len(grid)
12 ans = 0
13 grid_trans = [[0] * l for _ in range(l)]
14 for i in range(l):
15 for j in range(l):
16 grid_trans[i][j] = grid[j][i]
17 for i in grid:
18 for j in grid_trans:
19 if i == j:
20 ans += 1
21 return ans
]]>
鎬濊礬涓錛氳涓父鏍囷紝璁板綍褰撳墠琛岄潪璐熸暟鏈夊嚑涓?br />
2 #Runtime: 95 ms (Beats 68.82%)
3 #Memory: 14.5 MB (Beats 73.12%)
4
5 class Solution(object):
6 def countNegatives(self, grid):
7 """
8 :type grid: List[List[int]]
9 :rtype: int
10 """
11 pos = len(grid[0])
12 ans = 0
13 for i in range(len(grid)):
14 for j in range(pos):
15 if grid[i][j] < 0:
16 pos = j
17 break
18 ans += len(grid[0]) - pos
19 return ans
鎬濊礬浜岋細姣忚浜屽垎姹備綅緗紙澶嶆潅搴︽洿浣庯紝浣嗚繖棰樻暟鎹及璁″緢姘達紝鎵浠ュ弽鑰屾參錛?br />
2 #Runtime: 102 ms (Beats 25.81%)
3 #Memory: 14.5 MB (Beats 40.86%)
4
5 class Solution(object):
6 def countNegatives(self, grid):
7 """
8 :type grid: List[List[int]]
9 :rtype: int
10 """
11 def b_search(i):
12 l = 0
13 r = len(grid[i])
14 while l < r:
15 mid = (l + r) // 2
16 if grid[i][mid] >= 0:
17 l = mid + 1
18 else:
19 r = mid
20 return l
21
22 ans = 0
23 for i in range(len(grid)):
24 ans += len(grid[0]) - b_search(i)
25 return ans
]]>
2 #Runtime: 23 ms (Beats 81.90%)
3 #Memory: 13.5 MB (Beats 61.90%)
4
5 class Solution(object):
6 def canMakeArithmeticProgression(self, arr):
7 """
8 :type arr: List[int]
9 :rtype: bool
10 """
11 arr.sort()
12 inc = arr[1] - arr[0]
13 for i in range(2, len(arr)):
14 if arr[i] - arr[i - 1] != arr[1] - arr[0]:
15 return False
16 return True
]]>
2 #Runtime: 45 ms (Beats 45.11%)
3 #Memory: 13.9 MB (Beats 29.35%)
4
5 class Solution(object):
6 def checkStraightLine(self, coordinates):
7 """
8 :type coordinates: List[List[int]]
9 :rtype: bool
10 """
11 eps = 1e-6
12 for k in range(2, len(coordinates)):
13 if coordinates[0][0] == coordinates[1][0]:
14 if coordinates[k][0] != coordinates[0][0]:
15 return False
16 else:
17 if abs(coordinates[0][0] - coordinates[k][0]) < eps or abs(1.0 * (coordinates[0][1] - coordinates[1][1]) / (coordinates[0][0] - coordinates[1][0]) - 1.0 * (coordinates[0][1] - coordinates[k][1]) / (coordinates[0][0] - coordinates[k][0])) > eps:
18 return False
19 return True
]]>
2 #Runtime: 115 ms (Beats 52.8%)
3 #Memory: 14.1 MB (Beats 5.83%)
4
5 class ParkingSystem(object):
6
7 def __init__(self, big, medium, small):
8 """
9 :type big: int
10 :type medium: int
11 :type small: int
12 """
13 self.cnt_s = small
14 self.cnt_m = medium
15 self.cnt_b = big
16
17
18 def addCar(self, carType):
19 """
20 :type carType: int
21 :rtype: bool
22 """
23 if carType == 1:
24 if self.cnt_b > 0:
25 self.cnt_b -= 1
26 return True
27 else:
28 return False
29 if carType == 2:
30 if self.cnt_m > 0:
31 self.cnt_m -= 1
32 return True
33 else:
34 return False
35 if carType == 3:
36 if self.cnt_s > 0:
37 self.cnt_s -= 1
38 return True
39 else:
40 return False
41
42
43
44 # Your ParkingSystem object will be instantiated and called as such:
45 # obj = ParkingSystem(big, medium, small)
46 # param_1 = obj.addCar(carType)
]]>
2 #Runtime: 76 ms (Beats 90.9%)
3 #Memory: 16.8 MB (Beats 53.94%)
4
5 class Solution(object):
6 def topKFrequent(self, nums, k):
7 """
8 :type nums: List[int]
9 :type k: int
10 :rtype: List[int]
11 """
12 cnt = defaultdict(lambda:0)
13 for i in nums:
14 cnt[i] += 1
15 a = sorted(cnt.items(), key=lambda item: item[1], reverse=True)
16 ans = []
17 for x in a:
18 ans.append(x[0])
19 return ans[0:k]
]]>
[1,2,3],
[4,5,6],
[7,8,9]]
2 #Runtime: 18 ms (Beats 50.87%)
3 #Memory: 13.3 MB (Beats 90.64%)
4
5 class Solution(object):
6 def spiralOrder(self, matrix):
7 """
8 :type matrix: List[List[int]]
9 :rtype: List[int]
10 """
11 fg = [[0] * len(matrix[0]) for _ in range(len(matrix))]
12 ans = [matrix[0][0]]
13 fg[0][0] = 1
14 i, j, cnt, t = 0, 0, 1, 0
15 while cnt < len(matrix) * len(matrix[0]):
16 if t % 4 == 0:
17 while j < len(matrix[0]) - 1 and not fg[i][j + 1]:
18 j += 1
19 ans.append(matrix[i][j])
20 fg[i][j] = 1
21 cnt += 1
22 elif t % 4 == 1:
23 while i < len(matrix) - 1 and not fg[i + 1][j]:
24 i += 1
25 ans.append(matrix[i][j])
26 fg[i][j] = 1
27 cnt += 1
28 elif t % 4 == 2:
29 while j >= 0 and not fg[i][j - 1]:
30 j -= 1
31 ans.append(matrix[i][j])
32 fg[i][j] = 1
33 cnt += 1
34 elif t % 4 == 3:
35 while i >= 0 and not fg[i - 1][j]:
36 i -= 1
37 ans.append(matrix[i][j])
38 fg[i][j] = 1
39 cnt += 1
40 t += 1
41 return ans
]]>
2 #Runtime: 93 ms (Beats 7.69%)
3 #Memory: 13.7 MB (Beats 15.92%)
4
5 class Solution(object):
6 def diagonalSum(self, mat):
7 """
8 :type mat: List[List[int]]
9 :rtype: int
10 """
11 ans = 0
12 for i in range(min(len(mat), len(mat[0]))):
13 ans += mat[i][i]
14 for i in range(min(len(mat), len(mat[0]))):
15 j = len(mat[0]) - i - 1
16 if i != j:
17 ans += mat[i][j]
18 return ans
]]>
2 #Runtime: 142 ms (Beats 72.81%)
3 #Memory: 13.7 MB (Beats 71.49%)
4
5 class Solution(object):
6 def findDifference(self, nums1, nums2):
7 """
8 :type nums1: List[int]
9 :type nums2: List[int]
10 :rtype: List[List[int]]
11 """
12 s1 = set(nums1)
13 s2 = set(nums2)
14 return [list(s1 - s2), list(s2 - s1)]
]]>
2 #Runtime: 49 ms (Beats 14.51%)
3 #Memory: 13.4 MB (Beats 85.49%)
4
5 class Solution(object):
6 def arraySign(self, nums):
7 """
8 :type nums: List[int]
9 :rtype: int
10 """
11 t = 0
12 for i in nums:
13 if not i:
14 return 0
15 if i < 0:
16 t += 1
17 return -1 if t % 2 else 1
]]>
2 #Runtime: 21 ms (Beats 35.34%)
3 #Memory: 13.4 MB (Beats 73.89%)
4
5 class Solution(object):
6 def average(self, salary):
7 """
8 :type salary: List[int]
9 :rtype: float
10 """
11 ma = 0
12 mi = 1000001
13 s = 0
14 for i in salary:
15 if i < mi:
16 mi = i
17 if i > ma:
18 ma = i
19 s += i
20 return (s - mi - ma) / (len(salary) - 2.0)
]]>
2 #Runtime: 23 ms (Beats 46.91%)
3 #Memory: 13.3 MB (Beat 88.20%)
4
5 class Solution(object):
6 def addDigits(self, num):
7 """
8 :type num: int
9 :rtype: int
10 """
11 while num >= 10:
12 t = num
13 num = 0
14 while t > 0:
15 num += t % 10
16 t = t // 10
17 return num
]]>
2 #Runtime: 19 ms (Beats 58.68%)
3 #Memory: 13.6 MB (Beats 11.81%)
4
5 class Solution(object):
6 def mergeAlternately(self, word1, word2):
7 """
8 :type word1: str
9 :type word2: str
10 :rtype: str
11 """
12 ans = []
13 for i in range(min(len(word1), len(word2))):
14 ans.append(word1[i])
15 ans.append(word2[i])
16 if i < len(word1):
17 ans.append(word1[i + 1:])
18 if i < len(word2):
19 ans.append(word2[i + 1:])
20 return ''.join(ans)
]]>