File tree Expand file tree Collapse file tree 1 file changed +3
-22
lines changed Expand file tree Collapse file tree 1 file changed +3
-22
lines changed Original file line number Diff line number Diff line change 2626lookup {2:0} [0,1]
2727```
2828
29- 一但字典内有了这个 ` target - 当前数字 ` ,找到它的index和当前index一起返回。
30-
29+ * 建立字典 lookup 存放第一个数字,并存放该数字的 index
30+ * 判断 lookup 种是否存在: ` target - 当前数字 ` , 则表面 当前值和 lookup中的值加和为 target.
31+ * 如果存在,则返回: ` target - 当前数字 ` 的 index 和 当前值的 index
3132
3233``` python
3334class Solution (object ):
@@ -44,23 +45,3 @@ class Solution(object):
4445 lookup[num] = i
4546 return []
4647```
47-
48- > 思路 2
49-
50- 通过 差值 判断元素,是否存在 nums,存在就 return
51-
52- ``` python
53- class Solution :
54- def twoSum (self , nums , target ):
55- """
56- :type nums: List[int]
57- :type target: int
58- :rtype: List[int]
59- """
60- for num in nums:
61- if num < target:
62- l1_index = nums.index(num)
63- l2 = target - num
64- if l2 in nums:
65- return [l1_index, nums.index(l2)]
66- ```
You can’t perform that action at this time.
0 commit comments