Leetcode每日一题_15
public&nbsp;static&nbsp;List<List<Integer>>&nbsp;threeSum(int[]&nbsp;nums)&nbsp;{
//o(n*n)&nbsp;o(n)
&nbsp;&nbsp;&nbsp;&nbsp;List<List<Integer>>&nbsp;res=new&nbsp;ArrayList<>();
&nbsp;&nbsp;&nbsp;&nbsp;Arrays.sort(nums);
&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(int&nbsp;i&nbsp;=&nbsp;0;&nbsp;i&nbsp;<&nbsp;nums.length-2;&nbsp;i++)&nbsp;{
if(i>0&&nums[i]==nums[i-1])&nbsp;continue;
int&nbsp;low=i+1,high=nums.length-1,sum=0-nums[i];
while&nbsp;(low<high)&nbsp;{
if&nbsp;(nums[low]+nums[high]==sum)&nbsp;{
&nbsp;res.add(Arrays.asList(nums[i],nums[low],nums[high]));
while(low<high&&nums[low]==nums[low+1])&nbsp;low++;
while(low<high&&nums[high]==nums[high-1])&nbsp;high--;
low++;
high--;
}
else&nbsp;if&nbsp;(nums[low]+nums[high]<sum)&nbsp;{
low++;
}
else&nbsp;{
high--;
}
}
}
&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;res;
}
全部评论

相关推荐

点赞 评论 收藏
分享
头像 会员标识
今天 16:40
浙江大学 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务