---
description: LeetCode 1 (allow duplicate numbers)
---

# Two Sum

```python
def twoSum(nums, target):
    # nums = [value1, value2, value3 ...]
    nums.sort() 
    # dict = {value1 : position1, value2 : position2}
        dict = {}
        res = []
        i = 0
        while i < len(nums):
            # 若 i > 0 且 值重覆了, 則繼續往下找
            if i > 0 and nums[i] == nums[i-1]:
                i = i + 1
                continue 
            x = nums[i]
            if x in dict:
                res.append((dict[x], i))
            dict[target - x] = i
            i = i + 1
        return res

twoSum([-3, -2, 1, 2, 2, 3, 4], 1)
```
