|
5 | 5 | ## 题目描述
|
6 | 6 |
|
7 | 7 | <!-- 这里写题目描述 -->
|
| 8 | + |
8 | 9 | <p>设计一个算法,找出数组中两数之和为指定值的所有整数对。一个数只能属于一个数对。</p>
|
9 | 10 | <p><strong>示例 1:</strong></p>
|
10 | 11 | <pre><strong>输入:</strong> nums = [5,6,5], target = 11
|
|
16 | 17 | <ul>
|
17 | 18 | <li><code>nums.length <= 100000</code></li>
|
18 | 19 | </ul>
|
| 20 | + |
19 | 21 | ## 解法
|
| 22 | + |
20 | 23 | <!-- 这里可写通用的实现逻辑 -->
|
| 24 | + |
| 25 | +**方法一:哈希表** |
| 26 | + |
| 27 | +我们可以使用哈希表来存储数组中的元素,键为数组中的元素,值为该元素出现的次数。 |
| 28 | + |
| 29 | +遍历数组,对于每个元素 $x$,我们计算 $y = target - x$,如果哈希表中存在 $y$,则说明存在一对数 $(x, y)$,我们将其加入答案,并减少 $y$ 的出现次数。如果哈希表中不存在 $y$,则说明不存在这样的数对,我们将 $x$ 的出现次数加 $1$。 |
| 30 | + |
| 31 | +遍历结束后,即可得到答案。 |
| 32 | + |
| 33 | +时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组的长度。 |
| 34 | + |
21 | 35 | <!-- tabs:start -->
|
| 36 | + |
22 | 37 | ### **Python3**
|
| 38 | + |
23 | 39 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
| 40 | + |
24 | 41 | ```python
|
| 42 | +class Solution: |
| 43 | + def pairSums(self, nums: List[int], target: int) -> List[List[int]]: |
| 44 | + cnt = Counter() |
| 45 | + ans = [] |
| 46 | + for x in nums: |
| 47 | + y = target - x |
| 48 | + if cnt[y]: |
| 49 | + cnt[y] -= 1 |
| 50 | + ans.append([x, y]) |
| 51 | + else: |
| 52 | + cnt[x] += 1 |
| 53 | + return ans |
| 54 | +``` |
25 | 55 |
|
26 |
| -```` |
27 | 56 | ### **Java**
|
| 57 | + |
28 | 58 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
| 59 | + |
29 | 60 | ```java
|
| 61 | +class Solution { |
| 62 | + public List<List<Integer>> pairSums(int[] nums, int target) { |
| 63 | + Map<Integer, Integer> cnt = new HashMap<>(); |
| 64 | + List<List<Integer>> ans = new ArrayList<>(); |
| 65 | + for (int x : nums) { |
| 66 | + int y = target - x; |
| 67 | + if (cnt.containsKey(y)) { |
| 68 | + ans.add(List.of(x, y)); |
| 69 | + if (cnt.merge(y, -1, Integer::sum) == 0) { |
| 70 | + cnt.remove(y); |
| 71 | + } |
| 72 | + } else { |
| 73 | + cnt.merge(x, 1, Integer::sum); |
| 74 | + } |
| 75 | + } |
| 76 | + return ans; |
| 77 | + } |
| 78 | +} |
| 79 | +``` |
30 | 80 |
|
31 |
| -```` |
| 81 | +### **C++** |
| 82 | + |
| 83 | +```cpp |
| 84 | +class Solution { |
| 85 | +public: |
| 86 | + vector<vector<int>> pairSums(vector<int>& nums, int target) { |
| 87 | + unordered_map<int, int> cnt; |
| 88 | + vector<vector<int>> ans; |
| 89 | + for (int x : nums) { |
| 90 | + int y = target - x; |
| 91 | + if (cnt[y]) { |
| 92 | + --cnt[y]; |
| 93 | + ans.push_back({x, y}); |
| 94 | + } else { |
| 95 | + ++cnt[x]; |
| 96 | + } |
| 97 | + } |
| 98 | + return ans; |
| 99 | + } |
| 100 | +}; |
| 101 | +``` |
| 102 | +
|
| 103 | +### **Go** |
| 104 | +
|
| 105 | +```go |
| 106 | +func pairSums(nums []int, target int) (ans [][]int) { |
| 107 | + cnt := map[int]int{} |
| 108 | + for _, x := range nums { |
| 109 | + y := target - x |
| 110 | + if cnt[y] > 0 { |
| 111 | + cnt[y]-- |
| 112 | + ans = append(ans, []int{x, y}) |
| 113 | + } else { |
| 114 | + cnt[x]++ |
| 115 | + } |
| 116 | + } |
| 117 | + return |
| 118 | +} |
| 119 | +``` |
32 | 120 |
|
33 | 121 | ### **...**
|
34 | 122 |
|
|
0 commit comments