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的更多相关文章

  1. lintcode: k Sum 解题报告

    K SUM My Submissions http://www.lintcode.com/en/problem/k-sum/ 题目来自九章算法 13% Accepted Given n distinc ...

  2. LintCode "4 Sum"

    4 Pointer solution. Key: when moving pointers, we skip duplicated ones. Ref: https://github.com/xbz/ ...

  3. Lintcode: Subarray Sum 解题报告

    Subarray Sum 原题链接:http://lintcode.com/zh-cn/problem/subarray-sum/# Given an integer array, find a su ...

  4. 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 ...

  5. [LintCode] Two Sum 两数之和

    Given an array of integers, find two numbers such that they add up to a specific target number. The ...

  6. [LintCode] Submatrix Sum 子矩阵之和

    Given an integer matrix, find a submatrix where the sum of numbers is zero. Your code should return ...

  7. Lintcode: Interval Sum II

    Given an integer array in the construct method, implement two methods query(start, end) and modify(i ...

  8. 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. ...

  9. Lintcode: Subarray Sum Closest

    Given an integer array, find a subarray with sum closest to zero. Return the indexes of the first nu ...

  10. 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 ...

随机推荐

  1. 报错:无法截断表 '某表',因为该表正由 FOREIGN KEY 约束引用

    某表的某个字段作为另一个表的FOREIGN KEY,在truncate另外一个表后,再truncate某表,就报如上的错. 解决方法: → 删除另外一个表的外键 IF OBJECT_ID(N'[dbo ...

  2. c++中 extern

    用例子给你示范 // 1.cpp int x = 10; // 2.cpp 注意没有包含1.cpp #include <iostream> using namespace std; ext ...

  3. Linux的命名空间详解--Linux进程的管理与调度(二)

    转自:http://blog.csdn.net/gatieme/article/details/51383322 日期 内核版本 架构 作者 GitHub CSDN 2016-05-12 Linux- ...

  4. mysql递归查询子类ID查询所有子类

    先来看数据表的结构如下: id  name    parent_id  ---------------------------  1   Home        0  2   About        ...

  5. zoj2334 Monkey King , 并查集,可并堆,左偏树

    提交地址:点击打开链接 题意:  N(N<=10^5)仅仅猴子,初始每仅仅猴子为自己猴群的猴王.每仅仅猴子有一个初始的力量值.这些猴子会有M次会面. 每次两仅仅猴子x,y会面,若x,y属于同一个 ...

  6. Asp.Net 管道事件注册/HttpApplication事件注册

    一.HttpApplication简介 在HttpRuntime创建了HttpContext对象之后,HttpRuntime将随后创建一个用于处理请求的对象,这个对象的类型为HttpApplicati ...

  7. 层次聚类 Hierarchical Clustering

    -------------------------------- 不管是GMM,还是k-means,都面临一个问题,就是k的个数如何选取?比如在bag-of-words模型中,用k-means训练码书 ...

  8. 【转】搜狗开源内部项目管理平台Cynthia意欲何为

    FROM : http://blog.csdn.net/dj0379/article/details/38356825 目前,在项目管理与缺陷管理系统上,中国的中小开发团队基本都在使用国外产品,在理念 ...

  9. JAVA中String.format的用法 转16进制,还可以补0

    1.对整数进行格式化:%[index$][标识][最小宽度]转换方式        我们可以看到,格式化字符串由4部分组成,其中%[index$]的含义我们上面已经讲过,[最小宽度]的含义也很好理解, ...

  10. DBS-Tally book(记账本)

    ylbtech-dbs:DBS-Tally book(记账本) -- =============================================-- 记账本-- 模仿小程序“记账e”业 ...