Skip to content

Commit c9acc9c

Browse files
committed
feat: add java and python solutions to lcci question
添加《程序员面试金典题解》:面试题 16.06. 最小差
1 parent f76e41c commit c9acc9c

File tree

4 files changed

+87
-4
lines changed

4 files changed

+87
-4
lines changed

lcci/16.06.Smallest Difference/README.md

+29-2
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,41 @@
2323
<!-- 这里可写当前语言的特殊实现逻辑 -->
2424

2525
```python
26-
26+
class Solution:
27+
def smallestDifference(self, a: List[int], b: List[int]) -> int:
28+
a.sort()
29+
b.sort()
30+
i, j, res = 0, 0, 2147483647
31+
m, n = len(a), len(b)
32+
while i < m and j < n:
33+
if a[i] == b[j]: return 0
34+
res = min(res, abs(a[i] - b[j]))
35+
if a[i] > b[j]: j += 1
36+
else: i += 1
37+
return res
38+
2739
```
2840

2941
### Java
3042
<!-- 这里可写当前语言的特殊实现逻辑 -->
3143

3244
```java
33-
45+
class Solution {
46+
public int smallestDifference(int[] a, int[] b) {
47+
Arrays.sort(a);
48+
Arrays.sort(b);
49+
int m = a.length, n = b.length;
50+
int i = 0, j = 0;
51+
long res = Long.MAX_VALUE;
52+
while (i < m && j < n) {
53+
if (a[i] == b[j]) return 0;
54+
res = Math.min(res, Math.abs((long) a[i] - (long) b[j]));
55+
if (a[i] > b[j]) ++j;
56+
else ++i;
57+
}
58+
return (int) res;
59+
}
60+
}
3461
```
3562

3663
### ...

lcci/16.06.Smallest Difference/README_EN.md

+29-2
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,40 @@
3838
### Python3
3939

4040
```python
41-
41+
class Solution:
42+
def smallestDifference(self, a: List[int], b: List[int]) -> int:
43+
a.sort()
44+
b.sort()
45+
i, j, res = 0, 0, 2147483647
46+
m, n = len(a), len(b)
47+
while i < m and j < n:
48+
if a[i] == b[j]: return 0
49+
res = min(res, abs(a[i] - b[j]))
50+
if a[i] > b[j]: j += 1
51+
else: i += 1
52+
return res
53+
4254
```
4355

4456
### Java
4557

4658
```java
47-
59+
class Solution {
60+
public int smallestDifference(int[] a, int[] b) {
61+
Arrays.sort(a);
62+
Arrays.sort(b);
63+
int m = a.length, n = b.length;
64+
int i = 0, j = 0;
65+
long res = Long.MAX_VALUE;
66+
while (i < m && j < n) {
67+
if (a[i] == b[j]) return 0;
68+
res = Math.min(res, Math.abs((long) a[i] - (long) b[j]));
69+
if (a[i] > b[j]) ++j;
70+
else ++i;
71+
}
72+
return (int) res;
73+
}
74+
}
4875
```
4976

5077
### ...
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public int smallestDifference(int[] a, int[] b) {
3+
Arrays.sort(a);
4+
Arrays.sort(b);
5+
int m = a.length, n = b.length;
6+
int i = 0, j = 0;
7+
long res = Long.MAX_VALUE;
8+
while (i < m && j < n) {
9+
if (a[i] == b[j]) return 0;
10+
res = Math.min(res, Math.abs((long) a[i] - (long) b[j]));
11+
if (a[i] > b[j]) ++j;
12+
else ++i;
13+
}
14+
return (int) res;
15+
}
16+
}
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution:
2+
def smallestDifference(self, a: List[int], b: List[int]) -> int:
3+
a.sort()
4+
b.sort()
5+
i, j, res = 0, 0, 2147483647
6+
m, n = len(a), len(b)
7+
while i < m and j < n:
8+
if a[i] == b[j]: return 0
9+
res = min(res, abs(a[i] - b[j]))
10+
if a[i] > b[j]: j += 1
11+
else: i += 1
12+
return res
13+

0 commit comments

Comments
 (0)