Skip to content

Commit fff2695

Browse files
committed
feat: add python and java solutions to lcci problem: No.16.21
添加《程序员面试金典》题解:面试题 16.21. 交换和
1 parent a3f28ba commit fff2695

File tree

4 files changed

+100
-5
lines changed

4 files changed

+100
-5
lines changed

Diff for: lcci/16.21.Sum Swap/README.md

+34-3
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,51 @@
2828

2929
## 解法
3030
<!-- 这里可写通用的实现逻辑 -->
31-
31+
先计算两个数组的差值 `diff`,若 `diff` 为奇数,则说明无满足条件的数值,返回空数组。否则,将 `array2` 转为 `set`。然后遍历 `array1` 中的每个数 `e`,若值 `e - diff``set` 中,则说明找到满足条件的数值对。
3232

3333
### Python3
3434
<!-- 这里可写当前语言的特殊实现逻辑 -->
3535

3636
```python
37-
37+
class Solution:
38+
def findSwapValues(self, array1: List[int], array2: List[int]) -> List[int]:
39+
diff = sum(array1) - sum(array2)
40+
if diff & 1: return []
41+
diff >>= 1
42+
s = set(array2)
43+
for e in array1:
44+
if (e - diff) in s: return [e, e - diff]
45+
return []
3846
```
3947

4048
### Java
4149
<!-- 这里可写当前语言的特殊实现逻辑 -->
4250

4351
```java
44-
52+
class Solution {
53+
public int[] findSwapValues(int[] array1, int[] array2) {
54+
int diff = sum(array1) - sum(array2);
55+
if ((diff & 1) == 1) {
56+
return new int[]{};
57+
}
58+
diff >>= 1;
59+
Set<Integer> s = Arrays.stream(array2).boxed().collect(Collectors.toSet());
60+
for (int e : array1) {
61+
if (s.contains((e - diff))) {
62+
return new int[]{e, e - diff};
63+
}
64+
}
65+
return new int[]{};
66+
}
67+
68+
private int sum(int[] array) {
69+
int res = 0;
70+
for (int e : array) {
71+
res += e;
72+
}
73+
return res;
74+
}
75+
}s
4576
```
4677

4778
### ...

Diff for: lcci/16.21.Sum Swap/README_EN.md

+33-2
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,44 @@
5454
### Python3
5555

5656
```python
57-
57+
class Solution:
58+
def findSwapValues(self, array1: List[int], array2: List[int]) -> List[int]:
59+
diff = sum(array1) - sum(array2)
60+
if diff & 1: return []
61+
diff >>= 1
62+
s = set(array2)
63+
for e in array1:
64+
if (e - diff) in s: return [e, e - diff]
65+
return []
5866
```
5967

6068
### Java
6169

6270
```java
63-
71+
class Solution {
72+
public int[] findSwapValues(int[] array1, int[] array2) {
73+
int diff = sum(array1) - sum(array2);
74+
if ((diff & 1) == 1) {
75+
return new int[]{};
76+
}
77+
diff >>= 1;
78+
Set<Integer> s = Arrays.stream(array2).boxed().collect(Collectors.toSet());
79+
for (int e : array1) {
80+
if (s.contains((e - diff))) {
81+
return new int[]{e, e - diff};
82+
}
83+
}
84+
return new int[]{};
85+
}
86+
87+
private int sum(int[] array) {
88+
int res = 0;
89+
for (int e : array) {
90+
res += e;
91+
}
92+
return res;
93+
}
94+
}
6495
```
6596

6697
### ...

Diff for: lcci/16.21.Sum Swap/Solution.java

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public int[] findSwapValues(int[] array1, int[] array2) {
3+
int diff = sum(array1) - sum(array2);
4+
if ((diff & 1) == 1) {
5+
return new int[]{};
6+
}
7+
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};
12+
}
13+
}
14+
return new int[]{};
15+
}
16+
17+
private int sum(int[] array) {
18+
int res = 0;
19+
for (int e : array) {
20+
res += e;
21+
}
22+
return res;
23+
}
24+
}

Diff for: lcci/16.21.Sum Swap/Solution.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution:
2+
def findSwapValues(self, array1: List[int], array2: List[int]) -> List[int]:
3+
diff = sum(array1) - sum(array2)
4+
if diff & 1: return []
5+
diff >>= 1
6+
s = set(array2)
7+
for e in array1:
8+
if (e - diff) in s: return [e, e - diff]
9+
return []

0 commit comments

Comments
 (0)