锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
2 #Runtime: 911 ms (Beats 96.43%)
3 #Memory: 136.7 MB (Beats 10.71%)
4
5 # Definition for a binary tree node.
6 # class TreeNode(object):
7 # def __init__(self, val=0, left=None, right=None):
8 # self.val = val
9 # self.left = left
10 # self.right = right
11 class Solution(object):
12 def pseudoPalindromicPaths (self, root, cnt = 0):
13 """
14 :type root: TreeNode
15 :rtype: int
16 """
17 if not root:
18 return 0
19 cnt ^= 1 << (root.val - 1)
20 if root.left is None and root.right is None:
21 return 1 if cnt & (cnt - 1) == 0 else 0
22 return self.pseudoPalindromicPaths(root.left, cnt) + self.pseudoPalindromicPaths(root.right, cnt)
23
]]>
#Runtime: 11 ms (Beats 91.4%)
#Memory: 13.5 MB (Beats 47.76%)
class Solution(object):
def findDifferentBinaryString(self, nums):
"""
:type nums: List[str]
:rtype: str
"""
n = len(nums)
num_set = set(int(x, 2) for x in nums)
def change_to_n_bit(x, n):
return ("0"*(n - len(x))) + x
for x in xrange(1<<n):
if x not in num_set:
return change_to_n_bit(bin(x)[2:], n)
]]>
2 #Runtime: 52 ms (Beats 71.57%)
3 #Memory: 17.3 MB (Beats 36.53%)
4
5 class Solution(object):
6 def countBits(self, n):
7 """
8 :type n: int
9 :rtype: List[int]
10 """
11 ans = [1] * (n + 1)
12 ans[0] = 0
13 for i in range(1, n + 1):
14 if (i & (i-1)) == 0:
15 p = i
16 else:
17 ans[i] = 1 + ans[i - p]
18 return ans
]]>
#Runtime: 121 ms (Beats 88.89%)
#Memory: 18.1 MB (Beats 44.44%)
class Solution(object):
def smallestSufficientTeam(self, req_skills, people):
"""
:type req_skills: List[str]
:type people: List[List[str]]
:rtype: List[int]
"""
n_p = len(people)
n_s = len(req_skills)
sk_map = {sk: i for i, sk in enumerate(req_skills)}
dp = [None] * (1 << n_s)
dp[0] = []
sk_p = []
for i in range(n_p):
t = 0
for sk in people[i]:
t |= 1 << sk_map[sk]
sk_p.append(t)
discard_p = [False] * n_p
for i in range(n_p):
for j in range(i + 1, n_p):
if (sk_p[j] | sk_p[i]) == sk_p[i]:
discard_p[j] = True
elif (sk_p[j] | sk_p[i]) == sk_p[j]:
discard_p[i] = True
for i in range(n_p):
if not discard_p[i]:
for j in range(len(dp)):
if dp[j] is None:
continue
t = j | sk_p[i]
if dp[t] is None or len(dp[j]) + 1 < len(dp[t]):
dp[t] = dp[j] + [i]
return dp[(1 << n_s) - 1]
]]>
Python鐗?錛堟敞鎰忓鐞嗚礋鏁扮殑鎯呭喌錛岀湅浜咲iscussion鎵嶆剰璇嗗埌錛?br />
2 #Runtime: 83 ms (Beats 39.2%)
3 #Memory: 14.8 MB (Beats 89.66%)
4
5 class Solution(object):
6 def singleNumber(self, nums):
7 """
8 :type nums: List[int]
9 :rtype: int
10 """
11 ans = 0
12 for i in range(32):
13 cnt = 0
14 for m in nums:
15 t = (m >> i) & 1
16 cnt += t
17 cnt %= 3
18 if cnt and i == 31:
19 ans -= 1 << 31
20 else:
21 ans |= cnt << i
22 return ans
]]>
2 #Runtime: 305 ms (Beats 78.95%)
3 #Memory: 19.6 MB (Beats 36.84%)
4
5 class Solution(object):
6 def shortestPathAllKeys(self, grid):
7 """
8 :type grid: List[str]
9 :rtype: int
10 """
11 nkey = 0
12 n, m = len(grid), len(grid[0])
13 d = [(0, 1), (1, 0), (0, -1), (-1, 0)]
14 for i in range(n):
15 for j in range(m):
16 ch = grid[i][j]
17 if ch == '@':
18 sx, sy = i, j
19 if 'a' <= ch <= 'f':
20 nkey += 1
21 q = deque([(0, sx, sy)])
22 vis = defaultdict(bool)
23 vis[(0, sx, sy)] = True
24 stp = 0
25 while q:
26 sz = len(q)
27 while sz:
28 sz -= 1
29 ky, x, y = q.popleft()
30 if ky == (1 << nkey) - 1:
31 return stp
32 for i in d:
33 tx = x + i[0]
34 ty = y + i[1]
35 if 0 <= tx < n and 0 <= ty < m:
36 tp_ky = ky
37 ch = grid[tx][ty]
38 if ch == '#':
39 continue
40 if 'a' <= ch <= 'f':
41 tp_ky |= 1 << (ord(ch) - ord('a'))
42 if 'A' <= ch <= 'F' and not (ky & (1 << (ord(ch) - ord('A')))):
43 continue
44 if not vis[(tp_ky, tx, ty)]:
45 vis[(tp_ky, tx, ty)] = True
46 q.append((tp_ky, tx, ty))
47 stp += 1
48 return -1
]]>
2 #Runtime: 27 ms (Beats 6.6%)
3 #Memory: 13.3 MB (Beats 78.79%)
4
5 class Solution(object):
6 def minFlips(self, a, b, c):
7 """
8 :type a: int
9 :type b: int
10 :type c: int
11 :rtype: int
12 """
13 ans = 0
14 for i in range(31):
15 if (c >> i) & 1:
16 ans += ((a >> i) & 1) == 0 and ((b >> i) & 1) == 0
17 else:
18 ans += (a >> i) & 1
19 ans += (b >> i) & 1
20 return ans
]]>
鎬濊礬涓錛氬紑涓猟ict瀛樻瘡涓暟鍑虹幇鍑犳錛屽啀鎵竴閬嶆壘鍑篸uplicate鍜宮issing
2 #Runtime: 447 ms
3 #Memory Usage: 15.5 MB
4
5 class Solution(object):
6 def findErrorNums(self, nums):
7 """
8 :type nums: List[int]
9 :rtype: List[int]
10 """
11 dict_num = {}
12 ans = [0, 0]
13 for i in nums:
14 if i not in dict_num:
15 dict_num[i] = 1
16 else:
17 dict_num[i] += 1
18 for i in range(1, len(nums) + 1):
19 if i not in dict_num:
20 ans[1] = i
21 elif dict_num[i] > 1:
22 ans[0] = i
23 return ans
鎬濊礬浜岋紙鐪媠olution寰楀埌鐨勫惎鍙戯紝涓嶄嬌鐢ㄥ叾浠杁ict絳夊浣欏瓨鍌級錛氱涓閬嶆壂鐨勬椂鍊欏皢浣嶄簬nums[abs(nums[i]) - 1]鐨勬暟*-1錛屽彂鐜版煇涓暟宸茬粡鏄礋鐨勮瘽璇存槑duplicate錛岀浜岄亶鍐嶆壂涓嬈★紝鎵懼嚭浠嶇劧澶т簬0鐨勬暟錛屽叾瀵瑰簲鐨勪笅鏍囧氨鏄痬issing鐨勯偅涓?br />
2 #Runtime: 495 ms
3 #Memory Usage: 14.2 MB
4
5 class Solution(object):
6 def findErrorNums(self, nums):
7 """
8 :type nums: List[int]
9 :rtype: List[int]
10 """
11 ans = [0, 0]
12 for i in range(len(nums)):
13 if nums[abs(nums[i]) - 1] < 0:
14 ans[0] = abs(nums[i])
15 else:
16 nums[abs(nums[i]) - 1] *= -1
17 for i in range(len(nums)):
18 if nums[i] > 0:
19 ans[1] = i + 1
20 return ans
鎬濊礬涓夛紙鐪媠olution寰楀埌鐨勫惎鍙戯紝寮傛垨鎬濇兂錛屽彧鐢ㄤ竴閲峟or寰幆錛屼絾闇瑕佷竴涓猟ict錛夛細鍘熺悊錛歛^b^b=a
2 #Runtime: 471 ms
3 #Memory Usage: 15.7 MB
4
5 class Solution(object):
6 def findErrorNums(self, nums):
7 """
8 :type nums: List[int]
9 :rtype: List[int]
10 """
11 dict_num = {}
12 ans = [0, 0]
13 for i in range(len(nums)):
14 if nums[i] not in dict_num:
15 dict_num[nums[i]] = 1
16 else:
17 ans[0] = nums[i]
18 dict_num[nums[i]] += 1
19 ans[1] ^= (i + 1)
20 ans[1] ^= nums[i]
21 ans[1] ^= ans[0]
22 return ans
鎬濊礬鍥涳紙鐪媠olution寰楀埌鐨勫惎鍙戯紝寮傛垨鎬濇兂錛屼絾瀹為檯鍙渶瑕佷笁閲峟or寰幆錛屼笉闇瑕侀澶杁ict錛夛紝鍘熺悊錛歛^b^b=a錛屾壘鍑篴鍜宐浜岃繘鍒舵渶鍚庝竴浣嶅嚭鐜頒笉鍚岀殑浣嶇疆錛屽皢1-n鍒嗕負涓ょ被錛屾壂涓閬峮ums鍜?-n涔嬪悗錛屽湪鍏朵腑涓綾諱細鍑虹幇a錛屽彟涓綾誨嚭鐜癰^b^b錛?b錛夛紝鍐嶆壂涓嬈ums灝辮兘鎵懼埌b浣嶄簬鍝竴綾?br />
2 #Runtime: 306 ms
3 #Memory Usage: 14.8 MB
4
5 class Solution(object):
6 def findErrorNums(self, nums):
7 """
8 :type nums: List[int]
9 :rtype: List[int]
10 """
11 ans = [0, 0]
12 xor = 0
13 for i in range(len(nums)):
14 xor ^= (i + 1)
15 xor ^= nums[i]
16 rightmostbit = xor & ~(xor - 1)
17 for i in range(len(nums)):
18 if (i + 1) & rightmostbit != 0:
19 ans[1] ^= (i + 1)
20 else:
21 ans[0] ^= (i + 1)
22 if nums[i] & rightmostbit != 0:
23 ans[1] ^= nums[i]
24 else:
25 ans[0] ^= nums[i]
26 for i in nums:
27 if ans[0] == i:
28 return ans
29 return [ans[1], ans[0]]
]]>
2 public:
3 int singleNumber(int A[], int n) {
4 unsigned int res = 0;
5 for(int j = 0; j < 32; ++j) {
6 int tp = 0;
7 const unsigned int pos = 1 << j;
8 for(int i = 0; i < n; ++i) {
9 tp += (A[i] & pos) > 0;
10 }
11 res |= (!(tp % 3)) ? 0 : pos;
12 }
13 return (int)res;
14 }
15 };
鍙︿竴縐嶆瘮杈冮珮澶т笂鐨勫仛娉曟槸寮涓変釜鍙橀噺錛屽垎鍒爣璁板嚭鐜拌繃涓嬈★紝涓ゆ錛屼笁嬈$殑錛屾瘡澶勭悊涓涓暟鐨勬椂鍊欏垎鍒綆楀紓鎴栥佷笌銆侀潪[鍑虹幇涓夋鏃跺墠涓や釜鍙橀噺閮戒負1錛屽彇鍙嶅悗鍒╃敤絎笁涓彉閲忔竻闄よ鏁癩錛岀劧鍚庣涓涓彉閲忎腑鍓╀笅鐨勫氨鏄彧鍑虹幇榪囦竴嬈$殑鏁?br />浠g爜宸ㄤ紭緹巭
2 public:
3 int singleNumber(int A[], int n) {
4 int tp3, tp1 = 0, tp2 = 0;
5 for(int i = 0; i < n; ++i) {
6 tp2 |= tp1 & A[i];
7 tp1 ^= A[i];
8 tp3 = ~(tp1 & tp2);
9 tp1 &= tp3;
10 tp2 &= tp3;
11 }
12 return tp1;
13 }
14 };
]]>