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. Android-事件分发(OnTouchEvent,OnTouch,OnClick)

    http://blog.csdn.net/lmj623565791/article/details/38960443 http://blog.csdn.net/guolin_blog/article/ ...

  2. Android - fragment Manager

    fragment基本使用: http://www.cnblogs.com/qlky/p/5415679.html Fragmeng优点 Fragment可以使你能够将activity分离成多个可重用的 ...

  3. linux 新建用户和权限分配

    1.创建新用户:testuser 命令:#useradd 选项 用户名 选项: -c comment 指定一段注释性描述. -d 目录 指定用户主目录,如果此目录不存在,则同时使用-m选项,可以创建主 ...

  4. org.springframework.beans.factory.BeanDefinitionStoreException Invalid bean defi

    org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 's ...

  5. 1.String、StringBuffer与StringBuilder之间区别

    1.三者在执行速度方面的比较:StringBuilder >  StringBuffer  >  String 2.String <(StringBuffer,StringBuild ...

  6. 语义SLAM的数据关联和语义定位(一)

    语义SLAM和多传感器融合是自动驾驶建图和定位部分比较热门的两种技术.语义SLAM中,语义信息的数据关联相较于特征点的数据关联有所不同.我们一般用特征描述子的相似性来匹配和关联不同图像中的特征点.特征 ...

  7. SQLServer 学习笔记之超详细基础SQL语句 Part 3

    Sqlserver 学习笔记 by:授客 QQ:1033553122 -----------------------接Part 2------------------- 13. 使用compute对查 ...

  8. JavaScript Data.parse()转化时间戳安卓和ISO不兼容

    Data.parse()获取时间戳,在Android是没有问题的,但是在ISO就不行了,原因在于转化成时间戳的时间格式不一样. Android的格式是如“2017-12-12 12:12:12”,IS ...

  9. Bootstrap源码分析系列之核心CSS

    本节主要介绍核心CSS,从整体架构中的7个Less文件对应的源码分别进行分析 scaffolding.less 这个文件编译后的css文件(886~989行)其作用就像定义全局样式. //调整css盒 ...

  10. 从零自学Java-2.初步理解Java程序使如何工作的

    1.学习Java应用程序是如何工作的 2.构成一个应用程序 3.向应用程序传递参数 4.学习Java程序是如何组织的 5.在应用程序中创建一个对象 程序Root:输出225的正平方根 package ...