Skip to content

Commit b29c26b

Browse files
author
Jiayang Wu
committed
2019-11-14
1 parent 3cd8b24 commit b29c26b

File tree

3 files changed

+73
-28
lines changed

3 files changed

+73
-28
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution(object):
2+
def getHint(self, secret, guess):
3+
"""
4+
:type secret: str
5+
:type guess: str
6+
:rtype: str
7+
"""
8+
from collections import Counter
9+
dic_s = Counter(secret)
10+
dic_g = Counter(guess)
11+
12+
a, b = 0, 0
13+
for i in range(len(secret)):
14+
if secret[i] == guess[i]:
15+
a += 1
16+
dic_s[secret[i]] -= 1
17+
dic_g[secret[i]] -= 1
18+
19+
for i in dic_s & dic_g:
20+
b += min(dic_s[i], dic_g[i])
21+
22+
return "{}A{}B".format(a, b)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class TreeNode(object):
2+
def __init__(self, val):
3+
self.left = None
4+
self.right = None
5+
self.val = val
6+
self.left_subtree_cnt = 0
7+
8+
class Solution(object):
9+
def countSmaller(self, nums):
10+
"""
11+
:type nums: List[int]
12+
:rtype: List[int]
13+
"""
14+
15+
# 从左往右处理,右边是未知的,所以不好弄
16+
# 从右往左处理,则对于每个数,其右边的数都已知
17+
res = [0 for _ in nums]
18+
root = None
19+
for i, num in enumerate(nums[::-1]):
20+
root = self.insert(root, num, i, res)
21+
return res[::-1]
22+
23+
def insert(self, root, val, i, res):
24+
if not root:
25+
root = TreeNode(val)
26+
elif root.val >= val:
27+
root.left_subtree_cnt += 1
28+
root.left = self.insert(root.left, val, i, res)
29+
elif root.val < val:
30+
res[i] += root.left_subtree_cnt + 1
31+
root.right = self.insert(root.right, val, i, res)
32+
33+
return root
34+
35+

1055.形成字符串的最短路径/1055-形成字符串的最短路径.py

+16-28
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,21 @@ def shortestWay(self, source, target):
55
:type target: str
66
:rtype: int
77
"""
8-
9-
import collections
10-
dicse = collections.Counter(source)
11-
dictt = collections.Counter(target)
12-
13-
for key, val in dictt.items():
14-
if dicse.get(key, 0) == 0:
8+
s = set(source)
9+
t = set(target)
10+
for ch in t:
11+
if ch not in s:
1512
return -1
16-
17-
cnt = 0
18-
start, end = 0, 0
19-
i = 0
20-
while(i < len(target)):
21-
char = target[i]
22-
while(source[start] != char): #找起点,一定能找到
23-
start += 1
24-
end = start + 1
25-
26-
i += 1
27-
while(i < len(target) and end < len(source)):
28-
if source[end] == target[i]: #找最长的子序列
29-
i += 1
30-
end += 1
31-
32-
cnt += 1
33-
start = 0 #重置起点,为下一轮做准备
3413

35-
return cnt
36-
37-
14+
i, j = 0, 0
15+
res = 0
16+
while j < len(target):
17+
i = 0
18+
while i < len(source) and j < len(target):
19+
if source[i] == target[j]:
20+
i += 1
21+
j += 1
22+
else:
23+
i += 1
24+
res += 1
25+
return res

0 commit comments

Comments
 (0)