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从零开始学八(循环结构)
一.循环结构 循环结构则是根据判断条件的成立与否,决定程序段落的执行次数,而这个程序段落就称为循环主体.
- 3DMax脚本插件--改动材质&贴图名称
从网上淘到了一套人物的模型,当时的心情是激动无比,掏出用的不熟练的3DMax折腾了半天.突然发现了一个蛋疼的事儿,所有的模型文件,材质名称,子材质,以及贴图所实用的是中文命名!! ! 尽管说是能跑,只 ...
- 设置python 命令行交互程序自己主动补齐
1. 新建Python环境变量配置文件: vim ~/.pystartup # Add auto-completion and a stored history file of commands to ...
- Spring Web MVC 原理学习(下)
接着上一篇博客,这一篇.我们依据一个简单的demo,来对SpringMVC的原理再次学习: 一.配置web.xml 我们新建一个web项目.在 ...
- Java 基础【13】 I/O流概念分析整理
转载地址:http://blog.csdn.net/yuebinghaoyuan/article/details/7388059 java.io 中的流,可以从不同的角度进行分类. 按照数据流的方向不 ...
- eclipse could not create the Java Vitual Machine
eclipse could not create the Java Vitual Machine CreateTime--2018年4月27日11:07:15 Author:Marydon 1.情 ...
- 网速变慢解决方法.Tracert与PathPing(转)
Tracert命令与PathPing命令你常用吗: 前段时间本网吧网速不太正常.每晚8点后到11点之间网速爆慢.其余时间则正常.在8~11点间PING电信DNS TIME值要100多MS以上,但PIN ...
- CSS3+JS 实现的便签应用
概述 利用HTML5新增的 locationStorage 实现的便签应用,没有使用 JQuery,主要是为了练习原生JS的使用,采用响应式开发,在手机端和桌面端都有良好的体验,而且使用CSS3添加了 ...
- Android 一个apk多个ICON执行入口
一个工程对应一个AndroidManifest.xml文件,这个文件中包含有该项目的一些设置,如权限.SDk版Activity.Service信息等.一般而言,这个文件中会有且仅有一个applicat ...
- 【laravel5.4】迁移文件的生成、修改、删除
建议直接去官方文档查看: https://laravel-china.org/docs/laravel/5.4/migrations#creating-columns 1.生成迁移: 主要方式:1.创 ...