Skip to content

Commit 32c64fb

Browse files
committed
feat: add python and java solutions to leetcode problem: No.0259
1 parent 63a98d4 commit 32c64fb

File tree

6 files changed

+127
-4
lines changed

6 files changed

+127
-4
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
- [两数之和](/solution/0000-0099/0001.Two%20Sum/README.md)
5959
- [三数之和](/solution/0000-0099/0015.3Sum/README.md)
6060
- [四数之和](/solution/0000-0099/0018.4Sum/README.md)
61+
- [较小的三数之和](/solution/0200-0299/0259.3Sum%20Smaller/README.md)
6162
- [合并两个有序数组](/solution/0000-0099/0088.Merge%20Sorted%20Array/README.md)
6263
- [寻找旋转排序数组中的最小值](/solution/0100-0199/0153.Find%20Minimum%20in%20Rotated%20Sorted%20Array/README.md)
6364
- [寻找旋转排序数组中的最小值 II](/solution/0100-0199/0154.Find%20Minimum%20in%20Rotated%20Sorted%20Array%20II/README.md)

README_EN.md

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ Complete solutions to [LeetCode](https://leetcode-cn.com/problemset/all/), [LCOF
5757
- [Two Sum](/solution/0000-0099/0001.Two%20Sum/README_EN.md)
5858
- [3Sum](/solution/0000-0099/0015.3Sum/README_EN.md)
5959
- [4Sum](/solution/0000-0099/0018.4Sum/README_EN.md)
60+
- [3Sum Smaller](/solution/0200-0299/0259.3Sum%20Smaller/README_EN.md)
6061
- [Merge Sorted Array](/solution/0000-0099/0088.Merge%20Sorted%20Array/README_EN.md)
6162
- [Find Minimum in Rotated Sorted Array](/solution/0100-0199/0153.Find%20Minimum%20in%20Rotated%20Sorted%20Array/README_EN.md)
6263
- [Find Minimum in Rotated Sorted Array II](/solution/0100-0199/0154.Find%20Minimum%20in%20Rotated%20Sorted%20Array%20II/README_EN.md)

solution/0200-0299/0259.3Sum Smaller/README.md

+43-2
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,63 @@
2222

2323
<!-- 这里可写通用的实现逻辑 -->
2424

25+
双指针解决。
26+
2527
<!-- tabs:start -->
2628

2729
### **Python3**
2830

2931
<!-- 这里可写当前语言的特殊实现逻辑 -->
3032

3133
```python
32-
34+
class Solution:
35+
def threeSumSmaller(self, nums: List[int], target: int) -> int:
36+
def threeSumSmaller(nums, start, end, target):
37+
count = 0
38+
while start < end:
39+
if nums[start] + nums[end] < target:
40+
count += (end - start)
41+
start += 1
42+
else:
43+
end -= 1
44+
return count
45+
46+
nums.sort()
47+
n, count = len(nums), 0
48+
for i in range(n - 2):
49+
count += threeSumSmaller(nums, i + 1, n - 1, target - nums[i])
50+
return count
3351
```
3452

3553
### **Java**
3654

3755
<!-- 这里可写当前语言的特殊实现逻辑 -->
3856

3957
```java
40-
58+
class Solution {
59+
public int threeSumSmaller(int[] nums, int target) {
60+
Arrays.sort(nums);
61+
int n = nums.length;
62+
int count = 0;
63+
for (int i = 0; i < n - 2; ++i) {
64+
count += threeSumSmaller(nums, i + 1, n - 1, target - nums[i]);
65+
}
66+
return count;
67+
}
68+
69+
private int threeSumSmaller(int[] nums, int start, int end, int target) {
70+
int count = 0;
71+
while (start < end) {
72+
if (nums[start] + nums[end] < target) {
73+
count += (end - start);
74+
++start;
75+
} else {
76+
--end;
77+
}
78+
}
79+
return count;
80+
}
81+
}
4182
```
4283

4384
### **...**

solution/0200-0299/0259.3Sum Smaller/README_EN.md

+41-2
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,52 @@
2525
### **Python3**
2626

2727
```python
28-
28+
class Solution:
29+
def threeSumSmaller(self, nums: List[int], target: int) -> int:
30+
def threeSumSmaller(nums, start, end, target):
31+
count = 0
32+
while start < end:
33+
if nums[start] + nums[end] < target:
34+
count += (end - start)
35+
start += 1
36+
else:
37+
end -= 1
38+
return count
39+
40+
nums.sort()
41+
n, count = len(nums), 0
42+
for i in range(n - 2):
43+
count += threeSumSmaller(nums, i + 1, n - 1, target - nums[i])
44+
return count
2945
```
3046

3147
### **Java**
3248

3349
```java
34-
50+
class Solution {
51+
public int threeSumSmaller(int[] nums, int target) {
52+
Arrays.sort(nums);
53+
int n = nums.length;
54+
int count = 0;
55+
for (int i = 0; i < n - 2; ++i) {
56+
count += threeSumSmaller(nums, i + 1, n - 1, target - nums[i]);
57+
}
58+
return count;
59+
}
60+
61+
private int threeSumSmaller(int[] nums, int start, int end, int target) {
62+
int count = 0;
63+
while (start < end) {
64+
if (nums[start] + nums[end] < target) {
65+
count += (end - start);
66+
++start;
67+
} else {
68+
--end;
69+
}
70+
}
71+
return count;
72+
}
73+
}
3574
```
3675

3776
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public int threeSumSmaller(int[] nums, int target) {
3+
Arrays.sort(nums);
4+
int n = nums.length;
5+
int count = 0;
6+
for (int i = 0; i < n - 2; ++i) {
7+
count += threeSumSmaller(nums, i + 1, n - 1, target - nums[i]);
8+
}
9+
return count;
10+
}
11+
12+
private int threeSumSmaller(int[] nums, int start, int end, int target) {
13+
int count = 0;
14+
while (start < end) {
15+
if (nums[start] + nums[end] < target) {
16+
count += (end - start);
17+
++start;
18+
} else {
19+
--end;
20+
}
21+
}
22+
return count;
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution:
2+
def threeSumSmaller(self, nums: List[int], target: int) -> int:
3+
def threeSumSmaller(nums, start, end, target):
4+
count = 0
5+
while start < end:
6+
if nums[start] + nums[end] < target:
7+
count += (end - start)
8+
start += 1
9+
else:
10+
end -= 1
11+
return count
12+
13+
nums.sort()
14+
n, count = len(nums), 0
15+
for i in range(n - 2):
16+
count += threeSumSmaller(nums, i + 1, n - 1, target - nums[i])
17+
return count

0 commit comments

Comments
 (0)