three Sum
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.
class Solution {
public:
vector<vector<int> > threeSum(vector<int> &num) {
vector<int> NumCopy(num);
vector<vector<int> > returnvct;
sort(NumCopy.begin(),NumCopy.end());
int m=,cnt=;
while(NumCopy[m]<)
++m;
int f = m,j=m;
while(NumCopy[f] == ){
++f;
++cnt;
}
if(cnt>=){
f = m+cnt-;
j = m+cnt-;
}
for(int i=;f<NumCopy.size();++f){
int tempi =i,tempj =j;
int sum = - * (NumCopy[i]+NumCopy[j]);
while( sum > NumCopy[f]&& i<j ){
++i;
sum = -* (NumCopy[i]+ NumCopy[j]);
}
if(sum == NumCopy[f]){
vector<int> tmp;
tmp.push_back(i);
tmp.push_back(j);
tmp.push_back(f);
returnvct.push_back(tmp);
} while(sum < NumCopy[f] && i<j){
--j;
sum = - * (NumCopy[i]+ NumCopy[j]);
}
i = tempi;
j= tempj;
}
return returnvct;
}
};
利用昨天twoSum的方法 将a+b+c =0 变成 a+b =-c;数组 以0为界限 分为前后两部分。
可是又超时了::>_<:: 。
three 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 ...
- [LeetCode] Combination Sum IV 组合之和之四
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
随机推荐
- ASP.NET 使用AJAX让GridView的数据行显示提示框(ToolTip)
介绍ASP.NET AJAX可以使你的web应用程序具有更丰富的功能和更多的用户响应. 本文中,我将演示如何通过ASP.NET AJAX的帮助,给像GridView这样的数据绑定控件的数据行增加pop ...
- WinForm设置右键菜单
上一篇:设置窗体透明C#代码 如果要实现MDI窗体的右键菜单,可以添加一个contextMenuStrip(ID设置为:contextMenuStrip_hewenqi),然后设置窗体的Context ...
- net 数据库连接详解 相当经典啊
ADO.NET与抽水的故事 ADO.NET是微软新一代.NET数据库的访问架构,ADO是ActiveX Data Objects的缩写.ADO.NET是数据库应用程序和数据源之间沟通的桥梁,主要提供一 ...
- 客户端(Winform窗体)上传文件到服务器(web窗体)简单例子
客户端:先创建一个winform窗体的应用程序项目 项目结构
- [moka摘录]查看邮件是否已被阅读
原文地址:http://www.php100.com/html/php/hanshu/2013/1101/6347.html 查看邮件是否已被阅读 当你在发送邮件时,你或许很想知道该邮件是否被对方已阅 ...
- Spring RMI Example
一: 提供服务的远程一端 1-1. applicationContext.xml <?xml version="1.0" encoding="UTF-8" ...
- SQL Server SQL语句执行顺序
执行顺序: 1.FROM:对FROM子句中前两个表执行笛卡尔积生成虚拟表vt1 2.ON:对vt1表应用ON筛选器只有满足 为真的行才被插入vt2 3.OUTER(join):如果指定了 OUTER ...
- dapper的增、删、查改的CodeSmith模板
<%@ Template Language="C#" TargetLanguage="Text" %> <%@ Property Name=& ...
- A除以B问题
描述:本题要求计算A/B,其中A是不超过1000位的正整数,B是1位正整数.你需要输出商数Q和余数R,使得A = B * Q + R成立. 输入:输入在1行中依次给出A和B,中间以1空格分隔. 输出: ...
- RHEL7虚拟机实验快照
配置虚拟机连接网络 首先确保NetworkManager服务正常运行 [root@administrator ~]# systemctl status NetworkManager ● Network ...