Skip to content

Commit 1ba26a0

Browse files
author
liwentian
committed
fd
1 parent 7fc54f9 commit 1ba26a0

File tree

3 files changed

+36
-4
lines changed

3 files changed

+36
-4
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@
198198
|255|[Verify Preorder Sequence in Binary Search Tree](https://leetcode.com/problems/verify-preorder-sequence-in-binary-search-tree/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/VerifyPreorderSequenceInBinarySearchTree.java)|85|很简单,粗心错了|
199199
|256|[Paint House](https://leetcode.com/problems/paint-house/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/PaintHouse.java)|95|
200200
|257|[Binary Tree Paths](https://leetcode.com/problems/binary-tree-paths/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/BinaryTreePaths.java)|80|
201-
|259|[3Sum Smaller](https://leetcode.com/problems/3sum-smaller/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/ThreeSumSmaller.java)|95|
201+
|259|[3Sum Smaller](https://leetcode.com/problems/3sum-smaller/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/ThreeSumSmaller.java)|95|这题虽然简单,但是因为思维定式还是错了几次|
202202
|263|[Ugly Number](https://leetcode.com/problems/ugly-number/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/UglyNumber.java)|85|
203203
|264|[Ugly Number II](https://leetcode.com/problems/ugly-number-ii/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/UglyNumberII.java)|70|
204204
|265|[Paint House II](https://leetcode.com/problems/paint-house-ii)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/PaintHouseII.java)|60|
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.leetcode.google;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* Created by liwentian on 2017/8/30.
7+
*/
8+
9+
public class ThreeSumSmaller {
10+
// -4 -1 -1 0 1 2 ,target=-5
11+
public int threeSumSmaller(int[] nums, int target) {
12+
Arrays.sort(nums);
13+
14+
int count = 0;
15+
for (int i = 0; i < nums.length - 2; i++) {
16+
for (int j = i + 1, k = nums.length - 1; j < k; ) {
17+
if (nums[i] + nums[j] + nums[k] < target) {
18+
count += k - j;
19+
j++;
20+
} else {
21+
k--;
22+
}
23+
}
24+
}
25+
26+
return count;
27+
}
28+
}

solution/src/main/java/com/inuker/solution/ThreeSumSmaller.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,14 @@
1111
*/
1212
public class ThreeSumSmaller {
1313

14+
/**
15+
* 注意这里别画蛇添足的加上
16+
* if (nums[i] > target) {
17+
* break;
18+
* }
19+
* 虽然后面的数大于等于nums[i],但是有可能是负数,三个数之和还是可能小于target的
20+
*/
1421
public int threeSumSmaller(int[] nums, int target) {
15-
if (nums.length < 3) {
16-
return 0;
17-
}
1822
Arrays.sort(nums);
1923

2024
int count = 0;

0 commit comments

Comments
 (0)