4Sum、3Num
来源:互联网 发布:海贼王 知乎 编辑:程序博客网 时间:2024/06/15 21:07
15
Given an array S of n integers, are there elements a, b, c in S 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]]
18.4 Num
Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.
Note: The solution set must not contain duplicate quadruplets.
For example, given array S = [1, 0, -1, 0, -2, 2], and target = 0.A solution set is:[ [-1, 0, 0, 1], [-2, -1, 1, 2], [-2, 0, 0, 2]]
</pre>思路:三个数的时候 先定第一个数i,第二个数j=i+1,第三个数k=length-1 如果三个数之和小于target j++ 如果三个数之和大于target k--,需要考虑的是重复的问题,如果得到三个数之和等于target时,需要考虑 j和j++ 位置的值是否相等,相等则继续加1,k和k-1位置的值是否相等相等则继续减1,不等于target时不用检查因为不符合条件所以不会被加入到list中。不重复的时候再进行一次j++ 和k-- (此时的j和k才是和上一次不同的值) 四个数时多一层循环,注意去掉重复值即可<pre name="code" class="java">
public class Solution { public List<List<Integer>> threeSum(int[] nums) { // List<Integer> lii=new ArrayList<>(); List<List<Integer>> lio =new ArrayList<>(); Arrays.sort(nums); int i=0,j=1,k=nums.length-1; for(i=0;i<nums.length-2;i++){ j=i+1; k=nums.length-1; while(j<k){ if(nums[i]+nums[j]+nums[k]<0) { j++; continue; } else if(nums[i]+nums[j]+nums[k]>0) { k--; continue; } else if(nums[i]+nums[j]+nums[k]==0) { lio.add(Arrays.asList(nums[i], nums[j], nums[k])); // lii.clear(); while(j<k&&(nums[k]==nums[k-1])) k--; while(j<k&&(nums[j]==nums[j+1])) j++; k--; j++; while((i<nums.length-2)&&(nums[i]==nums[i+1])) i++; } } } return lio; }}
public class Solution { public List<List<Integer>> fourSum(int[] nums, int target) { Arrays.sort(nums); List<List<Integer>> list=new ArrayList<List<Integer>>(); if(nums.length<4) return list; for(int i=0;i<nums.length-3;i++){ for(int j=i+1;j<nums.length-1;j++){ int k=j+1,l=nums.length-1; while(k<l){ if(nums[i]+nums[j]+nums[k]+nums[l]<target){ k++; continue; } else if (nums[i]+nums[j]+nums[k]+nums[l]>target){ l--; continue; } else { list.add(Arrays.asList(nums[i],nums[j],nums[k],nums[l])); while(k<l&&(nums[l]==nums[l-1])) l--; while(k<l&&(nums[k]==nums[k+1])) k++; l--; k++; // j++; } } while(j<nums.length-2&&(nums[j]==nums[j+1])) j++; } while(i<nums.length-3&&nums[i]==nums[i+1]) i++; } return list; }}
确定第一个和第四个 对第二个和第三个进行遍历判断更快
</pre><p><pre name="code" class="java">public class Solution { public List<List<Integer>> fourSum(int[] nums, int target) { List<List<Integer>> list =new ArrayList<List<Integer>>(); Arrays.sort(nums); for(int i=0,L=nums.length;i<L-3;i++){ if(nums[i]<<2 >target) return list; for(int j=L-1;j>i+2;j--){ if(nums[j]<<2 < target) break; int rem =target-nums[i]-nums[j]; int lo=i+1,high=j-1; while(lo<high){ int sum=nums[lo]+nums[high]; if(sum<rem) lo++; else if (sum>rem) high--; else{ list.add(Arrays.asList(nums[i],nums[j],nums[lo],nums[high])); while(lo<high&&(nums[lo]==nums[lo+1])) lo++; while(lo<high&&(nums[high]==nums[high-1])) high--; lo++; high--; } } while(j>=1&&(nums[j]==nums[j-1])) j--; } while(i<L-1 && (nums[i]==nums[i+1])) i++; } return list; } }
0 0
- 4Sum、3Num
- ASP 求和(select sum(num))
- Num.4 分治算法
- Num.3 递归算法
- 3Sum 3Sum Closest 4Sum
- 3Sum & 3Sum Closest & 4Sum
- 3sum、3Sum closet、 4sum
- 3Sum, 3Sum Closest, 4 Sum
- leetcode 2 sum 3sum 4sum
- 2Sum 3Sum 4Sum
- Leetcode 2SUM-3SUM-4SUM
- Leetcode-2sum,3sum,4sum
- leetcode 2 sum & 3 sum & 4 sum
- [LeetCode] 2Sum, 3Sum, 4Sum, 3SUm closet
- Two Sum & 3Sum & 3Sum Closest & 4Sum
- Two Sum,3Sum,3Sum Closest,4Sum
- [LeetCode] K sum(2Sum、3Sum、4Sum)
- 2sum 3sum 4sum 各种sum
- Longest Consecutive Sequence
- Ubuntu16.4系统下为Python配置caffe环境
- c语言 可变参数 三个点 ...
- Linux互斥锁和条件变量
- AC自动机模板
- 4Sum、3Num
- Textview设置不同样式
- Win32字符串编码格式转化
- Zendstuido汉化方法
- 如何安装和配置Mantis
- 杭电5620
- Java跨域请求操作---封装类
- 【SQL】 DROP DELATE区别
- 线段树专题#1_蒟蒻训练历程记录_HDU1166