File tree Expand file tree Collapse file tree 1 file changed +8
-3
lines changed Expand file tree Collapse file tree 1 file changed +8
-3
lines changed Original file line number Diff line number Diff line change 1
1
# 提交版本
2
2
3
+ # 解题思路:
4
+ # 1、建立一个字典
5
+ # 2、扫描一遍list,如果target-nums[i]结果在字典内,即返回
6
+ # 3、注意特例:减法结果为nums[i]时,无效
7
+ # 参考资料:
8
+ # https://blog.csdn.net/weixin_43557139/article/details/120626173
9
+ # https://www.runoob.com/python3/python-sort-dictionaries-by-key-or-value.html
10
+
3
11
# class Solution(object):
4
12
# def twoSum(self, nums, target):
5
13
# """
@@ -32,15 +40,12 @@ def twoSum(nums, target):
32
40
"""
33
41
key_value = {} # 数值作为key,索引作为value
34
42
for i in range (len (nums )):
35
- #key_value[i] = nums[i]
36
43
key_value [nums [i ]] = i
37
- # sorted(key_value.items(), key = lambda kv:(kv[1], kv[0]))
38
44
39
45
res_id = []
40
46
for i in range (len (nums )):
41
47
res = target - nums [i ]
42
48
if res in key_value :
43
- # key_res = key_value.get(res, default=None)
44
49
kv = key_value [res ]
45
50
if kv != i :
46
51
id_stop = key_value [res ]
You can’t perform that action at this time.
0 commit comments