Skip to content

Commit 781ba37

Browse files
committed
feat: add solutions to lcci problem: No.16.24
No.16.24.Pairs With Sum
1 parent 8a61401 commit 781ba37

File tree

6 files changed

+219
-3
lines changed

6 files changed

+219
-3
lines changed

lcci/16.24.Pairs With Sum/README.md

+90-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
## 题目描述
66

77
<!-- 这里写题目描述 -->
8+
89
<p>设计一个算法,找出数组中两数之和为指定值的所有整数对。一个数只能属于一个数对。</p>
910
<p><strong>示例 1:</strong></p>
1011
<pre><strong>输入:</strong> nums = [5,6,5], target = 11
@@ -16,19 +17,106 @@
1617
<ul>
1718
<li><code>nums.length &lt;= 100000</code></li>
1819
</ul>
20+
1921
## 解法
22+
2023
<!-- 这里可写通用的实现逻辑 -->
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+
2135
<!-- tabs:start -->
36+
2237
### **Python3**
38+
2339
<!-- 这里可写当前语言的特殊实现逻辑 -->
40+
2441
```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+
```
2555

26-
````
2756
### **Java**
57+
2858
<!-- 这里可写当前语言的特殊实现逻辑 -->
59+
2960
```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+
```
3080

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+
```
32120

33121
### **...**
34122

lcci/16.24.Pairs With Sum/README_EN.md

+69-1
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,81 @@
2929
### **Python3**
3030

3131
```python
32-
32+
class Solution:
33+
def pairSums(self, nums: List[int], target: int) -> List[List[int]]:
34+
cnt = Counter()
35+
ans = []
36+
for x in nums:
37+
y = target - x
38+
if cnt[y]:
39+
cnt[y] -= 1
40+
ans.append([x, y])
41+
else:
42+
cnt[x] += 1
43+
return ans
3344
```
3445

3546
### **Java**
3647

3748
```java
49+
class Solution {
50+
public List<List<Integer>> pairSums(int[] nums, int target) {
51+
Map<Integer, Integer> cnt = new HashMap<>();
52+
List<List<Integer>> ans = new ArrayList<>();
53+
for (int x : nums) {
54+
int y = target - x;
55+
if (cnt.containsKey(y)) {
56+
ans.add(List.of(x, y));
57+
if (cnt.merge(y, -1, Integer::sum) == 0) {
58+
cnt.remove(y);
59+
}
60+
} else {
61+
cnt.merge(x, 1, Integer::sum);
62+
}
63+
}
64+
return ans;
65+
}
66+
}
67+
```
68+
69+
### **C++**
70+
71+
```cpp
72+
class Solution {
73+
public:
74+
vector<vector<int>> pairSums(vector<int>& nums, int target) {
75+
unordered_map<int, int> cnt;
76+
vector<vector<int>> ans;
77+
for (int x : nums) {
78+
int y = target - x;
79+
if (cnt[y]) {
80+
--cnt[y];
81+
ans.push_back({x, y});
82+
} else {
83+
++cnt[x];
84+
}
85+
}
86+
return ans;
87+
}
88+
};
89+
```
3890
91+
### **Go**
92+
93+
```go
94+
func pairSums(nums []int, target int) (ans [][]int) {
95+
cnt := map[int]int{}
96+
for _, x := range nums {
97+
y := target - x
98+
if cnt[y] > 0 {
99+
cnt[y]--
100+
ans = append(ans, []int{x, y})
101+
} else {
102+
cnt[x]++
103+
}
104+
}
105+
return
106+
}
39107
```
40108

41109
### **...**
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public:
3+
vector<vector<int>> pairSums(vector<int>& nums, int target) {
4+
unordered_map<int, int> cnt;
5+
vector<vector<int>> ans;
6+
for (int x : nums) {
7+
int y = target - x;
8+
if (cnt[y]) {
9+
--cnt[y];
10+
ans.push_back({x, y});
11+
} else {
12+
++cnt[x];
13+
}
14+
}
15+
return ans;
16+
}
17+
};

lcci/16.24.Pairs With Sum/Solution.go

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
func pairSums(nums []int, target int) (ans [][]int) {
2+
cnt := map[int]int{}
3+
for _, x := range nums {
4+
y := target - x
5+
if cnt[y] > 0 {
6+
cnt[y]--
7+
ans = append(ans, []int{x, y})
8+
} else {
9+
cnt[x]++
10+
}
11+
}
12+
return
13+
}
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public List<List<Integer>> pairSums(int[] nums, int target) {
3+
Map<Integer, Integer> cnt = new HashMap<>();
4+
List<List<Integer>> ans = new ArrayList<>();
5+
for (int x : nums) {
6+
int y = target - x;
7+
if (cnt.containsKey(y)) {
8+
ans.add(List.of(x, y));
9+
if (cnt.merge(y, -1, Integer::sum) == 0) {
10+
cnt.remove(y);
11+
}
12+
} else {
13+
cnt.merge(x, 1, Integer::sum);
14+
}
15+
}
16+
return ans;
17+
}
18+
}

lcci/16.24.Pairs With Sum/Solution.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
def pairSums(self, nums: List[int], target: int) -> List[List[int]]:
3+
cnt = Counter()
4+
ans = []
5+
for x in nums:
6+
y = target - x
7+
if cnt[y]:
8+
cnt[y] -= 1
9+
ans.append([x, y])
10+
else:
11+
cnt[x] += 1
12+
return ans

0 commit comments

Comments
 (0)