auto commit
This commit is contained in:
parent
62f8d7cd4a
commit
8f2b92d915
@ -1,7 +1,6 @@
|
||||
<div align="center">
|
||||
<a href="https://github.com/CyC2018/CS-Notes/blob/master/other/download.md">离线阅读版本下载</a>
|
||||
<a href="https://github.com/CyC2018/CS-Notes/blob/master/other/download.md">离线阅读版本下载(Download for offline reading)</a>
|
||||
</div>
|
||||
|
||||
<br>
|
||||
|
||||
| 算法 | 操作系统 | 网络 | 面向对象 | 数据库 | Java | 系统设计 | 工具 | 编码实践 | 后记 |
|
||||
|
@ -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) {
|
||||
|
@ -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) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user