【3Sum】cpp
题目:
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.
Note:
- Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c)
- The solution set must not contain duplicate triplets.
For example, given array S = {-1 0 1 2 -1 -4},
A solution set is:
(-1, 0, 1)
(-1, -1, 2)
代码:
class Solution {
public:
vector<vector<int> > threeSum(vector<int> &num) {
std::vector<std::vector<int> > ret_vector;
if (num.size() < )
{
return ret_vector;
}
// sort the vector
std::sort(num.begin(), num.end());
// visit all the left side element of the current element
const int target = ;
std::vector<int>::iterator end = num.end();
for (std::vector<int>::iterator i = num.begin(); i != end-; ++i){
// ignore first duplicate i
if ( i > num.begin() && *i==*(i-)) continue;
std::vector<int>::iterator j = i+;
std::vector<int>::iterator k = end-;
while (j<k){
const int tmp = *i+*j+*k;
if ( tmp < target ){
++j;
while ( *j==*(j-) && j<k ) ++j;
}
else if ( tmp > target ){
--k;
while ( *k==*(k+) && j<k ) --k;
}
else{
int tmp_triple[] = {*i,*j,*k};
std::vector<int> tmp_vector(tmp_triple,tmp_triple+);
ret_vector.push_back(tmp_vector);
++j;
--k;
while( *j==*(j-) && *k==*(k+) && j<k ) {
++j;
--k;
}
}
}
}
return ret_vector;
}
};
Tips:
1. 先对传入的vector排序,然后从前向后遍历。原则是:包含*i元素,且满足条件的triple都加入到返回结果中
2. 这里比较困难的一点是排除重复的triple,大概想一想原则,一些极端的case只能试验出来了
另,在mac上编辑的,不知道为什么用数组初始化vector编译不通过,所以采用了比较麻烦的传参方式
===============================================
第二次过这道题,大体思路记得不太清,重新学习了一下“排序+双指针夹逼”方法
参考的blog:http://www.cnblogs.com/tenosdoit/p/3649607.html
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
vector<vector<int> > ret;
if ( nums.size()< ) return ret;
std::sort(nums.begin(), nums.end());
for ( int i=; i<nums.size()-; ++i )
{
// skip the duplicates elements
if ( i> && nums[i]==nums[i-] ) continue;
Solution::TwoSum(ret, nums, i+, -nums[i]);
}
return ret;
}
static void TwoSum(vector<vector<int> >& ret, vector<int>& nums, int begin, int target)
{
vector<int> tmp;
int start = begin;
int end = nums.size()-;
while ( begin<end )
{
if ( nums[begin]+nums[end]<target ){
begin++;
}
else if ( nums[begin]+nums[end]>target ){
end--;
}
else
{
// add a triple
tmp.push_back(nums[start-]);
tmp.push_back(nums[begin]);
tmp.push_back(nums[end]);
ret.push_back(tmp);
tmp.clear();
// skip the duplicate elements
begin++;
while ( begin<end && nums[begin]==nums[begin-] ) begin++;
end--;
while ( begin<end && nums[end]==nums[end+] ) end--;
}
}
}
};
tips:
1. 先对数组进行排序
2. 固定一个元素,从其后面元素开始,采用两头夹逼的方式进行遍历。(为什么要两边夹逼?因为元素已经从小到大排序好了,向后移动begin可以增加两个元素的和,向前移动end可以减小两个元素的和;采用这样的方式,相当于进可往最大和靠,退可往最小值收,可以保证不漏)
3. 如果遇到重复的元素要跳过去,因为题中要求不能出现重复的triple。
【3Sum】cpp的更多相关文章
- 【4Sum】cpp
题目: Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = ...
- 【Permutations】cpp
题目: Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the fo ...
- 【Subsets】cpp
题目: Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a subset ...
- 【Anagrams】 cpp
题目: Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will ...
- 蓝桥杯 【dp?】.cpp
题意: 给出一个2*n的方格,当刷完某一个方格的漆后可以且只可以走到相邻的任何一格,即上 下 左 右 左上 左下 右上 右下.可以从任意一个格子开始刷墙,问有多少种刷法,因为随着n的增大方案数会变多, ...
- 【Triangle 】cpp
题目: Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjace ...
- 【N-Queens】cpp
题目: The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two que ...
- 【Combinations】cpp
题目: Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For ex ...
- 【Candy】cpp
题目: There are N children standing in a line. Each child is assigned a rating value. You are giving c ...
随机推荐
- Centos install ICU, INTL for php
1. Install ICU from source wget http://download.icu-project.org/files/icu4c/56.1/icu4c-56_1-src.tgz ...
- 利用Python实现 队列的算法
以下内容都是来自“悟空“大神的讲解,听他的视频课,利用Python实现堆栈的算法,自己做了一些小总结,可能会存在自己理解的一些误区, 1.栈的实现 队列的特征是先进先出,同我们生活中的队列具有相同的特 ...
- PMBOK(第六版) PMP笔记——第十章(项目沟通管理)
PM 大多数时间都用在与干系人的沟通上.第十章有三个过程: 规划沟通管理:根据干系人的需求,制定沟通管理计划管理沟通:根据沟通管理计划发布.收集.处理信息监督沟通:确保在正确时间将正确信息传递给正确的 ...
- CUDA:Supercomputing for the Masses (用于大量数据的超级计算)-第九节
原文链接 第九节:使用CUDA拓展高等级语言 Rob Farber 是西北太平洋国家实验室(Pacific Northwest National Laboratory)的高级科研人员.他在多个国家级的 ...
- C# 运用StreamReader类和StreamWriter类实现文件的读写操作
对文件的读写操作应该是最重要的文件操作,System.IO命名空间为我们提供了诸多文件读写操作类,在这里我要向大家介绍最常用也是最基本的StreamReader类和StreamWriter类.从这两个 ...
- 1046: [HAOI2007]上升序列
Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 5822 Solved: 2071[Submit][Status][Discuss] Descript ...
- PAT 乙级 1003
题目 题目地址:PAT 乙级 1003 题解 规律观察题,本题的关键在于把题读懂,同时还有几个比较容易疏忽的地方需要注意:总之这道题要考虑的东西更多,细节上也要特别注意: 规律:“如果 aPbTc 是 ...
- WireShark抓包命令
本机环回包 在进行通信开发的过程中,我们往往会把本机既作为客户端又作为服务器端来调试代码,使得本机自己和自己通信.但是wireshark此时是无法抓取到数据包的,需要通过简单的设置才可以. 具体方法如 ...
- 52shaidan.net
52shaidan.net 52gendan.net 朋友的域名
- Cisco交换机与路由器命令总结
1.查看信息 show version 查看版本及引导信息 show running-config 查看运行设置 show startup-config 查看开机设置 show ...