mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
0001. Add python v2. Making use of the dictionary property.
This commit is contained in:
@ -118,6 +118,18 @@ class Solution:
|
|||||||
return [records[target - val], idx] # 如果存在就返回字典记录索引和当前索引
|
return [records[target - val], idx] # 如果存在就返回字典记录索引和当前索引
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Python (v2):
|
||||||
|
|
||||||
|
```python
|
||||||
|
class Solution:
|
||||||
|
def twoSum(self, nums: List[int], target: int) -> List[int]:
|
||||||
|
rec = {}
|
||||||
|
for i in range(len(nums)):
|
||||||
|
rest = target - nums[i]
|
||||||
|
# Use get to get the index of the data, making use of one of the dictionary properties.
|
||||||
|
if rec.get(rest, None) is not None: return [rec[rest], i]
|
||||||
|
rec[nums[i]] = i
|
||||||
|
```
|
||||||
|
|
||||||
Go:
|
Go:
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user