Skip to content

Commit 291ca56

Browse files
committed
2019-11-13
1 parent 3d8c2a4 commit 291ca56

File tree

20 files changed

+484
-0
lines changed

20 files changed

+484
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution(object):
2+
def isSubsequence(self, s, t):
3+
"""
4+
:type s: str
5+
:type t: str
6+
:rtype: bool
7+
"""
8+
i, j = 0, 0
9+
while i < len(s) and j < len(t):
10+
if s[i] == t[j]:
11+
i += 1
12+
j += 1
13+
return i == len(s)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution(object):
2+
def totalHammingDistance(self, nums):
3+
"""
4+
:type nums: List[int]
5+
:rtype: int
6+
"""
7+
if not nums:
8+
return 0
9+
res = 0
10+
mask = 1
11+
for i in range(32):
12+
cnt_one = 0
13+
for num in nums:
14+
cnt_one += 1 if num & mask else 0
15+
16+
res += cnt_one * (len(nums) - cnt_one)
17+
mask = mask << 1
18+
return res
19+
20+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode(object):
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.left = None
6+
# self.right = None
7+
8+
class Solution(object):
9+
def diameterOfBinaryTree(self, root):
10+
"""
11+
:type root: TreeNode
12+
:rtype: int
13+
"""
14+
if not root:
15+
return 0
16+
self.res = 0
17+
def height(node):
18+
if not node:
19+
return 0
20+
left_h = height(node.left)
21+
right_h = height(node.right)
22+
23+
self.res = max(self.res, left_h + right_h)
24+
return 1 + max(left_h, right_h)
25+
height(root)
26+
return self.res
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution(object):
2+
def findLongestChain(self, pairs):
3+
"""
4+
:type pairs: List[List[int]]
5+
:rtype: int
6+
"""
7+
pairs = sorted(pairs, key = lambda x: x[1])
8+
9+
end = pairs[0][0] - 1
10+
res = 0
11+
for pair in pairs:
12+
if pair[0] > end:
13+
res += 1
14+
end = pair[1]
15+
16+
return res
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution(object):
2+
def numMatchingSubseq(self, S, words):
3+
"""
4+
:type S: str
5+
:type words: List[str]
6+
:rtype: int
7+
"""
8+
from collections import defaultdict
9+
10+
dic = defaultdict(list)
11+
for i, ch in enumerate(S):
12+
dic[ch].append(i)
13+
14+
res = 0
15+
for word in words:
16+
pre = -1
17+
flag = True
18+
for i, ch in enumerate(word):
19+
l = dic[ch]
20+
# 在l找第一个比pre大的元素
21+
idx = bisect.bisect(l, pre)
22+
23+
if idx == len(l):# 没找到
24+
flag = False
25+
break
26+
pre = l[idx]
27+
28+
if flag:
29+
res += 1
30+
31+
return res
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution(object):
2+
def countLetters(self, S):
3+
"""
4+
:type S: str
5+
:rtype: int
6+
"""
7+
res = 0
8+
for i in range(len(S)):
9+
for j in range(i + 1, len(S) + 1):
10+
substring = S[i:j]
11+
if substring == substring[0] * len(substring):
12+
res += 1
13+
# print substring
14+
return res
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution(object):
2+
def uniqueOccurrences(self, arr):
3+
"""
4+
:type arr: List[int]
5+
:rtype: bool
6+
"""
7+
d = collections.Counter(arr)
8+
return len(d.values()) == len(set(d.values()))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution(object):
2+
def arraysIntersection(self, arr1, arr2, arr3):
3+
"""
4+
:type arr1: List[int]
5+
:type arr2: List[int]
6+
:type arr3: List[int]
7+
:rtype: List[int]
8+
"""
9+
res = []
10+
record = [0 for _ in range(2005)]
11+
for num in arr1 + arr2 + arr3:
12+
record[num] += 1
13+
14+
for i in range(len(record)):
15+
if record[i] == 3:
16+
res.append(i)
17+
18+
return res
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution(object):
2+
def balancedStringSplit(self, s):
3+
"""
4+
:type s: str
5+
:rtype: int
6+
"""
7+
if not s:
8+
return 0
9+
# print s
10+
l, r = 0, 0
11+
12+
for i in range(len(s)):
13+
if s[i] == "R":
14+
r += 1
15+
else:
16+
l += 1
17+
# print r, l
18+
if l == r:
19+
return 1 + self.balancedStringSplit(s[i + 1:])
20+
21+
return 0
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution(object):
2+
def checkStraightLine(self, coordinates):
3+
"""
4+
:type coordinates: List[List[int]]
5+
:rtype: bool
6+
"""
7+
c = sorted(coordinates, key = lambda x:x[0])
8+
k = None
9+
for i in range(len(c)):
10+
if i:
11+
x0, y0 = c[i - 1][0], c[i - 1][1]
12+
x1, y1 = c[i][0], c[i][1]
13+
14+
if x0 == x1:
15+
return False
16+
new_k = 1.0 * (y1 - y0) / (x1 - x0)
17+
if k and k != new_k:
18+
return False
19+
k = new_k
20+
21+
return True
22+
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution(object):
2+
def transformArray(self, arr):
3+
"""
4+
:type arr: List[int]
5+
:rtype: List[int]
6+
"""
7+
flag = 1
8+
while flag:
9+
flag = 0
10+
res = [num for num in arr]
11+
for i in range(1, len(arr) - 1):
12+
if arr[i - 1] < arr[i] and arr[i] > arr[i + 1]:
13+
res[i] -= 1
14+
flag = 1
15+
elif arr[i - 1] > arr[i] and arr[i] < arr[i + 1]:
16+
res[i] += 1
17+
flag = 1
18+
arr = res[:]
19+
return res
20+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from collections import defaultdict
2+
from heapq import *
3+
class Leaderboard(object):
4+
5+
def __init__(self):
6+
self.dic = defaultdict(int)
7+
8+
def addScore(self, playerId, score):
9+
"""
10+
:type playerId: int
11+
:type score: int
12+
:rtype: None
13+
"""
14+
self.dic[playerId] += score
15+
16+
def top(self, K):
17+
"""
18+
:type K: int
19+
:rtype: int
20+
"""
21+
self.l = []
22+
heapify(self.l)
23+
for pid, score in self.dic.items():
24+
if len(self.l) >= K:
25+
if score > self.l[0]:
26+
heappush(self.l, score)
27+
heappop(self.l)
28+
else:
29+
heappush(self.l, score)
30+
31+
return sum(self.l)
32+
33+
34+
def reset(self, playerId):
35+
"""
36+
:type playerId: int
37+
:rtype: None
38+
"""
39+
self.dic[playerId] = 0
40+
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from collections import defaultdict
2+
class Solution(object):
3+
def treeDiameter(self, edges):
4+
"""
5+
:type edges: List[List[int]]
6+
:rtype: int
7+
"""
8+
if not edges:
9+
return 0
10+
11+
self.neibors = defaultdict(set)
12+
self.res = 0
13+
14+
for start, end in edges: # 建树
15+
self.neibors[start].add(end)
16+
17+
def getHeight(node):
18+
res = []
19+
for neibor in self.neibors[node]:
20+
res.append(getHeight(neibor))
21+
22+
while len(res) < 2: # 如果孩子少于两个,就给它补空的上去
23+
res.append(0)
24+
25+
res = sorted(res)
26+
self.res = max(self.res, sum(res[-2:])) # 取最长的两个子树长度
27+
return 1 + max(res)
28+
29+
getHeight(edges[0][0])
30+
return self.res
31+
32+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution(object):
2+
def minimumSwap(self, s1, s2):
3+
"""
4+
:type s1: str
5+
:type s2: str
6+
:rtype: int
7+
"""
8+
s = s1 + s2
9+
x = s.count("x")
10+
if len(s1) != len(s2) or x % 2 == 1 or (len(s) - x) % 2 == 1:
11+
return -1
12+
13+
pair1 = 0
14+
pair2 = 0
15+
for i in range(len(s1)):
16+
if s1[i] == "y" and s2[i] == "x":
17+
pair1 += 1
18+
elif s1[i] == "x" and s2[i] == "y":
19+
pair2 += 1
20+
21+
return pair1 // 2 + pair2 // 2 + pair1 % 2 + pair2 % 2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
class Solution(object):
2+
def numberOfSubarrays(self, nums, k):
3+
"""
4+
:type nums: List[int]
5+
:type k: int
6+
:rtype: int
7+
"""
8+
if not nums:
9+
return 0
10+
res = 0
11+
odd = []
12+
for i, num in enumerate(nums):
13+
if num % 2:
14+
odd.append(i)
15+
16+
if len(odd) < k:
17+
return 0
18+
19+
20+
for i in range(len(odd)):
21+
if i + k > len(odd):
22+
break
23+
if i:
24+
last = odd[i - 1]
25+
else:
26+
last = -1
27+
28+
if i + k < len(odd):
29+
nxt = odd[i + k]
30+
else:
31+
nxt = len(nums)
32+
33+
left = odd[i] - last
34+
right = nxt - odd[i + k - 1]
35+
res += left * right
36+
return res
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution(object):
2+
def minRemoveToMakeValid(self, s):
3+
"""
4+
:type s: str
5+
:rtype: str
6+
"""
7+
left, right = 0, 0
8+
stack = []
9+
remove = set()
10+
for i, ch in enumerate(s):
11+
if ch == "(":
12+
stack.append(i)
13+
elif ch == ")":
14+
if stack:
15+
stack.pop()
16+
else:
17+
remove.add(i)
18+
stack = set(stack)
19+
res = ""
20+
for i, ch in enumerate(s):
21+
if i not in stack and i not in remove:
22+
res += ch
23+
return res

0 commit comments

Comments
 (0)