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. 理解Java异常

    一.Java异常的简介 Java异常是Java提供的一种识别及响应错误的一致性机制.具体来说,异常机制提供了程序退出的安全通道.当出现错误后,程序执行的流程发生改变,程序的控制权转移到异常处理器.Ja ...

  2. UI-12组结对编程作业总结

    UI-12组结对编程作业总结 源码Github地址 https://github.com/tilmto/TILMTO/tree/master/Arithmetic 作业摘要 本次结对编程作业分为以下两 ...

  3. CodeDOM 系列一: 初识

    最近手头项目接触到了CodeDom,顺带着在这里做个系列文章,有兴趣的可以做个参考.   CodeDOM是个用于运行时生成代码,以及编译生成的代码的相关技术.我们通过构造CodeDOM这样的DOM树 ...

  4. Django Rest Framework 请求流程

    用户请求到django,首先经过wsgi,中间件,然后到url路由系统,执行视图类中继承APIView执行as_view方法,在源码中可以看到VPIView继承了django的View类,通过supe ...

  5. grunt搭建自动化的web前端开发环境(转)

    1. 前言 各位web前端开发人员,如果你现在还不知道grunt或者听说过.但是不会熟练使用grunt,那你就真的真的真的out了(三个“真的”重复,表示重点).至于grunt的作用,这里不详细说了, ...

  6. canvas绘画交叉波浪

    做个记录,自己写的动态效果,可能以后用的着呢: <!DOCTYPE html> <html> <head> <meta charset="UTF-8 ...

  7. Echarts地图绘制(散点,色卡)

    echarts绘制地图时,提供了js内部注册,也提供了json数据手动注册,这两种都可以绘制对应地图,但有一点不同的是,js内部只注册了中国地图和世界地图,而json数据提供了世界,中国,中国城市的数 ...

  8. eclipse搭建hibernate环境

    一.打开eclipse,help=>Install New Software 输入:http://download.jboss.org/jbosstools/updates/stable/kep ...

  9. JavaSE——线程调度

    线程调度: 按照特定机制为线程分配cpu的使用权. 线程调度模型: 分时调度 所有线程轮流获得cpu的使用权,平均分配每个线程占用的cpu的时间片. 抢占时调度(java虚拟机) 可运行池中优先级高的 ...

  10. localStorage/cookie 用法分析与简单封装

    本地存储是HTML5中提出来的概念,分localStorage和sessionStorage.通过本地存储,web应用程序能够在用户浏览器中对数据进行本地的存储.与 cookie 不同,存储限制要大得 ...