Skip to content

Commit b7116af

Browse files
committed
add python solution of contest 140, include 5383, 5084, 5086, 5087.
1 parent e514e1b commit b7116af

File tree

4 files changed

+122
-0
lines changed

4 files changed

+122
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'''
2+
分词判断即可
3+
'''
4+
5+
6+
class Solution:
7+
def findOcurrences(self, text: str, first: str, second: str) -> List[str]:
8+
ls = text.split()
9+
loc = 0
10+
ans = []
11+
while loc < len(ls) - 2:
12+
if ls[loc] == first and ls[loc + 1] == second:
13+
ans.append(ls[loc + 2])
14+
loc += 1
15+
return ans
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
'''
2+
对于每一个node选一条最大的路径,若其和小于limit,则该节点应该被删除。
3+
'''
4+
5+
# Definition for a binary tree node.
6+
# class TreeNode:
7+
# def __init__(self, x):
8+
# self.val = x
9+
# self.left = None
10+
# self.right = None
11+
12+
13+
class Solution:
14+
def sufficientSubset(self, root, limit: int) -> TreeNode:
15+
self.findAns(root, limit, 0)
16+
if root.val is None:
17+
return None
18+
self.trever(root)
19+
return root
20+
21+
def findAns(self, node, limit, presum):
22+
'''
23+
标记需要删除的节点
24+
'''
25+
if node is None:
26+
return 0
27+
left = self.findAns(node.left, limit, presum + node.val)
28+
right = self.findAns(node.right, limit, presum + node.val)
29+
ret = presum + node.val + max(left, right)
30+
if ret < limit:
31+
node.val = None
32+
return ret - presum
33+
34+
def trever(self, node):
35+
'''
36+
删除节点
37+
'''
38+
if node.left is not None:
39+
if node.left.val is None:
40+
node.left = None
41+
else:
42+
self.trever(node.left)
43+
if node.right is not None:
44+
if node.right.val is None:
45+
node.right = None
46+
else:
47+
self.trever(node.right)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
'''
2+
用栈思想。元素在字符串中出现的次数视为元素的可用性。
3+
从前往后遍历字符串。若当前字符为i,则:
4+
1、栈为空,i进栈;
5+
2、若i大于栈顶元素,i进栈;
6+
3、若i小于栈顶:1)i在栈中出现过一次,i可以性减一;2)进行出栈操作直到栈为空,或栈顶元素可用性为一,或i大于栈顶元素,将i进栈。每个元素出栈则该元素可用性减一。
7+
栈底到栈定元素次序即为所求。
8+
'''
9+
10+
11+
class Solution:
12+
def smallestSubsequence(self, text: str) -> str:
13+
dic = {}
14+
for i in range(len(text)):
15+
if text[i] not in dic:
16+
dic[text[i]] = 1
17+
else:
18+
dic[text[i]] += 1
19+
ls = []
20+
flag = set()
21+
loc = 0
22+
while loc < len(text):
23+
if len(ls) == 0:
24+
ls.append(text[loc])
25+
flag.add(text[loc])
26+
elif text[loc] in flag:
27+
dic[text[loc]] -= 1
28+
elif ls[-1] < text[loc]:
29+
ls.append(text[loc])
30+
flag.add(text[loc])
31+
elif ls[-1] >= text[loc]:
32+
while len(ls) > 0 and ls[-1] >= text[loc]:
33+
if dic[ls[-1]] == 1:
34+
break
35+
dic[ls[-1]] -= 1
36+
flag.remove(ls[-1])
37+
ls.pop(-1)
38+
ls.append(text[loc])
39+
flag.add(text[loc])
40+
loc += 1
41+
return ''.join(ls)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
'''
2+
搜索
3+
'''
4+
class Solution:
5+
6+
def numTilePossibilities(self, tiles: str) -> int:
7+
dic=set()
8+
visit=set()
9+
self.dfs(tiles,dic,visit,'')
10+
return len(dic)
11+
12+
def dfs(self,tiles,dic,visit,s):
13+
for i in range(len(tiles)):
14+
if i not in visit:
15+
visit.add(i)
16+
if s+tiles[i] not in dic:
17+
dic.add(s+tiles[i])
18+
self.dfs(tiles,dic,visit,s+tiles[i])
19+
visit.remove(i)

0 commit comments

Comments
 (0)