Skip to content

Commit 0776d44

Browse files
committed
feat: add solutions to lcci problem: No.16.21. Sum Swap
1 parent e722e68 commit 0776d44

File tree

6 files changed

+217
-44
lines changed

6 files changed

+217
-44
lines changed

lcci/16.21.Sum Swap/README.md

+76-15
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131
<!-- 这里可写通用的实现逻辑 -->
3232

33-
先计算两个数组的差值 `diff`,若 `diff` 为奇数,则说明无满足条件的数值,返回空数组。否则,将 `array2` 转为 `set`。然后遍历 `array1` 中的每个数 `e`,若值 `e - diff``set` 中,则说明找到满足条件的数值对。
33+
先计算两个数组的差值 `diff`,若 `diff` 为奇数,则说明无满足条件的数值,返回空数组。否则,将 `array2` 转为 `set`。然后遍历 `array1` 中的每个数 `a`,若值 `a - diff``set` 中,则说明找到满足条件的数值对。
3434

3535
<!-- tabs:start -->
3636

@@ -42,11 +42,14 @@
4242
class Solution:
4343
def findSwapValues(self, array1: List[int], array2: List[int]) -> List[int]:
4444
diff = sum(array1) - sum(array2)
45-
if diff & 1: return []
45+
if diff & 1:
46+
return []
4647
diff >>= 1
4748
s = set(array2)
48-
for e in array1:
49-
if (e - diff) in s: return [e, e - diff]
49+
for a in array1:
50+
b = a - diff
51+
if b in s:
52+
return [a, b]
5053
return []
5154
```
5255

@@ -57,28 +60,86 @@ class Solution:
5760
```java
5861
class Solution {
5962
public int[] findSwapValues(int[] array1, int[] array2) {
60-
int diff = sum(array1) - sum(array2);
63+
int s1 = 0, s2 = 0;
64+
Set<Integer> s = new HashSet<>();
65+
for (int a : array1) {
66+
s1 += a;
67+
}
68+
for (int b : array2) {
69+
s.add(b);
70+
s2 += b;
71+
}
72+
int diff = s1 - s2;
6173
if ((diff & 1) == 1) {
6274
return new int[]{};
6375
}
6476
diff >>= 1;
65-
Set<Integer> s = Arrays.stream(array2).boxed().collect(Collectors.toSet());
66-
for (int e : array1) {
67-
if (s.contains((e - diff))) {
68-
return new int[]{e, e - diff};
77+
for (int a : array1) {
78+
int b = a - diff;
79+
if (s.contains(b)) {
80+
return new int[]{a, b};
6981
}
7082
}
7183
return new int[]{};
7284
}
85+
}
86+
```
7387

74-
private int sum(int[] array) {
75-
int res = 0;
76-
for (int e : array) {
77-
res += e;
88+
### **C++**
89+
90+
```cpp
91+
class Solution {
92+
public:
93+
vector<int> findSwapValues(vector<int>& array1, vector<int>& array2) {
94+
int s1 = 0, s2 = 0;
95+
unordered_set<int> s;
96+
for (int a : array1) s1 += a;
97+
for (int b : array2) {
98+
s2 += b;
99+
s.insert(b);
100+
}
101+
int diff = s1 - s2;
102+
if (diff & 1) {
103+
return {};
78104
}
79-
return res;
105+
diff >>= 1;
106+
for (int a : array1) {
107+
int b = a - diff;
108+
if (s.count(b)) {
109+
return {a, b};
110+
}
111+
}
112+
return {};
80113
}
81-
}s
114+
};
115+
```
116+
117+
### **Go**
118+
119+
```go
120+
func findSwapValues(array1 []int, array2 []int) []int {
121+
s1, s2 := 0, 0
122+
for _, a := range array1 {
123+
s1 += a
124+
}
125+
s := make(map[int]bool)
126+
for _, b := range array2 {
127+
s2 += b
128+
s[b] = true
129+
}
130+
diff := s1 - s2
131+
if (diff & 1) == 1 {
132+
return []int{}
133+
}
134+
diff >>= 1
135+
for _, a := range array1 {
136+
b := a - diff
137+
if s[b] {
138+
return []int{a, b}
139+
}
140+
}
141+
return []int{}
142+
}
82143
```
83144

84145
### **...**

lcci/16.21.Sum Swap/README_EN.md

+74-13
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,14 @@
4242
class Solution:
4343
def findSwapValues(self, array1: List[int], array2: List[int]) -> List[int]:
4444
diff = sum(array1) - sum(array2)
45-
if diff & 1: return []
45+
if diff & 1:
46+
return []
4647
diff >>= 1
4748
s = set(array2)
48-
for e in array1:
49-
if (e - diff) in s: return [e, e - diff]
49+
for a in array1:
50+
b = a - diff
51+
if b in s:
52+
return [a, b]
5053
return []
5154
```
5255

@@ -55,27 +58,85 @@ class Solution:
5558
```java
5659
class Solution {
5760
public int[] findSwapValues(int[] array1, int[] array2) {
58-
int diff = sum(array1) - sum(array2);
61+
int s1 = 0, s2 = 0;
62+
Set<Integer> s = new HashSet<>();
63+
for (int a : array1) {
64+
s1 += a;
65+
}
66+
for (int b : array2) {
67+
s.add(b);
68+
s2 += b;
69+
}
70+
int diff = s1 - s2;
5971
if ((diff & 1) == 1) {
6072
return new int[]{};
6173
}
6274
diff >>= 1;
63-
Set<Integer> s = Arrays.stream(array2).boxed().collect(Collectors.toSet());
64-
for (int e : array1) {
65-
if (s.contains((e - diff))) {
66-
return new int[]{e, e - diff};
75+
for (int a : array1) {
76+
int b = a - diff;
77+
if (s.contains(b)) {
78+
return new int[]{a, b};
6779
}
6880
}
6981
return new int[]{};
7082
}
83+
}
84+
```
85+
86+
### **C++**
7187

72-
private int sum(int[] array) {
73-
int res = 0;
74-
for (int e : array) {
75-
res += e;
88+
```cpp
89+
class Solution {
90+
public:
91+
vector<int> findSwapValues(vector<int>& array1, vector<int>& array2) {
92+
int s1 = 0, s2 = 0;
93+
unordered_set<int> s;
94+
for (int a : array1) s1 += a;
95+
for (int b : array2) {
96+
s2 += b;
97+
s.insert(b);
7698
}
77-
return res;
99+
int diff = s1 - s2;
100+
if (diff & 1) {
101+
return {};
102+
}
103+
diff >>= 1;
104+
for (int a : array1) {
105+
int b = a - diff;
106+
if (s.count(b)) {
107+
return {a, b};
108+
}
109+
}
110+
return {};
78111
}
112+
};
113+
```
114+
115+
### **Go**
116+
117+
```go
118+
func findSwapValues(array1 []int, array2 []int) []int {
119+
s1, s2 := 0, 0
120+
for _, a := range array1 {
121+
s1 += a
122+
}
123+
s := make(map[int]bool)
124+
for _, b := range array2 {
125+
s2 += b
126+
s[b] = true
127+
}
128+
diff := s1 - s2
129+
if (diff & 1) == 1 {
130+
return []int{}
131+
}
132+
diff >>= 1
133+
for _, a := range array1 {
134+
b := a - diff
135+
if s[b] {
136+
return []int{a, b}
137+
}
138+
}
139+
return []int{}
79140
}
80141
```
81142

lcci/16.21.Sum Swap/Solution.cpp

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public:
3+
vector<int> findSwapValues(vector<int>& array1, vector<int>& array2) {
4+
int s1 = 0, s2 = 0;
5+
unordered_set<int> s;
6+
for (int a : array1) s1 += a;
7+
for (int b : array2) {
8+
s2 += b;
9+
s.insert(b);
10+
}
11+
int diff = s1 - s2;
12+
if (diff & 1) {
13+
return {};
14+
}
15+
diff >>= 1;
16+
for (int a : array1) {
17+
int b = a - diff;
18+
if (s.count(b)) {
19+
return {a, b};
20+
}
21+
}
22+
return {};
23+
}
24+
};

lcci/16.21.Sum Swap/Solution.go

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
func findSwapValues(array1 []int, array2 []int) []int {
2+
s1, s2 := 0, 0
3+
for _, a := range array1 {
4+
s1 += a
5+
}
6+
s := make(map[int]bool)
7+
for _, b := range array2 {
8+
s2 += b
9+
s[b] = true
10+
}
11+
diff := s1 - s2
12+
if (diff & 1) == 1 {
13+
return []int{}
14+
}
15+
diff >>= 1
16+
for _, a := range array1 {
17+
b := a - diff
18+
if s[b] {
19+
return []int{a, b}
20+
}
21+
}
22+
return []int{}
23+
}

lcci/16.21.Sum Swap/Solution.java

+14-13
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
11
class Solution {
22
public int[] findSwapValues(int[] array1, int[] array2) {
3-
int diff = sum(array1) - sum(array2);
3+
int s1 = 0, s2 = 0;
4+
Set<Integer> s = new HashSet<>();
5+
for (int a : array1) {
6+
s1 += a;
7+
}
8+
for (int b : array2) {
9+
s.add(b);
10+
s2 += b;
11+
}
12+
int diff = s1 - s2;
413
if ((diff & 1) == 1) {
514
return new int[]{};
615
}
716
diff >>= 1;
8-
Set<Integer> s = Arrays.stream(array2).boxed().collect(Collectors.toSet());
9-
for (int e : array1) {
10-
if (s.contains((e - diff))) {
11-
return new int[]{e, e - diff};
17+
for (int a : array1) {
18+
int b = a - diff;
19+
if (s.contains(b)) {
20+
return new int[]{a, b};
1221
}
1322
}
1423
return new int[]{};
1524
}
16-
17-
private int sum(int[] array) {
18-
int res = 0;
19-
for (int e : array) {
20-
res += e;
21-
}
22-
return res;
23-
}
2425
}

lcci/16.21.Sum Swap/Solution.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
class Solution:
22
def findSwapValues(self, array1: List[int], array2: List[int]) -> List[int]:
33
diff = sum(array1) - sum(array2)
4-
if diff & 1: return []
4+
if diff & 1:
5+
return []
56
diff >>= 1
67
s = set(array2)
7-
for e in array1:
8-
if (e - diff) in s: return [e, e - diff]
8+
for a in array1:
9+
b = a - diff
10+
if b in s:
11+
return [a, b]
912
return []

0 commit comments

Comments
 (0)