`
king_tt
  • 浏览: 2148904 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

【leetcode】4Sum

 
阅读更多

Question:

Given an arraySofnintegers, are there elementsa,b,c, anddinSsuch thata+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)


Anwser 1: O(n^3)

class Solution {
public:
    vector<vector<int> > fourSum(vector<int> &num, int target) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        int len = num.size();
        vector< vector<int> > ret;
        
        if(len < 4) return ret;
        
        vector<int> num2;
        num2.assign(num.begin(), num.end());
        
        sort(num2.begin(), num2.end());
        
        vector<int> tmpRet(4);
        
        tmpRet[0] = num2[0] - 1;
        for(int i = 0; i < len - 3; i++){
            if(tmpRet[0] == num2[i]) continue;
            
            tmpRet[0] = num2[i];
            
            tmpRet[1] = num2[i+1] - 1;
            for(int j = i + 1; j < len - 2; j++){
                if(tmpRet[1] == num2[j]) continue;
                tmpRet[1] = num2[j];
                
                int l = j + 1;
                int r = len - 1;
                
                while(l < r){
                    if(tmpRet[0] + tmpRet[1] + num2[l] + num2[r] == target){
                        tmpRet[2] = num2[l];
                        tmpRet[3] = num2[r];
                        
                        ret.push_back(tmpRet);
                        
                        while(num2[++l] == num2[l-1] && l < r);
                        while(num2[--r] == num2[r+1] && l < r);
                        
                    } else if(tmpRet[0] + tmpRet[1] + num2[l] + num2[r] < target){
                        l++;
                    } else {
                        r--;
                    }
                }
            }
        }
        
    }
};




参考推荐:

3Sum

LeetCode: 4Sum


分享到:
评论

相关推荐

    python数组双指针算法求和问题LeetCode2sum3sum4sum含代码

    双指针算法,python数组双指针算法求和问题LeetCode2sum3sum4sum含代码

    文档python数组双指针算法求和问题LeetCode2sum3sum4sum含代码

    文档python数组双指针算法求和问题LeetCode2sum3sum4sum含代码提取方式是百度网盘分享地址

    Leetcode two sum java 解法

    Leetcode two sum java 解法

    oj.leetcode 2sum 解

    oj.leetcode题解, 2sum, 运用hashtable解决这到问题,时间复杂度O(N)

    leetcode2sum-leetCodeSolutions:我对leetCode问题的解决方案

    leetcode 2sum leetCode解决方案 我对 leetCode 问题的解决方案 已解决的练习列表: 简单的: 2sum - 带有未排序的数组; O(n) 复杂度; 花了~3ms 2sum - 使用排序数组; O(n) 复杂度 罗马转整数; O(n) 复杂度 合并...

    leetcode2sum-Problems:编程问题的回购

    LC4: Longest Palindromic Substring [Medium] LC5: Median Of Two Sorted Arrays [Hard] LC6: Zigzag Conversion [Medium] LC7: Reverse Integer [Easy] LC8: String To Integer Atoi [Medium] LC9: Palindrome ...

    2sumleetcode-2SUM:使用先前地图的最快二和O(N)

    leetcode 2SUM 使用以前的地图最快的 2Sum O(N) 使用 unordered_map 比 map 快 以前的地图 là gì ? 上一张地图 đơn giản là 地图 nhưng thay vì ta cần một vòng for để khởi tạo 地图 thì ta khởi...

    leetcode2sum-Q1.-2Sum:力码研究1

    leetcode 2sum 2总和 力码研究1

    leetcode3sum-LeetCode:leetcode问题用C#解决

    3sum 力码 力扣算法 (注:“ :locked: " 表示你需要来自 Leetcode) # 标题 解决方案 困难 001 简单的 002 中等的 003 中等的 004 难的 005 中等的 006 简单的 007 简单的 008 简单的 009 简单的 010 难的 011 中等...

    leetcode3sum-leetcode-curation-topical:技术面试准备清单

    3sum leetcode-curation-topical 精选 Leetcode 问题,按主题/概念分类。 我的策展标准是问题必须是有价值的,而不仅仅是为了难而难。 有价值的问题通常可以以不同的时间/空间效率(通过使用各种数据结构)以多种...

    LeetCode最全代码

    18| [4 Sum](https://leetcode.com/problems/4sum/) | [C++](./C++/4sum.cpp) [Python](./Python/4sum.py) | _O(n^3)_ | _O(1)_ | Medium || Two Pointers 26 | [Remove Duplicates from Sorted Array]...

    LeetCode 刷题汇总1

    * 4Sum(4Sum):找到数组中四个元素的和等于目标值的元素。 这些知识点涵盖了算法设计、数据结构、数学运算、字符串操作、链表操作、栈和队列操作、字符串匹配和排序搜索等多方面的内容,为LeetCode刷题提供了一...

    LeetCode -- Path Sum III分析及实现方法

    主要介绍了LeetCode -- Path Sum III分析及实现方法的相关资料,希望通过本文能帮助到大家,需要的朋友可以参考下

    2sumleetcode-LeetCode:力码

    leetcode 轮廓 1_count_and_say.cpp - super_ugly_number.cpp - Detect_Pattern.cpp - degree_of_array.cpp - 键盘.cpp - 2Sum_Data_Structure_Design.cpp - shuffle_array.cpp - permutations.cpp - kth_missing_...

    leetcode3sumnlogn-learn-algorithm:学习算法

    leetcode 3sum nlogn 让我们研究算法 微电脑网 : 2018.09.06。 用 BFS 搜索,用 DP 解决。 我过去没能做到,但感觉很好。 : 2018.09.06。 起初,它以这样的方式重复,如果有什么变化,它会再次旋转。 -&gt; 超时 接下来...

    leetcode答案-LeetCode_1_TwoSum:LeetCode_1_TwoSum

    答案LeetCode_1_TwoSum LeetCode 问题:给定一个整数数组,找出两个数字,使它们相加为特定的目标数字。 函数 twoSum 应该返回两个数字的索引,使它们相加为目标,其中 index1 必须小于 index2。 请注意,您返回的...

    Leetcode 题解.pdf

    双指针可以用来解决有序数组中的两数之和问题,例如 Leetcode 的 167 题 Two Sum II - Input array is sorted( Easy)。 在这个题目中,我们可以使用双指针来遍历数组,一个指针指向值较小的元素,一个指针指向值...

    Two Sum leetcode c++

    The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2)...

    leetcode2sumc-leetcode:JavaScript版本leetcode中文版代码

    leetcode 2 sum c leetcode 力扣(Leetcode)编程题,JavaScript版本。 编号 中文题目 英文题目 题目描述 JavaScript 难度 1 Two Sum 简单 2 Add Two Numbers 中等 ...3Sum ...3Sum ...4Sum 中等 19 Remo

    程序员面试宝典LeetCode刷题手册

    18. 4Sum 19. Remove Nth Node From End of List 20. Valid Parentheses 21. Merge Two Sorted Lists 22. Generate Parentheses 23. Merge k Sorted Lists 24. Swap Nodes in Pairs 25. Reverse Nodes in k-Group 26...

Global site tag (gtag.js) - Google Analytics