57. 3Sum【medium】
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.
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.
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】的更多相关文章
- 2. Add Two Numbers【medium】
2. Add Two Numbers[medium] You are given two non-empty linked lists representing two non-negative in ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 75. Find Peak Element 【medium】
75. Find Peak Element [medium] There is an integer array which has the following features: The numbe ...
- 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 ...
- 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 ...
随机推荐
- Java从零开始学七(选择结构)
一. 程序的结构: 一般来说程序的结构包含有下面三种: 1.顺序结构 2.选择结构 3.循环结构 二.顺序结构 程序至上而下逐行执行,一条语句执行完之后继续执行下一条语句,一直到程序的末尾
- 算法笔记_189:历届试题 横向打印二叉树(Java)
目录 1 问题描述 2 解决方案 1 问题描述 问题描述 二叉树可以用于排序.其原理很简单:对于一个排序二叉树添加新节点时,先与根节点比较,若小则交给左子树继续处理,否则交给右子树. 当遇到空子树 ...
- python中的sort、sorted、reverse、reversed详解
python语言中的列表排序方法有三个:reverse反转/倒序排序.sort正序排序.sorted可以获取排序后的列表.在更高级列表排序中,后两中方法还可以加入条件参数进行排序. reverse() ...
- OpenERP 负载平衡
OpenERP 7.0 带来了许多新特性,架构上也有许多改进.其中可配置 worker 参数,可使 OpenERP 运行在多进程模式,突破GIL的限制,有效利用了现代多核CPU的性能.但默认情况下,O ...
- Linux下监视NVIDIA的GPU使用情况(转)
在使用TensorFlow跑深度学习的时候,经常出现显存不足的情况,所以我们希望能够随时查看GPU时使用率.如果你是Nvidia的GPU,那么在命令行下,只需要一行命令就可以实现. 1. 显示当前GP ...
- 【LeetCode】130. Surrounded Regions (2 solutions)
Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A ...
- Linux 常用脚本
Linux 常用脚本 修改表列属性 sql可任意修改,若数据库正好在执行机器上,可去掉ip地址 echo 为输出 #!/bin/shfor((i=0;i<256;i++));do ...
- SVN标准开发布局目录,trunk,branches,tags用法详解
http://www.cnblogs.com/newstar/archive/2011/01/04/svn.html 关于 SVN 目录结构 Subversion有一个很标准的目录结构,是 ...
- window下删除所有带.svn文件夹及文件,删除所有的.svn文件夹
(一)------------------------------------------------------------------------------------------------- ...
- centos(7) 使用yum进行安装lamp环境
1.用yum安装Apache,Mysql,PHP. 1.1安装Apache yum install httpd httpd-devel 安装完成后,用service httpd start 1.2 安 ...