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 = 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)

这是求四个数的和的问题。能够和前面三个数和的问题一样转化成两个数和的问题。即先固定第一个数。再依据第一个数固定第二个数,然后就是求两数和的问题了。数组里面的元素可能会有反复元素,所以须要注意消除反复的推断。时间复杂度是O(N^3)。

runtime:192ms

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());
        vector<int>::iterator iter1=nums.begin();
        vector<int>::iterator iter2=iter1+1;

        //最外层第一个指针遍历
        for(;iter1!=nums.end()-3;iter1++)
        {

            //固定第一个指针后第二个指针遍历
            for(iter2=iter1+1;iter2!=nums.end()-2;iter2++)
            {

                //内层两个指针从两个方向遍历,这是经典的求两个数的和的问题
                int base=*iter1+*iter2;
                vector<int>::iterator iter3=iter2+1;
                vector<int>::iterator iter4=nums.end()-1;
                while(iter3<iter4)
                {
                    if((*iter3+*iter4+base)<target)
                    {
                        iter3++;
                    }
                    else if((*iter3+*iter4+base)>target)
                    {
                        iter4--;
                    }
                    else
                    {
                        //这里须要注意vector中push_back和用下标訪问的差别,用小标訪问时相似于数组,所以tmp初始化时须要指定vector的大小,可是用push_back时不要指定大小。否则小心push_back是在你指定大小的vector后加入元素
                        vector<int> tmp;
                        tmp.push_back(*iter1);
                        tmp.push_back(*iter2);
                        tmp.push_back(*iter3);
                        tmp.push_back(*iter4);
                        result.push_back(tmp);

                        //这是推断是否有反复的方式,假设下一指针依旧小于iter4而且下一个指针指向的值与当前值同样。iter3须要加1
                        while((iter3+1)<iter4 && *(iter3)==*(iter3+1)) iter3++;

                        while(iter3<(iter4-1)&& *(iter4)==*(iter4-1)) iter4--;

                        //推断完毕后还须要将iter3加1或iter4减一以继续查寻下一个和为常数的组合
                        iter3++;
                    }
                }

                //对于iter2也须要推断是否存在反复元素问题
                while(((iter2+1)!=(nums.end()-2))&&(*(iter2)==*(iter2+1))) iter2++;

            }

            //同理对于ier1也是如此
            while(((iter1+1)!=(nums.end()-3))&&(*(iter1)==*(iter1+1))) iter1++;

        }
          return result;
    }
};
时间: 2024-08-06 11:18:13

LeetCode18:4Sum的相关文章

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

No.018:4Sum

题目: 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: The solution set must not contain duplicate quadruplets. For ex

LeetCode OJ:4Sum(4数字之和)

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.

leetcode笔记:4Sum

一.题目描述 二.解题技巧 这道题从表面上看与3Sum极其相似,事实上确实可以使用相同的思维和方法,只不过这样做的话,时间复杂度为O(n^3),空间复杂度为O(1)将超时. 这道题也可以在排序之后先计算后面两个数的和,将其方法一个哈希表中,由于可能存在不同的两个数的和为相同值,因此,可以考虑将和为相同的值放在一个链表中,然后将变量头放在哈希表中.然后再按照3Sum的思路,不过第三个数在这里变成了第三个和第四个数的和,通过哈希表可以方便地找到和为固定值的数的链表,就可以找到符合条件的四个数.这种方

LeetCode 018 4Sum

题目描述:4Sum 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-descend

LeetCode——18. 4Sum

一.题目链接:https://leetcode.com/problems/4sum/ 二.题目大意: 给定一个数组A和一个目标值target,要求从数组A中找出4个数来使之构成一个4元祖,使得这四个数的和等于target,找出所有的四元组,当然这些四元组不能有重复的. 三.题解: 这道题实质就是3sum的变形,关于4sum问题,已经在https://www.cnblogs.com/wangkundentisy/p/9079622.html这里说过了,无外乎最外面两层循环,最里面的循环使用哈希表或

LeetCode Animation 题目图解汇总(持续更新中...)

我会尽力将LeetCode上所有的题目都用动画的形式演示出来,期待与你见证这一天! GitHub Repo:LeetCode Animation Follow: MisterBooo · GitHub Problems ID Problem Article Animation 001 两数之和 每天一算:Two Sum 019 删除链表的倒数第N个节点 每天一算:Remove Nth Node From End of List 020 有效的括号 每天一算:Valid Parentheses 0

Oracle 11g数据库详解(2015-02-28更新)

Oracle 11g数据库详解 整理者:高压锅 QQ:280604597 Email:[email protected] 大家有什么不明白的地方,或者想要详细了解的地方可以联系我,我会认真回复的 1   简介 数据库操作主要有以下几步: 1.  启动.停止数据库 2.  连接.断开数据库 3.  创建.修改.删除数据库用户 4.  表空间 5.  新建.修改.删除表 6.  查询.插入.修改.删除表数据 7.  新建.修改.删除视图 8.  新建.修改.删除存储过程 9.  新建.修改.删除触发

LeetCode:18. 4Sum(Medium)

1. 原题链接 https://leetcode.com/problems/4sum/description/ 2. 题目要求 给出整数数组S[n],在数组S中是否存在a,b,c,d四个整数,使得四个数之和等于目标整数target.请找出所有满足此条件的四个整数. 3. 解题思路 先对nums进行排序,然后采用两层for循环来确定前两个数字,最后在第二层for循环中确定后两个数字. 注意可能存在重复解!! 如下图所示,对Input先进行排序:[-4, -1, -1,0, 1,2],target