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.

Note:

The solution set must not contain duplicate quadruplets.

Example:

Given array nums = [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]
]
想法:类似于3Sum的解决方式,设立双指针求解
class Solution {
public:
    vector<vector<int>> fourSum(vector<int>& nums, int target) {
        int len = nums.size();
        vector<vector<int>> result;
         == len)
            return result;
        sort(nums.begin(),nums.end());
         ; i < len- ; i++){
             && nums.at(i) == nums.at(i-))
                continue;
             && nums.at(i) + nums.at(i+)+nums.at(i+)+nums.at(i+) > target)
                break;
             && nums.at(i) + nums.at(len-)+nums.at(len-)+nums.at(len-) < target)
                continue;
                 ; j < len- ; j++){
                     && nums[j] == nums[j-])
                        continue;
                     && nums[i] + nums[j] + nums[j+] + nums[j+] > target)
                        break;
                     && nums[i] + nums[j] + nums[len-] + nums[len-] < target)
                        continue;

                    ;
                        ;
                        while(left < right){
                            int temp = nums.at(i) + nums.at(j) + nums.at(left) + nums.at(right);
                            if(temp == target){
                                result.push_back({nums.at(i),nums.at(j),nums.at(left),nums.at(right)});
                                left++;
                                right--;
                                ])//避免重复
                                    left++;
                                ])
                                    right--;
                            }else if(temp < target){
                                left++;
                            }else{
                                right--;
                            }
                        }

                }

        }
        return result;

    }
};

leetcode18—4Sum的更多相关文章

  1. [array] leetCode-18. 4Sum -Medium

    18. 4Sum -Medium descrition Given an array S of n integers, are there elements a, b, c, and d in S s ...

  2. LeetCode18 4Sum

    题意: Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = ...

  3. ARTS第八周

    1.Algorithm:每周至少做一个 leetcode 的算法题2.Review:阅读并点评至少一篇英文技术文章3.Tip:学习至少一个技术技巧4.Share:分享一篇有观点和思考的技术文章 以下是 ...

  4. LeetCode18: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 ...

  5. [Swift]LeetCode18. 四数之和 | 4Sum

    Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums s ...

  6. [LeetCode] 4Sum II 四数之和之二

    Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such t ...

  7. [LeetCode] 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 ...

  8. LeetCode:3Sum, 3Sum Closest, 4Sum

    3Sum Closest Given an array S of n integers, find three integers in S such that the sum is closest t ...

  9. 2016/10/28 很久没更了 leetcode解题 3sum问题进阶版4sum

    18. 4Sum Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c  ...

随机推荐

  1. BestCoder Round #27

    Jump and Jump... Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  2. 常见hash算法的原理(转)

    常见hash算法的原理   散列表,它是基于快速存取的角度设计的,也是一种典型的“空间换时间”的做法.顾名思义,该数据结构可以理解为一个线性表,但是其中的元素不是紧密排列的,而是可能存在空隙. 散列表 ...

  3. java环境配置及原理详解

    java环境配置及原理详解 1.java跨平台的本质 我们谈到java,总是提到跨平台这个词.那么java语言是怎么实现跨平台的呢? 我们编写的java代码不是直接让windows系统读取解析,而是在 ...

  4. 类(class)相关概念小结

    参考在线文档,整理php中类的相关概念如下   $this 在类的内部可以使用伪变量$this,这个伪变量为一个到主叫对象(经个人测试理解这应该是在运行时的真实对象,不是类,运行时绑定)的引用,所以一 ...

  5. 使用ThinkPHP实现分页功能

    前几篇(上传,缩略图,验证码,自动验证表单)文章介绍的功能实现都是基于ThinkPHP框架封装好的类进行实现的,所以这次自己写一个分页类在框架中使用. 首先在根目录建一个Tools文件夹,在Tools ...

  6. 设计模式原则(7)--Composition&AggregationPrinciple(CARP)--合成&聚合复用原则

    作者QQ:1095737364    QQ群:123300273     欢迎加入! 1.定义:  要尽量使用合成和聚合,尽量不要使用继承. 2.使用场景: 要正确的选择合成/复用和继承,必须透彻地理 ...

  7. JAVA generic array 泛型数组

    在JAVA中是不支持泛型数组的,不能通过 Z[] array=new Z[10] 这样的方式来创建数组,而是使用反射Aarry.newInstance来创建: 具体代码如下: public Z[][] ...

  8. CPA理论与Base理论

    CPA理论: 由于对系统或者数据进行了拆分,我们的系统不再是单机系统,而是分布式系统,针对分布式系统的CAP原理包含如下三个元素. C:Consistency,一致性.在分布式系统中的所有数据 备份, ...

  9. 令人血脉喷张的animate.css

    都说html5.css3是大局是潮流,尽管css3也不难,学起来比较简单,但我还是喜欢他的轻量级集大成者的animate.css框架,初学的小伙伴们加油啦 下面我分析一下animate.css框架里面 ...

  10. css 文本两行显示,超出省略号表示

     重点:text-overflow: ellipsis;只对display:inline:起作用 例子: <span class="a">我说说<b class= ...