leetcode 44: 4Sum
来源:互联网 发布:vb制作剪刀石头布游戏 编辑:程序博客网 时间:2024/06/09 19:40
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)
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 vector<vector<int> > rel; int sz = num.size(); if(sz<4) return rel; sort( num.begin(), num.end() ); set< vector<int> > mySet; for( int i=0; i<sz-3; i++) { for( int j=i+1; j<sz-2; j++) { int l = j + 1; int h = sz-1; while( l < h) { int temp = num[i] + num[j] + num[l] + num[h]; if( temp == target){ vector<int> vec; vec.push_back( num[i]); vec.push_back( num[j]); vec.push_back( num[l]); vec.push_back( num[h]); if( mySet.find( vec ) == mySet.end() ) { rel.push_back( vec ); mySet.insert( vec ); } ++l; --h; } else if( temp < target) { ++l; } else { --h; } } } } return rel; } };
- leetcode 44: 4Sum
- 【Leetcode】4Sum (Sum)
- leetcode 2 sum 3sum 4sum
- Leetcode 2SUM-3SUM-4SUM
- Leetcode-2sum,3sum,4sum
- leetcode 2 sum & 3 sum & 4 sum
- [LeetCode] 2Sum, 3Sum, 4Sum, 3SUm closet
- [LeetCode] K sum(2Sum、3Sum、4Sum)
- leetcode--sum集合:2sum,3sum,4sum
- leetcode --- 2 sum , 3 sum , 4 sum , k sum problem
- LeetCode: 4 Sum
- LeetCode: 4Sum
- LeetCode 4Sum
- Leetcode: 4SUM
- 【leetcode】 4 sum
- LeetCode 4Sum
- [Leetcode] 4 Sum [Unsolved]
- 【leetcode】4Sum
- 黑马程序员——java集合
- firefox用12306Helper 0.2结合ie tab+自动订火车票和完成支付全攻略
- 便利构造器
- js 常用函数
- WinCE7的文件系统注册表设置
- leetcode 44: 4Sum
- 2013年云计算盈利时点将出现 行业指导意见或出台
- shell表达式中引用外部变量的方法(例如awk中引用外部变量)
- vim(gvim)支持对齐线
- non-numeric second argument to `wordlist' function: '
- c#获取文件夹名称
- mysql 索引优化
- allocwithzone
- Ethernet versus ATM