两数之和 VII
https://www.lintcode.com/problem/1879/description?utm_source=sc-cheatsheet-cyc
输入: [0,-1,2,-3,4]1输出: [[1,2],[3,4]]说明: nums[1] + nums[2] = -1 + 2 = 1, nums[3] + nums[4] = -3 + 4 = 1你也可以返回 [[3,4],[1,2]],系统将自动帮你排序成 [[1,2],[3,4]]。但是[[2,1],[3,4]]是不合法的。```python
from typing import (
List,
)
class Solution:
"""
@param nums: the input array
@param target: the target number
@return: return the target pair
we will sort your return value in output
"""
def two_sum_v_i_i(self, nums: List[int], target: int) -> List[List[int]]:
# write your code here
find = {}
res = []
if len(nums) < 2:
return res
for i, num in enumerate(nums):
if num in find:
res.append([find[num], i])
if abs(target - num) < abs(num):
continue
find[target - num] = i
return res
```Last updated