Skip to content

Commit 6ef504c

Browse files
committed
2020-07-10
1 parent 6429124 commit 6ef504c

File tree

2 files changed

+12
-20
lines changed

2 files changed

+12
-20
lines changed

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

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
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
1+
class Solution:
2+
def twoSum(self, nums: List[int], target: int) -> List[int]:
3+
hashmap = {}
4+
for index, item in enumerate(nums):
5+
if target - item in hashmap:
6+
return hashmap[target-item],index
7+
hashmap[item] = index

315.计算右侧小于当前元素的个数/315-计算右侧小于当前元素的个数.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ def countSmaller(self, nums):
1111
:type nums: List[int]
1212
:rtype: List[int]
1313
"""
14-
1514
# 从左往右处理,右边是未知的,所以不好弄
1615
# 从右往左处理,则对于每个数,其右边的数都已知
1716
res = [0 for _ in nums]
@@ -21,15 +20,13 @@ def countSmaller(self, nums):
2120
return res[::-1]
2221

2322
def insert(self, root, val, i, res):
24-
if not root:
23+
if not root: #如果当前root为空
2524
root = TreeNode(val)
26-
elif root.val >= val:
25+
elif root.val >= val: # 如果应该插入左子树
2726
root.left_subtree_cnt += 1
2827
root.left = self.insert(root.left, val, i, res)
29-
elif root.val < val:
30-
res[i] += root.left_subtree_cnt + 1
28+
elif root.val < val: # 如果应该插入右子树
29+
res[i] += root.left_subtree_cnt + 1 # 1 代表当前root
3130
root.right = self.insert(root.right, val, i, res)
3231

33-
return root
34-
35-
32+
return root

0 commit comments

Comments
 (0)