Array + two points leetcode.18 - 4Sum
题面
Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums such that a + b+ c + d = target? Find all unique quadruplets in the array which gives the sum of target.
给定无序数组,找到和为target的不重复的长度为4的子序列
样例
1. Given array nums = [1, 0, -1, 0, -2, 2], and target = 0.
solution set is:
[
[-1, 0, 0, 1],
[-2, -1, 1, 2],
[-2, 0, 0, 2]
]
2. Given array nums = [0, 0, 0, 0], and target = 0.
solution set is:
[
[0, 0, 0, 0]
]
note: This example need to think specially!
3. Given array nums = [-3, -3, -3, 2, 2, 2,0, 0, 0, 3, 3, 3], and target = 0.
solution set is:
[
[-1, 0, 0, 1],
[-2, -1, 1, 2],
[-2, 0, 0, 2]
]
Note: How to deal with the duplicate quadruplets ?
思路(这里是4个数的和为target,我们之前不是做过3个数的吗?Refer this one 正好可以用上)
1. 升序排序(sort即可)
2. 不可避免地要遍历数组(i)
3. 借鉴leetcode-15中的三个数的和,我们如法炮制,搜索剩下的三个数(j = i+1, l=j+1, r = size()-1);对参数有疑惑的话,看下面代码。
时间复杂度:O(n3),暴力的话应该会超时,没试!
这题里面尤其要注意避免重复
源码
 class Solution {
 public:
     vector<vector<int>> fourSum(vector<int>& nums, int target) {
         vector<vector<int>> res;
         int len = nums.size();
         if(len < )
             return res;
         sort(nums.begin(), nums.end());
         for(int i=; i<=len-; i++)
         {
             while(i> && i<=len- && nums[i]==nums[i-])
                 i++;//消除i的重复
             for(int j=i+; j<=len-; j++)
             {
                 while(j>i+ && j<=len- && nums[j]==nums[j-])
                     j++;//消除j的重复
                 int l=j+, r=len-;
                 while(l < r)
                 {
                     int sum = nums[i] + nums[j] + nums[l] + nums[r];
                     if(sum > target)
                         r--;
                     else if(sum < target)
                         l++;
                     else
                     {
                         res.push_back(vector<int> {nums[i], nums[j], nums[l], nums[r]});
                         while(l<r && nums[l]==nums[l+]) l++;//消除l的重复
                         while(l<r && nums[r]==nums[r-]) r--;//消除r的重复
                         l++; r--;
                     }
                 }
             }
         }
         return res;
     }
 };
如果对消除重复有疑问的童鞋,请留言, 或者自行把example 3 手推一遍就明白了。
Array + two points leetcode.18 - 4Sum的更多相关文章
- [LeetCode] 18. 4Sum 四数之和
		Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = tar ... 
- [LeetCode] 18. 4Sum ☆☆
		Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = tar ... 
- LeetCode 18. 4Sum (四数之和)
		Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = tar ... 
- LeetCode——18. 4Sum
		一.题目链接:https://leetcode.com/problems/4sum/ 二.题目大意: 给定一个数组A和一个目标值target,要求从数组A中找出4个数来使之构成一个4元祖,使得这四个数 ... 
- LeetCode 18 4Sum (4个数字之和等于target)
		题目链接 https://leetcode.com/problems/4sum/?tab=Description 找到数组中满足 a+b+c+d=0的所有组合,要求不重复. Basic idea is ... 
- Leetcode 18. 4Sum
		Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = tar ... 
- Java [leetcode 18]4Sum
		问题描述: Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d ... 
- C#解leetcode   18. 4Sum
		Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = tar ... 
- [leetcode]18. 4Sum四数之和
		Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums s ... 
随机推荐
- (十)java虚拟机性能监控工具
			一. jps(Java Virtual Machine Process Status Tool) jps主要用来输出JVM中运行的进程状态信息.语法格式如下: 命令行参数选项说明如下: 1.1 案例 ... 
- (四)HttpServletRequest对象(转)
			转自“孤傲苍狼”博客. Web服务器收到客户端的http请求,会针对每一次请求,分别创建一个用于代表请求的request对象.和代表响应的response对象. request和response对象即 ... 
- C#关于DateTime得到的当前时间的格式和用法
			DateTime.Now.ToShortTimeString() DateTime dt = DateTime.Now; dt.ToString();//2005-11-5 13:21:25 dt.T ... 
- 前端添加视频流rtmp格式
			要求:rtmp格式, 在线直播 url地址 效果: 代码:初次打开时间较长, <!DOCTYPE html> <html> <head> <script ty ... 
- Vue双向数据绑定简易实现
			一.vue中的双向数据绑定主要使用到了Object.defineProperty(新版的使用Proxy实现的)对Model层的数据进行getter和setter进行劫持,修改Model层数据的时候,在 ... 
- Redis HashMap 使用
			散列类型相当于Java中的HashMap,他的值是一个字典,保存很多key,value对,每对key,value的值个键都是字符串类型,换句话说,散列类型不能嵌套其他数据类型.一个散列类型键最多可以包 ... 
- python数据结构_递归_汉诺塔问题
			已经不是第一次写这个汉诺塔问题, 其实递归还真是不太好理解, 因为递归这种是想其实有点反人类, 为什么? 因为不太清楚, 写个循环一目了然, 用递归其实要把核心逻辑理清楚, 要不根本没法进行下去 所有 ... 
- annotation  @Retention@Target
			一.注解:深入理解JAVA注解 要深入学习注解,我们就必须能定义自己的注解,并使用注解,在定义自己的注解之前,我们就必须要了解Java为我们提供的元注解和相关定义注解的语法. 1.元注解(meta-a ... 
- Java面试 - static 修饰的变量和方法有哪些特点?
			1.static修饰的变量和方法,在类加载时即被初始化,可直接通过类名.变量名和类型.方法名进行调用. 2.static修饰的变量,在类加载时会被分配到数据区的方法区.类的实例可共享方法区中的变量.如 ... 
- Detect cycle in a directed graph
			Question: Detect cycle in a directed graph Answer: Depth First Traversal can be used to detect cycle ... 
