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. uni-app 如何引入全局方法或变量?

    利用Vue.prototype挂载到Vue实例上即可

  2. Java从零开始学二十五(枚举定义和简单使用)

    一.枚举 枚举是指由一组固定的常量组成的类型,表示特定的数据集合,只是在这个数据集合定义时,所有可能的值都是已知的. 枚举常量的名称建议大写. 枚举常量就是枚举的静态字段,枚举常量之间使用逗号隔开. ...

  3. word2vec模型cbow与skip-gram的比较

    cbow和skip-gram都是在word2vec中用于将文本进行向量表示的实现方法,具体的算法实现细节可以去看word2vec的原理介绍文章.我们这里大体讲下两者的区别,尤其注意在使用当中的不同特点 ...

  4. maven Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12.4

      maven Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12.4 CreateTime--201 ...

  5. 突破单机多实例Elasticsearch

    默认大家都是单机单实例es,在实验环境下想尽可能模拟各种场景.单机多实例就出来了... 实验拓扑图 01.es安装这里就不说了 详情:http://www.cnblogs.com/xiaochina/ ...

  6. ant入门程序

    一. ant简单介绍 Ant是apache的一个核心项目, 它的作用是项目自己主动化构建, 由于它内置了Javac.Java.创建文件夹.拷贝文件等功能, 直接执行build.xml文件就能够编译我们 ...

  7. IBM InfoSphere DataStage 8.1 DataStage Job 开发具体解释

    简单介绍 DataStage 使用了 Client-Server 架构,server端存储全部的项目和元数据,client DataStage Designer 为整个 ETL 过程提供了一个图形化的 ...

  8. PHP 与 UTF-8

    没有一行式解决方案.小心.注意细节,以及一致性. PHP 中的 UTF-8 糟透了.原谅我的用词. 目前 PHP 在低层次上还不支持 Unicode.有几种方式可以确保 UTF-8 字符串能够被正确处 ...

  9. [译]为什么Vue不支持templateURL

    原文链接 Vue的新用户最常问的一个问题,特别是以前使用Angular的用户,是"我可以使用" templateURL吗?这个问题我回答过很多次,现在写一个统一回复. 在Angul ...

  10. RabbitMQ消息队列名词解释[转]

    从AMQP协议可以看出,MessageQueue.Exchange和Binding构成了AMQP协议的核心,下面我们就围绕这三个主要组件    从应用使用的角度全面的介绍如何利用Rabbit MQ构建 ...