Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f186090

Browse files
author
scuyjzh
committed
modify solutions to problem - "15. 3Sum"
1 parent 04a931c commit f186090

File tree

2 files changed

+9
-10
lines changed

2 files changed

+9
-10
lines changed

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@
219219
| ---- | ------------------------------------------------------------ | ------------------------------------------------------------ | ---- | -------------------- |
220220
| 11 | [盛最多水的容器](https://leetcode-cn.com/problems/container-with-most-water/) | [Java](./src/com/scuyjzh/leetcode/medium/No_0011_Container_With_Most_Water/Solution.java) | 中等 | 贪心 数组 双指针 |
221221
| 167 | [两数之和 II - 输入有序数组](https://leetcode-cn.com/problems/two-sum-ii-input-array-is-sorted/) | [Java](./src/com/scuyjzh/leetcode/medium/No_0167_Two_Sum_II/Solution.java) | 中等 | 数组 双指针 二分查找 |
222+
| 15 | [三数之和](https://leetcode-cn.com/problems/3sum/) | [Java](./src/com/scuyjzh/leetcode/medium/No_0015_3Sum/Solution.java) | 中等 | 数组 双指针 排序 |
222223

223224
### Array
224225

‎src/com/scuyjzh/leetcode/medium/No_0015_3Sum/Solution.java‎

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,12 @@
77
*
88
* 给你一个包含 n 个整数的数组nums,判断nums中是否存在三个元素
99
* a,b,c ,使得a + b + c = 0 ?请你找出所有和为 0 且不重复的三元组。
10+
*
1011
* 注意:答案中不可以包含重复的三元组。
1112
*/
1213
class Solution {
1314
/**
1415
* 方法:排序 + 双指针
15-
*
16-
* • 时间复杂度:O(N^2),其中 N 是数组 nums 的长度。
17-
* • 空间复杂度:O(logN)。忽略存储答案的空间,额外的排序的空间复杂度为 O(logN)。
1816
*/
1917
public List<List<Integer>> threeSum(int[] nums) {
2018
List<List<Integer>> res = new LinkedList<>();
@@ -24,7 +22,7 @@ public List<List<Integer>> threeSum(int[] nums) {
2422
// 排序
2523
Arrays.sort(nums);
2624
for (int i = 0; i < nums.length - 2; ++i) {
27-
// 如果当前数字大于0,则三数之和一定大于0,所以结束循环
25+
// 如果当前数字大于 0,则三数之和一定大于 0,所以结束循环
2826
if (nums[i] > 0) {
2927
break;
3028
}
@@ -37,19 +35,19 @@ public List<List<Integer>> threeSum(int[] nums) {
3735
int sum = nums[i] + nums[left] + nums[right];
3836
if (0 == sum) {
3937
res.add(Arrays.asList(nums[i], nums[left], nums[right]));
40-
left++;
41-
right--;
38+
++left;
39+
--right;
4240
// 去除重复解
4341
while (left < right && nums[left] == nums[left - 1]) {
44-
left++;
42+
++left;
4543
}
4644
while (left < right && nums[right] == nums[right + 1]) {
47-
right--;
45+
--right;
4846
}
4947
} else if (sum < 0) {
50-
left++;
48+
++left;
5149
} else {
52-
right--;
50+
--right;
5351
}
5452
}
5553
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /