一天一道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. linux2.6.37内核接两个硬盘导致读写效率变低的问题

    一.问题分析: 通过跟踪定位write系统调用的实现发现,在每次调用a_ops->write_end之后,都会去调用balance_dirty_pages_ratelimited,该函数负责检查 ...

  2. Linux命令行总结

    1.修改同一目录下所有图片的尺寸(宽x高) 长宽比不变&长宽比改变 find ./ -name '*.jpg' -exec convert -resize 600x480 {} {} \; f ...

  3. Web自动化框架LazyUI使用手册(3)--单个xpath抓取插件详解(selenium元素抓取,有此插件,便再无所求!)

    概述 前面的一篇博文粗略介绍了基于lazyUI的第一个demo,本文将详细描述此工具的设计和使用. 元素获取插件:LazyUI Elements Extractor,作为Chrome插件,用于抓取页面 ...

  4. Dynamics CRM2016 时间字段属性中的新增行为

    之前的博客中有特地介绍过CRM中的时间字段以及它在不同的应用场景中涉及的时制转换,而CRM2016又给时间字段添加了新的行为,具体见下属截图,简单介绍下每个图中对应的行为的意思,最后会做demo来具体 ...

  5. T-SQL 中的CROSS JOIN用法(半翻译)

    突然发现个很吊的链接,我们来看看学习数据库要做些什么,胆小慎点:DBA工作内容!!!! 今天来翻译一篇关于T-SQL的文章,本文可供微软认证70-461:QueryingMicrosoft SQL S ...

  6. 【java虚拟机序列】java中的垃圾回收与内存分配策略

    在[java虚拟机系列]java虚拟机系列之JVM总述中我们已经详细讲解过java中的内存模型,了解了关于JVM中内存管理的基本知识,接下来本博客将带领大家了解java中的垃圾回收与内存分配策略. 垃 ...

  7. Shell 整数比较、字符串比较

    整数比较  -eq       等于,如:if [ "$a" -eq "$b" ]  -ne       不等于,如:if [ "$a" - ...

  8. Xdoclet + Ant自动生成Hibernate配置文件

    在使用Hibernate的时候,过多的Hibernate配置文件是一个让人头疼的问题.最近接触了Xdoclet这个工具.它实际上就是一个自动代码生成的工具,Xdoclet不能单独运行,必须搭配其他工具 ...

  9. UNIX网络编程——使用select 实现套接字I/O超时

    下面程序包含read_timeout.write_timeout.accept_timeout.connect_timeout 四个函数封装: /* read_timeout - 读超时检测函数,不含 ...

  10. 美国康奈尔大学BioNB441元胞自动机MATLAB应用

    美国康奈尔大学BioNB441在Matlab中的元胞自动机 介绍 元胞自动机(CA)是用于计算计划利用当地的规则和本地通信.普遍CA定义一个网格,网格上的每个点代表一个有限数量的状态中的细胞.过渡规则 ...