Given an array S of n integers, are there elements abc in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.

Notice

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.

 
Example

For example, given array S = {-1 0 1 2 -1 -4}, A solution set is:

(-1, 0, 1)
(-1, -1, 2)

题意

给出一个有n个整数的数组S,在S中找到三个整数a, b, c,找到所有使得a + b + c = 0的三元组。

在三元组(a, b, c),要求a <= b <= c。结果不能包含重复的三元组。

解法一:

 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) {
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++;
end--;
}
}
} return result;
}
};

解法二:

 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> &numbers) {
vector<vector<int>> ret; int n = numbers.size();
sort(numbers.begin(), numbers.end()); for (int i = ; i < n - ; ++i) {
if (i != && numbers[i] == numbers[i-]) {
continue;
} int sum = -numbers[i];
int j = i + , k = n - ; while (j < k) {
int tmp = numbers[j] + numbers[k];
if (tmp == sum) {
vector<int> sol{numbers[i], numbers[j], numbers[k]};
ret.push_back(sol);
while (j < k && numbers[j] == numbers[j+]) {
j++;
}
while (j < k && numbers[k] == numbers[k-]) {
k--;
}
j++;
k--;
} else if (tmp > sum) {
k--;
} else {
j++;
}
}
}
return ret;
}
};

57. 3Sum【medium】的更多相关文章

  1. 2. Add Two Numbers【medium】

    2. Add Two Numbers[medium] You are given two non-empty linked lists representing two non-negative in ...

  2. 92. Reverse Linked List II【Medium】

    92. Reverse Linked List II[Medium] Reverse a linked list from position m to n. Do it in-place and in ...

  3. 82. Remove Duplicates from Sorted List II【Medium】

    82. Remove Duplicates from Sorted List II[Medium] Given a sorted linked list, delete all nodes that ...

  4. 61. Search for a Range【medium】

    61. Search for a Range[medium] Given a sorted array of n integers, find the starting and ending posi ...

  5. 62. Search in Rotated Sorted Array【medium】

    62. Search in Rotated Sorted Array[medium] Suppose a sorted array is rotated at some pivot unknown t ...

  6. 74. First Bad Version 【medium】

    74. First Bad Version [medium] The code base version is an integer start from 1 to n. One day, someo ...

  7. 75. Find Peak Element 【medium】

    75. Find Peak Element [medium] There is an integer array which has the following features: The numbe ...

  8. 159. Find Minimum in Rotated Sorted Array 【medium】

    159. Find Minimum in Rotated Sorted Array [medium] Suppose a sorted array is rotated at some pivot u ...

  9. Java for LeetCode 207 Course Schedule【Medium】

    There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...

随机推荐

  1. Phpcms没有找到网址列表

    今天在搞phpcms的采集遇到了这个问题. 没有找到网址列表,请先进行网址采集. 百度,google了好久: 网上答案: 方案1 1.尝试清除 v9_collection_history 表里的内容 ...

  2. 服务名无效。请键入 NET HELPMSG 2185 以获得更多的帮助。

    1:关闭MySQL服务的时候出现“服务名无效.请键入 NET HELPMSG 2185 以获得更多的帮助.”错误. 2:查看服务名称 3:重新关闭服务 4:管理员运行控制台后重新执行 成功停掉了.

  3. dyld: Library not loaded: @rpath/XCTest.framework/XCTest Referenced from: /private/var/mobile/Conta

    dyld: Library not loaded: @rpath/XCTest.framework/XCTest   Referenced from: /private/var/mobile/Cont ...

  4. [Unity-1] Unity简单介绍

    Unity是一套包含图形.声音.物理等功能的游戏引擎,提供了一个强大的关卡编辑器.支持大部分主流3D软件格式,使用C#或者JavaScript等高级语言实现脚本功能.使开发人员无需了解底层复杂技术,高 ...

  5. grid++ 开发教程

      grid++ 开发教程 CreateTime--2018年4月24日18:43:23 Author:Marydon 1.插入报表头.明细网格.报表尾 右键-->插入-->报表节--&g ...

  6. SettingsEclipse&MyEclipse

      eclipse优化 迁移时间--2017年5月20日09:39:16 CreateTime--2016年11月18日11:27:02 Author:Marydon ModifyTime--2017 ...

  7. 【Java虚拟机】运行时数据区

    Java在执行Java程序的过程中会把它所管理的内存划分为若干个不同的数据区域.这些区域都有各自的用途.创建和销毁的时间,有一些是随虚拟机的启动而创建,随虚拟机的退出而销毁,有些则是与线程一一对应,随 ...

  8. OpenWrt的开机启动服务(init scripts)

    参考 https://wiki.openwrt.org/doc/techref/initscripts 以一个简单的例子来说明 #!/bin/sh /etc/rc.common # Example s ...

  9. MongoDB: 聚集管道

    在MongoDB2.2新出现的. 聚集管道式基于数据处理管道概念建模的数据聚集框架.文档进入一个多阶段能将该文档转化为聚集结果的管道. 聚集管道提供了map-reduce方法了替代物,并在非常多聚集任 ...

  10. WinForm如何调用Web Service

    参考地址 今天看了李天平关于WinForm调用Web Service的代码,我自己模仿做一个代码基本都是复制粘贴的,结果不好使.郁闷的是,又碰到那个该死的GET调用Web Service,我想肯定又是 ...