LintCode: 3 Sum
C++
把3个数求和,转变为2个数求和
1. 把数组排序
2. 注意过滤重复值
3. 从前到后遍历,游标i
4. 从后边数中找start + end = -arr[i]的2 sum
5. start + end < -arr[i], start++
6. start + end > -arr[i], end--
7. start + end = -arr[i], insert <i, start, end> into result vecotr
class Solution {
public:
/**
* @param numbers : Give an array numbers of n integer
* @return : Find all unique triplets in the array which gives the sum of zero.
*/
vector<vector<int> > threeSum(vector<int> &nums) {
// write your code here
vector<vector<int> > result;
sort(nums.begin(), nums.end());
for (int i = ; i < nums.size(); i++) {
if (i > && nums[i] == nums[i - ]) {
continue;
}
// two sum;
int start = i + , end = nums.size() - ;
int target = -nums[i];
while (start < end) {
if (start > i + && nums[start - ] == nums[start]) {
start++;
continue;
}
if (nums[start] + nums[end] < target) {
start++;
} else if (nums[start] + nums[end] > target) {
end--;
} else {
vector<int> triple;
triple.push_back(nums[i]);
triple.push_back(nums[start]);
triple.push_back(nums[end]);
result.push_back(triple);
start++;
}
}
}
return result;
}
};
LintCode: 3 Sum的更多相关文章
- lintcode: k Sum 解题报告
K SUM My Submissions http://www.lintcode.com/en/problem/k-sum/ 题目来自九章算法 13% Accepted Given n distinc ...
- LintCode "4 Sum"
4 Pointer solution. Key: when moving pointers, we skip duplicated ones. Ref: https://github.com/xbz/ ...
- Lintcode: Subarray Sum 解题报告
Subarray Sum 原题链接:http://lintcode.com/zh-cn/problem/subarray-sum/# Given an integer array, find a su ...
- LintCode Subarray Sum
For this problem we need to learn a new trick that if your start sum up all elements in an array. Wh ...
- [LintCode] Two Sum 两数之和
Given an array of integers, find two numbers such that they add up to a specific target number. The ...
- [LintCode] Submatrix Sum 子矩阵之和
Given an integer matrix, find a submatrix where the sum of numbers is zero. Your code should return ...
- Lintcode: Interval Sum II
Given an integer array in the construct method, implement two methods query(start, end) and modify(i ...
- Lintcode: Interval Sum
Given an integer array (index from 0 to n-1, where n is the size of this array), and an query list. ...
- Lintcode: Subarray Sum Closest
Given an integer array, find a subarray with sum closest to zero. Return the indexes of the first nu ...
- LintCode "Submatrix Sum"
Naive solution is O(n^4). But on 1 certain dimension, naive O(n^2) can be O(n) by this well-known eq ...
随机推荐
- Linux安装ImageMagick与JMagick完成过程及配置
原文地址:http://www.iitshare.com/linux-install-imagemagick-jmagick.html 一.安装背景 最近在服务器上部署了HapiCMS的产品,因为其对 ...
- Android Initializing a Build Environment
from://https://source.android.com/source/initializing.html#next-download-the-source Initializing a B ...
- python测试开发django-38.多对多(ManyToManyField)查询
前言 一个学生可以对应多个老师,一个老师也可以教多个学生,这就是一种多对多的关系 models建表 新建一个老师表Teacher,和一个学生表Student class Teacher(models. ...
- Android 百度地图开发(三)
实现比例尺功能和替换自带的缩放组件 ScaleView是比例尺控件.ZoomControlView是缩放控件,MainActivity就是我们的主界面了 先看下ZoomControlView类.代码例 ...
- 深入浅出!从语义角度分析隐藏在Unity协程背后的原理
Unity的协程使用起来比较方便,但是由于其封装和隐藏了太多细节,使其看起来比较神秘.比如协程是否是真正的异步执行?协程与线程到底是什么关系?本文将从语义角度来分析隐藏在协程背后的原理,并使用C++来 ...
- 腾讯Bugly2015年移动应用质量大数据报告 原 荐
在这份报告中,腾讯Bugly和腾讯优测会对2015年Android和iOS平台上的应用质量进行详细盘点,帮助你了解你的产品质量在行业中处于什么位置. 首先,让我们从整体上,回顾一下2015年度的应用和 ...
- Eclipse国内镜像源配置方法
Table of Contents 我们在国内从官网下载Eclipse以及插件非常慢,那么,有没有方法变快呢? 有,那就是使用国内的公开镜像源替换官方源. 1 下载Eclipse 首先,我们看一个链接 ...
- Martin Fowler谈微服务的优缺点
很多开发团队已经认识到微服务架构比单体架构更优越.但是也有其他团队感觉到这是一种消弱生产力的负担.就像任何软件架构,微服务架构同样有利弊.为了能做出一个明智的选择,你必须了解这些应用并将它们运用到你特 ...
- 迪米特法则(Law Of Demeter)
定义:一个对象应该对其他对象保持最少的了解. 问题由来:类与类之间的关系越密切,耦合度越大,当一个类发生改变时,对另一个类的影响也越大. 解决方案:尽量降低类与类之间的耦合. 自从我们接触编程开始,就 ...
- Swift:宏定义
一.简述 swift中并没有加入宏系统,C语言使用#define定义的基本常量在导入Swift时被Swift编译自动转为Swfit语言的全局变量.但复杂的宏定义不能被Swift转换.Swift中类似宏 ...