LeetCode18--4Sum--数组中某四个元素之和为某个输入的数值,输出这四个元素的值,并且这个四元组唯一
来源:互联网 发布:mysql服务占多大内存 编辑:程序博客网 时间:2024/06/07 00:20
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]]
public class Solution { public List<List<Integer>> fourSum(int[] nums, int target) { Arrays.sort(nums); List<List<Integer>> res = new LinkedList<>();//保证输出的顺序就是插入的顺序 if(nums == null || nums.length <= 0){ return res; } int max = nums[nums.length - 1]; for (int i = 0; i < nums.length - 3; i++) {//比如:{-4, -1, -1, 0, 1, 5 ,5},最后一次比较的是0,1,5,5 if (4 * nums[0] > target || 4 * max < target) { break; } if (nums[0] + 3 * nums[1] > target || max + 3 * nums[nums.length - 2] < target) { break; } if (4 * nums[i] > target) { break; } if (nums[i] + 3 * max < target) { continue; } if(nums[i]*4 == target){ if(i + 3 < nums.length && nums[i+3] == nums[i]){ res.add(Arrays.asList(nums[i],nums[i],nums[i],nums[i])); } continue; } List<Integer> list; for (int j = i + 1; j < nums.length - 2; j++) { if (j == i + 1 || (j > i + 1 && nums[j] != nums[j - 1])) {//比如-1只能做基准1次,后面的-1直接跳过 int low = j + 1; int high = nums.length - 1;//最后一次num[i] = 0,num[j] = 1,num[lo] = 5,num[hi] = 5 while (low < high) { if (nums[i] + nums[j] + nums[low] + nums[high] == target) { list = Arrays.asList(nums[i], nums[j], nums[low], nums[high]); if (!res.contains(list)) {//去除重复的结果 res.add(list); } while (low < high && nums[low] == nums[low + 1]) low++;//比如:{-4, -1, -1, 0, 1, 5 ,5},-1只能计算一次,不然会有重复计算 while (low < high && nums[high] == nums[high - 1]) high--;//比如:{-4, -1, -1, 0, 1, 5, 5},5只能计算一次,不然会有重复计算 low++; high--; } else if (nums[i] + nums[j] + nums[low] + nums[high] < target) { low++; } else high--; }//while }//if }//for }//for return res; }//fourSum}
282 / 282 test cases passed.
Status: AcceptedRuntime: 36 ms
Your runtime beats 76.98 % of java submissions.
T(n) = O(n^3)
方法二:
public class Solution { public List<List<Integer>> fourSum(int[] num, int target) { ArrayList<List<Integer>> ans = new ArrayList<>(); if(num.length<4)return ans; Arrays.sort(num); for(int i=0; i<num.length-3; i++){ if(num[i]+num[i+1]+num[i+2]+num[i+3]>target)break; //first candidate too large, search finished if(num[i]+num[num.length-1]+num[num.length-2]+num[num.length-3]<target)continue; //first candidate too small if(i>0&&num[i]==num[i-1])continue; //prevents duplicate result in ans list for(int j=i+1; j<num.length-2; j++){ if(num[i]+num[j]+num[j+1]+num[j+2]>target)break; //second candidate too large if(num[i]+num[j]+num[num.length-1]+num[num.length-2]<target)continue; //second candidate too small if(j>i+1&&num[j]==num[j-1])continue; //prevents duplicate results in ans list,num[i]不能和num[j]去比较是否相等 int low=j+1, high=num.length-1; while(low<high){ int sum=num[i]+num[j]+num[low]+num[high]; if(sum==target){ ans.add(Arrays.asList(num[i], num[j], num[low], num[high])); while(low<high&&num[low]==num[low+1])low++; //skipping over duplicate on low while(low<high&&num[high]==num[high-1])high--; //skipping over duplicate on high low++; high--; } //move window else if(sum<target)low++; else high--; } } } return ans; }}282 / 282 test cases passed.
Status: Accepted
Runtime: 24 ms
Your runtime beats 97.98 % of java submissions.
阅读全文
0 0
- LeetCode18--4Sum--数组中某四个元素之和为某个输入的数值,输出这四个元素的值,并且这个四元组唯一
- LeetCode 15. 3Sum -- 数组中某三个元素之和为0,输出这三个元素的值,且这个三元组唯一
- LeetCode 454. 4Sum II--在四个等长的数组中,找到有多少个四元组的数值之和为0(每个数组中各占一个元素)
- LeetCode 1. Two Sum--数组中两元素相加为该数值,输出对应的两个索引
- 用c++建立一个类SUM,求二维数组外围各元素的和,并且输出数组各元素及所求之和。
- c++ 建立一个类SUM,求二维数组外围各元素的和,并且输出数组各元素及所求之和
- 建立一个类SUM,求二维数组外围各元素的和,并且输出数组各元素及所求之和。具体要求如下:
- C语言 输出一个数组中,所有元素之和为0的子序列
- margin四个元素的顺序
- 第1题 在排序数组中查找和为某个指定数的两个元素之和
- 编写一个程序,创建两个8元素的double数组,第一个数组有用户键入8个值,第二个为第一个的累积,例如,第二个数组的第四个元素应该等于第一个数组的前四个元素之和
- 找出数组中和为指定值的四个数 4sum
- m×m的二维数组中,最大元素放在中心,四个角放最小的元素
- 找出升序数组中元素之和最接近于给定数值的两个数
- 【题目40】求整形数组中满足两数之和为sum的数值对
- 找出数组中唯一的重复元素
- 求数组中唯一重复的元素
- 找出数组中唯一的重复元素
- 进程间通信——信号量
- iOS Swift print了解和不换行
- TCP流量控制与拥塞控制浅析
- JQuery实现 任务清单案例
- PAT基础题 4-1 简单输出整数
- LeetCode18--4Sum--数组中某四个元素之和为某个输入的数值,输出这四个元素的值,并且这个四元组唯一
- mfc中设置statictext的字体和颜色
- linux-磁盘的管理
- try-with-resource小demo遇到的一些小问题
- 进程间通信——信号
- 预解析与函数变量声明提升
- HTML5 服务器发送事件 Server-Send
- FTP SFTP TFTP的区别
- 2017.8.1拼多多内推内推笔试题(3)—六一儿童节(贪心问题)