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. (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>& nums, int target) {
sort(nums.begin(), nums.end());
int sz = nums.size();
vector<vector<int>> ret;
for(int i = ; i < sz; ++i){
if(i != && nums[i] == nums[i - ])
continue;
for(int j = i + ; j < sz; ++j){
if(j > i + && nums[j] == nums[j - ])
continue;
for(int beg = j + , end = sz - ; beg < end;){
while(beg > j + && nums[beg] == nums[beg - ])
beg++;
while(end < sz - && nums[end] == nums[end + ])
end--;
if(beg >= end) break;
int sum = nums[i] + nums[j] + nums[beg] + nums[end];
if(sum == target){
vector<int> tmp;
tmp.push_back(nums[i]);
tmp.push_back(nums[j]);
tmp.push_back(nums[beg]);
tmp.push_back(nums[end]);
ret.push_back(tmp);
beg++, end--;
}else if(sum < target){
beg++;
}else{
end--;
}
}
}
}
return ret;
}
};
LeetCode OJ:4Sum(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 ...
- 九度OJ 1106:数字之和 (基础题)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2505 解决:1706 题目描述: 对于给定的正整数 n,计算其十进制形式下所有位置数字之和,并计算其平方的各位数字之和. 输入: 每行输入 ...
- [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 ...
- [LeetCode] 454. 4Sum II 四数之和II
Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such t ...
- [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 = tar ...
- 【LeetCode】18. 4Sum 四数之和
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:four sum, 4sum, 四数之和,题解,leet ...
- [LeetCode] Sum Root to Leaf Numbers 求根到叶节点数字之和
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...
- Leetcode之深度优先搜索(DFS)专题-129. 求根到叶子节点数字之和(Sum Root to Leaf Numbers)
Leetcode之深度优先搜索(DFS)专题-129. 求根到叶子节点数字之和(Sum Root to Leaf Numbers) 深度优先搜索的解题详细介绍,点击 给定一个二叉树,它的每个结点都存放 ...
- LeetCode 129. 求根到叶子节点数字之和(Sum Root to Leaf Numbers)
题目描述 给定一个二叉树,它的每个结点都存放一个 0-9 的数字,每条从根到叶子节点的路径都代表一个数字. 例如,从根到叶子节点路径 1->2->3 代表数字 123. 计算从根到叶子节点 ...
- Java实现 LeetCode 129 求根到叶子节点数字之和
129. 求根到叶子节点数字之和 给定一个二叉树,它的每个结点都存放一个 0-9 的数字,每条从根到叶子节点的路径都代表一个数字. 例如,从根到叶子节点路径 1->2->3 代表数字 12 ...
随机推荐
- sql server always on 2014安装配置
SQL 2014 AlwaysOn 搭建 原文:SQL 2014 AlwaysOn 搭建 AlwaysOn底层依然采用Windows 故障转移群集的机制进行监测和转移,因此也需要先建立Window ...
- Android studio怎么修改文件名
选中需要重新命名的文件 点击Android studio菜单中列表中的Refactor的选项 选择下拉菜单中的“rename”的选项 弹出rename的选项框,在输入框中输入需要重新的命名的名称. 点 ...
- android学习五---OpenCV for android环境搭建
学习android的目的是想在手机上实现计算机视觉的算法.一般算法的研究都是在Matlab上进行,但是手机平台没有那么多的计算资源,用matlab显然是不太现实的.而OpenCV是基于C++语言编写的 ...
- 解读tensorflow之rnn 的示例 ptb_word_lm.py
这两天想搞清楚用tensorflow来实现rnn/lstm如何做,但是google了半天,发现tf在rnn方面的实现代码或者教程都太少了,仅有的几个教程讲的又过于简单.没办法,只能亲自动手一步步研究官 ...
- python库numpy的reshape的终极解释
a = np.arange(2*4*4) b = a.reshape(1,4,4,2) #应该这样按反序来理解:最后一个2是一个只有2个元素的向量,最后的4,2代表4×2的矩阵,最 ...
- django安装与卸载
安装 D:\Django-1.8.2>python setup.py installrunning install Installed d:\python27\lib\site-packages ...
- C# 建立UDP服务器并接收客户端数据
C# 建立UDP服务器并接收客户端数据 2015-02-11 17:20 1218人阅读 评论(0) 收藏 举报 分类: C#开发技术(22) 版权声明:本文为博主原创文章,未经博主允许不得转载. ...
- mysqlbinlog作用
mysqlbinlog:解析mysql的binlog日志 在 mysql-bin.index里面记录了所有的binlog文件,它是一个索引 binlog日志的作用:用来记录mysql内部增删改查等对m ...
- HDU - 1695 GCD (容斥+枚举)
题意:求区间1<=i<=b与区间1<=j<=d之间满足gcd(i,j) = k 的数对 (i,j) 个数.(i,j)与(j,i) 算一个. 分析:gcd(i,j)=k可以转化为 ...
- SqlHelper简单实现(通过Expression和反射)8.Sql Server数据处理类
这个类基本上就是调用EntityHelper,ExpressionHelper和ObjectHelper来进行各种完整SQL的拼接操作. using System; using System.Conf ...