15. 3Sum
来源:互联网 发布:爱温士采暖知乎 编辑:程序博客网 时间:2024/06/06 09:40
Given an array S of n integers, are there elements a,b,c inS such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.
Note: The solution set must not contain duplicate triplets.
For example, given array S = [-1, 0, 1, 2, -1, -4],A solution set is:[ [-1, 0, 1], [-1, -1, 2]]public class Solution { public List<List<Integer>> threeSum(int[] nums) { List<List<Integer>> answerList=new ArrayList<List<Integer>>(); HashSet<List<Integer>> anSet=new HashSet<>(); ArrayList<Integer> origin=new ArrayList<>(); if(nums.length<3)return answerList; for(int i=0;i<nums.length;i++) { origin.add(nums[i]); } Collections.sort(origin); for(int i=0;i<origin.size()-2;i++) { if(i==0||origin.get(i)!=origin.get(i-1)) { int low=i+1; int high=origin.size()-1; int sum=0-origin.get(i); while(low<high) { if(origin.get(low)+origin.get(high)==sum) { ArrayList<Integer> result=new ArrayList<>(); result.add(origin.get(i)); result.add(origin.get(low)); result.add(origin.get(high)); anSet.add(result); low++; high--; while(low<high&&origin.get(low)==origin.get(low-1))low++; while(low<high&&origin.get(high)==origin.get(high+1))high--; if(low==high) { if(low>0&&origin.get(low)+origin.get(low-1)==sum) { result=new ArrayList<>(); result.add(origin.get(i)); result.add(origin.get(low-1)); result.add(origin.get(low)); anSet.add(result); low++; high--; } if(high<origin.size()-1&&origin.get(high)+origin.get(high+1)==sum) { result=new ArrayList<>(); result.add(origin.get(i)); result.add(origin.get(high)); result.add(origin.get(high+1)); anSet.add(result); low++; high--; } } } else if(origin.get(low)+origin.get(high)>sum) { high--; } else { low++; } } } } answerList.addAll(anSet); return answerList; } }4SUM代码如下:class Solution { public List<List<Integer>> fourSum(int[] nums, int target) { List<List<Integer>> result=new ArrayList<>(); Arrays.sort(nums); for(int i=0;i<nums.length-3;) { for(int j=i+1;j<nums.length-2;) { int lo=j+1; int hi=nums.length-1; while(lo<hi) { int sum=nums[i]+nums[j]+nums[lo]+nums[hi]; if(sum==target) { List<Integer> array=new ArrayList<>(); array.add(nums[i]); array.add(nums[j]); array.add(nums[lo]); array.add(nums[hi]); result.add(array); hi--; while(nums[hi]==nums[hi+1]) { if(lo<hi)hi--; else break; } lo++; while(nums[lo]==nums[lo-1]) { if(lo<hi)lo++; else break; } } else if(sum>target) { hi--; while(nums[hi]==nums[hi+1]) { if(lo<hi)hi--; else break; } } else { lo++; while(nums[lo]==nums[lo-1]) { if(lo<hi)lo++; else break; } } } j++; while(j<nums.length-2&&nums[j]==nums[j-1])j++; } i++; while(i<nums.length-3&&nums[i]==nums[i-1])i++; } return result; }}
阅读全文
0 0
- 15. 3Sum && 16 sum closet && 18 4sum
- 15. 3Sum 和 18. 4Sum
- 1. Two Sum&15. 3Sum
- LeetCode 15. 3Sum
- LeetCode --- 15. 3Sum
- [Leetcode] 15. 3Sum
- 【LeetCode】15. 3Sum
- 15. 3Sum
- 【leetcode】15. 3sum
- 【leetcode】15. 3Sum
- 15. 3Sum
- [leetcode] 15. 3Sum
- 15. 3Sum
- 15. 3Sum
- 15. 3Sum
- 15. 3Sum
- 15. 3Sum
- leetcode 15. 3Sum
- React Native 的顶部导航栏和底部导航栏目
- linux软链接
- 内核---内核应用层
- 欢迎使用CSDN-markdown编辑器
- 关于“相亲数”的算法实现
- 15. 3Sum
- hdu 6194 恰好出现k次的子串
- 99%的人理解错 HTTP 中 GET 与 POST 的区别
- 廖雪峰python学习笔记(1)--网络编程
- 守护线程
- Serializable 接口与 Java 序列化与反序列化
- leetcode练习 Reverse Pairs
- uva 10168 【素数筛法】
- string string string hdu 6194 (后缀数组做法)