LeetCode_ 4 sum
- 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> &num, int target) {
- // Note: The Solution object is instantiated only once and is reused by each test case.
- vector<vector<int>> res;
- int len = num.size();
- if(len < ) return res;
- sort(num.begin(),num.end());
- for(int i = ; i< len-;++i){
- while(i> && i< len- && num[i] == num[i-])++i;
- for(int j = i+; j< len-; ++j){
- while(j!=i+ && j< len-&&num[j] == num[j-])++j;
- int left = j+;
- int right = len-;
- while(left < right){
- int sum = num[i] + num[j] +num[left]+num[right];
- if(sum == target){
- vector<int> ans;
- ans.push_back(num[i]);
- ans.push_back(num[j]);
- ans.push_back(num[left]);
- ans.push_back(num[right]);
- res.push_back(ans);
- left++;
- while(left<right && num[left]==num[left-]) ++left;
- right--;
- while(left < right && num[right] == num[right+]) --right;
- }else if(sum <target){
- left++;
- while(left<right && num[left]==num[left-]) ++left;
- }else{
- right--;
- while(left < right && num[right] == num[right+]) --right;
- }
- }//while(left <right)
- }//for j
- }// for i
- return res;
- }
- };
LeetCode_ 4 sum的更多相关文章
- LeetCode - Two Sum
Two Sum 題目連結 官網題目說明: 解法: 從給定的一組值內找出第一組兩數相加剛好等於給定的目標值,暴力解很簡單(只會這樣= =),兩個迴圈,只要找到相加的值就跳出. /// <summa ...
- Leetcode 笔记 113 - Path Sum II
题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- POJ 2739. Sum of Consecutive Prime Numbers
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20050 ...
- BZOJ 3944 Sum
题目链接:Sum 嗯--不要在意--我发这篇博客只是为了保存一下杜教筛的板子的-- 你说你不会杜教筛?有一篇博客写的很好,看完应该就会了-- 这道题就是杜教筛板子题,也没什么好讲的-- 下面贴代码(不 ...
- [LeetCode] Path Sum III 二叉树的路径和之三
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
- [LeetCode] Partition Equal Subset Sum 相同子集和分割
Given a non-empty array containing only positive integers, find if the array can be partitioned into ...
- [LeetCode] Split Array Largest Sum 分割数组的最大值
Given an array which consists of non-negative integers and an integer m, you can split the array int ...
- [LeetCode] Sum of Left Leaves 左子叶之和
Find the sum of all left leaves in a given binary tree. Example: 3 / \ 9 20 / \ 15 7 There are two l ...
随机推荐
- Collections之sort、reverse
在使用List集合时,通常情况下希望从集合中得到的对象是按照一定顺序排列的,但是List集合的默认排序方式为按照对象的插入顺序,可以通过java.util.Collections类的静态方法sort( ...
- gradle 集成到myeclipse
新的项目用到gradle,所以学了下,地址:http://dist.springsource.com/release/TOOLS/gradle :help 下,安装好,重启即可,gradle作为mav ...
- 狗血phonegap备忘录[3.3]
phonegap平台就是个狗血的坑,最近的一个项目技术因为上面选型失败,使用了phonegap,加上客户的要求是"像微信一样",真可谓历经坎坷. 基本上评估一个项目是否应该或者可以 ...
- lvchange的available參数
available參数在man info help中均无此參数,事实上參数为:activate 写此此.值得用的人注意. available 參数实为: -a, --activate [a|e|l] ...
- Android用户界面概览
用户界面的概观 全部的Android应用程序的用户界面元素都是用View和ViewGroup对象构建的.View就是在手机屏幕上描绘一个能够与用户交互的一个对象.ViewGroup ...
- ubuntu开机黑屏
以下皆在VM虚拟机下 (1)ctr+alt+f4 进入命令行 (2) sudo apt-get update sudo apt-get install xserver-xorg-lts-quantal ...
- Linux Kernel: buffers和cached的区别
The page cache caches pages of files to optimize file I/O. The buffer cache caches disk blocks to op ...
- iOS 开发-单元测试
前言 维基百科对单元测试的定义如下: 在计算机编程中,单元测试(英语:Unit Testing)又称为模块测试, 是针对程序模块(软件设计的最小单位)来进行正确性检验的测试工作.程序单元是应用的最小可 ...
- Gson解析json数据(转)
一. www.json.org这是JSON的官方网站. 首先,我,我们需要在code.google.com/p/google-gson/downloads/list下载JSON的jar包,解析后把gs ...
- 【教训】rm -fr ./* 教训
昨晚犯了一个重大错误,运行了 rm -rf ./* 本来是要删除一个不重要的目录的,结果在它的父目录下运行了上面命令,结果...都没了... 幸好数据库文件没有被删掉,数据还在,网站程序被删掉了,不久 ...