Skip to content

Commit b1f7d10

Browse files
committed
2020-07-01
1 parent be2e252 commit b1f7d10

File tree

241 files changed

+5372
-37
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

241 files changed

+5372
-37
lines changed

1.两数之和/1-两数之和.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution(object):
2+
def twoSum(self, nums, target):
3+
"""
4+
:type nums: List[int]
5+
:type target: int
6+
:rtype: List[int]
7+
"""
8+
dic = {}
9+
for i, num in enumerate(nums):
10+
if target - num in dic:
11+
return [dic[target - num], i]
12+
dic[num] = i
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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 levelOrder(self, root):
10+
"""
11+
:type root: TreeNode
12+
:rtype: List[List[int]]
13+
"""
14+
if not root:
15+
return []
16+
queue = [root]
17+
res = list()
18+
while queue:
19+
nextqueue = list()
20+
layer = list()
21+
for node in queue:
22+
if node.left:
23+
nextqueue.append(node.left)
24+
if node.right:
25+
nextqueue.append(node.right)
26+
layer.append(node.val)
27+
28+
queue = nextqueue[:]
29+
res.append(layer)
30+
return res
31+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution(object):
2+
def maxArea(self, height):
3+
"""
4+
:type height: List[int]
5+
:rtype: int
6+
"""
7+
lo, hi = 0, len(height) - 1
8+
res = 0
9+
while(lo < hi):
10+
if height[lo] > height[hi]:
11+
area = height[hi] * (hi - lo)
12+
hi -= 1
13+
else:
14+
area = height[lo] * (hi - lo)
15+
lo += 1
16+
# print area
17+
res = max(area, res)
18+
19+
return res
20+

1150.检查一个数是否在数组中占绝大多数/1150-检查一个数是否在数组中占绝大多数.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ def isMajorityElement(self, nums, target):
55
:type target: int
66
:rtype: bool
77
"""
8-
return nums.count(target) > len(nums) // 2
8+
return nums.count(target) > (len(nums) // 2)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
"""
2+
# Definition for a Node.
3+
class Node(object):
4+
def __init__(self, val=0, left=None, right=None, next=None):
5+
self.val = val
6+
self.left = left
7+
self.right = right
8+
self.next = next
9+
"""
10+
class Solution(object):
11+
def connect(self, root):
12+
"""
13+
:type root: Node
14+
:rtype: Node
15+
"""
16+
if not root or not root.left:
17+
return root
18+
19+
root.left.next = root.right
20+
if root.next:
21+
root.right.next = root.next.left
22+
self.connect(root.left)
23+
self.connect(root.right)
24+
25+
return root
26+
27+
def findNext(self, node):
28+
nxt = node.next
29+
while nxt:
30+
if nxt.left:
31+
return nxt.left
32+
elif nxt.right:
33+
return nxt.right
34+
else:
35+
nxt = nxt.next
36+
return None
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
"""
2+
# Definition for a Node.
3+
class Node(object):
4+
def __init__(self, val=0, left=None, right=None, next=None):
5+
self.val = val
6+
self.left = left
7+
self.right = right
8+
self.next = next
9+
"""
10+
class Solution(object):
11+
def connect(self, root):
12+
"""
13+
:type root: Node
14+
:rtype: Node
15+
"""
16+
if not root:
17+
return root
18+
19+
if root.left and root.right:
20+
root.left.next = root.right
21+
root.right.next = self.findNext(root)
22+
23+
elif root.left:
24+
root.left.next = self.findNext(root)
25+
elif root.right:
26+
root.right.next = self.findNext(root)
27+
28+
self.connect(root.right)
29+
self.connect(root.left)
30+
31+
return root
32+
33+
def findNext(self, node):
34+
nxt = node.next
35+
while nxt:
36+
if nxt.left:
37+
return nxt.left
38+
if nxt.right:
39+
return nxt.right
40+
nxt = nxt.next
41+
return None

1170.比较字符串最小字母出现频次/1170-比较字符串最小字母出现频次.py

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,18 @@ def numSmallerByFrequency(self, queries, words):
55
:type words: List[str]
66
:rtype: List[int]
77
"""
8+
def f(word):
9+
return word.count(min(word))
810

9-
def func(word):
10-
for char in "abcdefghijklmnopqrstuvwxyz":
11-
if char in word:
12-
return word.count(char)
13-
return 0
11+
f_words = sorted([f(word) for word in words])
1412

15-
def func2(word):
16-
record = collections.Counter(word)
17-
return record[min(record.keys())]
18-
19-
20-
words_count = sorted(map(func2, words))
21-
queries_count = map(func2, queries)
22-
# print words_count, queries_count
23-
ans = []
24-
for query in queries_count:
25-
index = bisect.bisect(words_count, query) #bisect可以迅速找出有index个数 <= query
26-
ans.append(len(words_count) - index)# 减法找有多少个数比query大
27-
return ans
13+
res = []
14+
# print f_words
15+
for q in queries:
16+
cnt = f(q)
17+
# print(bisect.bisect(f_words, cnt))
18+
res.append(len(f_words) - bisect.bisect(f_words, cnt))
19+
20+
return res
21+
2822

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution(object):
2+
def removeDuplicates(self, s, k):
3+
"""
4+
:type s: str
5+
:type k: int
6+
:rtype: str
7+
"""
8+
stack = []
9+
for ch in s:
10+
if not stack:
11+
stack.append([ch, 1])
12+
else:
13+
if stack[-1][0] == ch:
14+
stack[-1][1] += 1
15+
if stack[-1][1] % k == 0:
16+
stack.pop()
17+
else:
18+
stack.append([ch, 1])
19+
20+
return "".join(ch * freq for ch, freq in stack)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution(object):
2+
def maxProfit(self, prices):
3+
"""
4+
:type prices: List[int]
5+
:rtype: int
6+
"""
7+
res = 0
8+
pre_min = prices[0] if prices else 0
9+
for price in prices:
10+
res = max(res, price - pre_min)
11+
pre_min = min(pre_min, price)
12+
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 getMaximumGold(self, grid):
3+
"""
4+
:type grid: List[List[int]]
5+
:rtype: int
6+
"""
7+
dx = [1, -1, 0, 0]
8+
dy = [0, 0, 1, -1]
9+
m, n = len(grid), len(grid[0])
10+
visited = set()
11+
self.res = 0
12+
13+
def dfs(x0, y0, tmp):
14+
self.res = max(self.res, tmp)
15+
for k in range(4):
16+
x = x0 + dx[k]
17+
y = y0 + dy[k]
18+
19+
if 0 <= x < m and 0 <= y < n and grid[x][y] > 0:
20+
value = grid[x][y]
21+
grid[x][y] = -1
22+
dfs(x, y, tmp + value)
23+
grid[x][y] = value
24+
25+
for i in range(m):
26+
for j in range(n):
27+
value = grid[i][j]
28+
grid[i][j] = -1
29+
dfs(i, j, value)
30+
grid[i][j] = value
31+
return self.res

0 commit comments

Comments
 (0)