auto commit
This commit is contained in:
@ -147,6 +147,8 @@ public class BinarySearch {
|
||||
|
||||
更有效的方法是先将数组排序,然后使用双指针进行查找,时间复杂度为 O(N<sup>2</sup>)。
|
||||
|
||||
同样不适用与数组存在重复元素的情况。
|
||||
|
||||
```java
|
||||
public class ThreeSumTwoPointer implements ThreeSum {
|
||||
|
||||
@ -157,13 +159,10 @@ public class ThreeSumTwoPointer implements ThreeSum {
|
||||
Arrays.sort(nums);
|
||||
for (int i = 0; i < N - 2; i++) {
|
||||
int l = i + 1, h = N - 1, target = -nums[i];
|
||||
if (i > 0 && nums[i] == nums[i - 1]) continue;
|
||||
while (l < h) {
|
||||
int sum = nums[l] + nums[h];
|
||||
if (sum == target) {
|
||||
cnt++;
|
||||
while (l < h && nums[l] == nums[l + 1]) l++;
|
||||
while (l < h && nums[h] == nums[h - 1]) h--;
|
||||
l++;
|
||||
h--;
|
||||
} else if (sum < target) {
|
||||
|
Reference in New Issue
Block a user