三数之和(排序+双指针)
2021-04-19 18:27
标签:list 双指针 return loading continue 循环 for lse mamicode 代码源于: https://leetcode-cn.com/problems/3sum/solution/hua-jie-suan-fa-15-san-shu-zhi-he-by-guanpengchn/ 三数之和(排序+双指针) 标签:list 双指针 return loading continue 循环 for lse mamicode 原文地址:https://www.cnblogs.com/cstdio1/p/13288557.html题解
nums[i]为最左端的数,nums[L]为中间,nums[R]为最右端数,
最外层循环遍历最左端,nums[L]最初指向nuns[i+1],nums[R]最初指向nums[nums.length-1],根据sum变化不断移动
代码
class Solution {
public static List
> threeSum(int[] nums) {
List
> ans = new ArrayList();
int len = nums.length;
if(nums == null || len 0) break; // 如果当前数字大于0,则三数之和一定大于0,所以结束循环
if(i > 0 && nums[i] == nums[i-1]) continue; // 去重
int L = i+1;
int R = len-1;
while(L 0) R--;
}
}
return ans;
}
}