一天一道LeetCode

(一)题目

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: Elements in a quadruplet (a,b,c,d) must be in non-descending

order. (ie, a ≤ b ≤ c ≤ d) 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)

(二)解题

勉强不超时的代码。(注:和3sum比较类似)

class Solution {
public:
    vector<vector<int>> fourSum(vector<int>& nums, int target) {
        vector<vector<int>> result;
        if(nums.size()<4) return result;
        sort(nums.begin(),nums.end());
        for(int i = 0 ; i < nums.size()-3 ; )
        {
            for(int j=i+1 ; j < nums.size()-2 ; )
            {
                int start = j+1;
                int end = nums.size()-1;
                while(start<end)
                {
                    int sum = nums[i]+nums[j]+nums[start]+nums[end];
                    if(sum==target)
                    {
                        vector<int> vec;
                        vec.push_back(nums[i]);
                        vec.push_back(nums[j]);
                        vec.push_back(nums[start]);
                        vec.push_back(nums[end]);
                        result.push_back(vec);
                        start++;
                        while(start<end && nums[start] == nums[start-1]) start++;
                        end--;
                        while(start<end && nums[end] == nums[end+1]) end--;
                    }
                    else if(sum>target)
                    {
                        end--;
                        while(start<end && nums[end] == nums[end+1]) end--;
                    }
                    else {
                        start++;
                        while(start<end && nums[start] == nums[start-1]) start++;;
                    }
                }
                j++;
                while(j<nums.size()-2 && nums[j] == nums[j-1]) j++;
            }
            i++;
            while(i<nums.size()-3 && nums[i] == nums[i-1]) i++;
        }
        return result;
    }
};

【一天一道LeetCode】#18. 4Sum的更多相关文章

  1. [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 ...

  2. LeetCode——18. 4Sum

    一.题目链接:https://leetcode.com/problems/4sum/ 二.题目大意: 给定一个数组A和一个目标值target,要求从数组A中找出4个数来使之构成一个4元祖,使得这四个数 ...

  3. LeetCode 18 4Sum (4个数字之和等于target)

    题目链接 https://leetcode.com/problems/4sum/?tab=Description 找到数组中满足 a+b+c+d=0的所有组合,要求不重复. Basic idea is ...

  4. [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 ...

  5. 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 ...

  6. leetcode 15 3sum & leetcode 18 4sum

    3sum: 1 class Solution { public: vector<vector<int>> threeSum(vector<int>& num ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

  10. [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 ...

随机推荐

  1. 20160208.CCPP体系详解(0018天)

    程序片段(01):main.c 内容概要:PointWithOutInit #include <stdio.h> #include <stdlib.h> //01.野指针详解: ...

  2. Tomcat怎么实现异步Servlet

    有时Servlet在生成响应报文前必须等待某些耗时的操作,比如在等待一个可用的JDBC连接或等待一个远程Web服务的响应.对于这种情况servlet规范中定义了异步处理方式,由于Servlet中等待阻 ...

  3. Android TV开发总结(六)构建一个TV app的直播节目实例

    请尊重分享成果,转载请注明出处:http://blog.csdn.net/hejjunlin/article/details/52966319 近年来,Android TV的迅速发展,传统的有线电视受 ...

  4. Premake可生成vcxproj.filters

    Premake可生成vcxproj.filters (金庆的专栏) 添加 vcxproj.filters 文件可以用目录结构组织源文件. 例如premake5添加所有文件: files {       ...

  5. Android开发基础规范(一)

    转载请把头部出处链接和尾部二维码一起转载,本文出自逆流的鱼yuiop:http://blog.csdn.net/hejjunlin/article/details/52602487 前言:Androi ...

  6. FFmpeg的HEVC解码器源代码简单分析:概述

    ===================================================== HEVC源代码分析文章列表: [解码 -libavcodec HEVC 解码器] FFmpe ...

  7. XMPP系列(七)---获取群组列表

    上一篇介绍了如何创建群组,这一篇就介绍一下,如何获取自己的群组列表. 在上一篇有提到,如果我们创建的群组是公共的群组,那么获取自己的群组列表时,会获取到自己的群组列表和那些公共的群组.而实际做社交的应 ...

  8. JQuery之DOM操作及常用函数

    属性操作 attr(name)获取属性值 var imgSrc = $("img").attr("src") attr(name,value)设置属性值 $(& ...

  9. Servlet3.0注解@WebInitParam和@WebServlet

    在以前的servlet中我们初始化一些参数都是配置在web.xml中的,自从servlet3.0之后给我们提供了注解@WebServlet和@WebInitParam,@WebServlet是用来配置 ...

  10. Android实现系统ROOT, 并能赋予app root权限

    1. 获取root权限 -->  修改adb源码     a. 打开 system/core/adb/adb_main.cpp,或者是 system/core/adb/daemon/main.c ...