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
Lines changed: 13 additions & 0 deletions
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)
Lines changed: 20 additions & 0 deletions
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+
Lines changed: 26 additions & 0 deletions
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
Lines changed: 16 additions & 0 deletions
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
Lines changed: 31 additions & 0 deletions
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
Lines changed: 14 additions & 0 deletions
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
Lines changed: 8 additions & 0 deletions
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()))
Lines changed: 18 additions & 0 deletions
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
Lines changed: 21 additions & 0 deletions
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
Lines changed: 22 additions & 0 deletions
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+

0 commit comments

Comments
 (0)