Skip to content

Commit 40ca33a

Browse files
committedDec 19, 2020
1219初始化
0 parents  commit 40ca33a

File tree

240 files changed

+6880
-0
lines changed

Some content is hidden

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

240 files changed

+6880
-0
lines changed
 

Diff for: ‎array/easy/1-two-sum-2.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
"""
2+
思路:把遍历过的数据存入缓存,每次查找之前,先在缓存中查找有没有,如果有目标数据,就不用算了。
3+
"""
4+
from typing import List
5+
6+
7+
class Solution:
8+
def twoSum(self, nums: List[int], target: int) -> List[int]:
9+
cache = {}
10+
for idx, v in enumerate(nums):
11+
if v in cache:
12+
return [cache[v], idx]
13+
remnant = target - v
14+
cache[remnant] = idx
15+
return None
16+
17+
18+
nums = [1, 1, 1, 1, 1, 2, 1, 4, 1, 1]
19+
s = Solution()
20+
print(s.twoSum(nums, 9))
21+

Diff for: ‎array/easy/1-two-sum.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"""
2+
思路:遍历表格,查找符合条件的值
3+
"""
4+
5+
6+
class Solution(object):
7+
def twoSum(self, nums, target):
8+
"""
9+
:type nums: List[int]
10+
:type target: int
11+
:rtype: List[int]
12+
"""
13+
index1 = 0
14+
index2 = 0
15+
for i in range(0, len(nums) - 1):
16+
for j in range(i + 1, len(nums)):
17+
if nums[i] + nums[j] == target:
18+
index1 = i
19+
index2 = j
20+
break
21+
return [index1, index2]
22+
23+
24+
nums = [2, 7, 11, 15]
25+
s = Solution()

0 commit comments

Comments
 (0)
Please sign in to comment.