LeetCode——3Sum
1. Question
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.
Note: 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]
]
2. Solution
先排序,然后对于每一个nums[i],在其后找到两个数字num[j]和num[k],j<k,使得三者和为0。
去掉重复的组合,如果满足三者为0,那么j需要后移到一个不同的数,k也需要前移到一个不同的数。
重复的nums[i]也需要去掉,因为nums[i]和nums[i+1] 都是和其后的进行组合,如果两个一样的话,就重复了,需要去掉。
3. Code
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
if (nums.size() <= 2)
return vector<vector<int>>();
sort(nums.begin(), nums.end());
vector<vector<int>> res;
for (int i = 0; i < nums.size() - 2; i++) {
int start = i + 1;
int end = nums.size() - 1;
while (start < end) {
int tmp = nums[start] + nums[end];
int target = -nums[i];
if (tmp == target) {
vector<int> ele;
ele.push_back(nums[i]);
ele.push_back(nums[start]);
ele.push_back(nums[end]);
res.push_back(ele);
// 去掉start重复的
while (start < end && nums[start] == ele[1])
start++;
// 去掉end重复的
while (end > start && nums[end] == ele[2])
end--;
} else if (tmp > target)
end--;
else
start++;
}
// 去掉nums[i]重复的
while (i + 1 < nums.size() - 2 && nums[i + 1] == nums[i])
i++;
}
return res;
}
};
LeetCode——3Sum的更多相关文章
- [LeetCode] 3Sum Smaller 三数之和较小值
Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 < ...
- [LeetCode] 3Sum Closest 最近三数之和
Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...
- [LeetCode] 3Sum 三数之和
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...
- LeetCode 3Sum Smaller
原题链接在这里:https://leetcode.com/problems/3sum-smaller/ 题目: Given an array of n integers nums and a targ ...
- leetcode — 3sum
import java.util.*; /** * Source : https://oj.leetcode.com/problems/3sum/ * * Created by lverpeng on ...
- LeetCode: 3Sum
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...
- LeetCode:3Sum, 3Sum Closest, 4Sum
3Sum Closest Given an array S of n integers, find three integers in S such that the sum is closest t ...
- Leetcode 3Sum Closest
Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...
- leetcode—3sum
1.题目描述 Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find ...
- Leetcode 3Sum Closet
二手和3Sum像几乎相同的想法.二进制搜索.关键修剪.但是,在修剪做出很多错误. 然后还有一个更加速了原来的想法O(n^2). #include<iostream> #include &l ...
随机推荐
- /proc/iomem和/proc/ioports对应的fops
/proc/iomem和/proc/ioports对应的fops static int __init ioresources_init(void) { struct proc_dir_entr ...
- 【开发者笔记】归并排序过程呈现之java内置GUI表示
在网上看到一个视频将各种排序用视频表示出来,配上音乐,挺好玩的样子,就算是不会编程的人看到也会觉得很舒服,碰巧我也正在写归并算法,于是就用java的GUI实现一个. 归并排序的时间复杂度是T(n)=O ...
- abap编辑器中代码不可修改
当出现abap编辑器中代码不能修改的情况,可以按如下设置: edit——> 修改操作——>关闭助手
- webuploader 多图片上传
WebUploader是由Baidu WebFE(FEX)团队开发的一个简单的以HTML5为主,FLASH为辅的现代文件上传组件. 具体接口参考 webuploader接口文档地址 一.图片上传功能 ...
- kettle转换添加hbase-output无法获取hbase表
问题:kettle转换添加hbase-output无法获取hbase表 win本机安装kettle,本机虚拟机三台,root用户安装有jdk.zookeeper.hadoop.hbase kettle ...
- 宏表达式与函数、#undef、条件编译、
宏表达式在预编译期被处理,编译器不知道宏表达式的存在. 宏表达式没有任何的调用开销 宏表达式中不能出现递归定义. C语言中强大的内置宏 __FILE__:被编译的文件名 //双底线 __LINE__: ...
- C的指针疑惑:C和指针17(经典抽象数据类型)
堆栈这种数据最鲜明的特点是:后进先出. 用动态数组实现堆栈: #include "C17.h" #include <stdio.h> #include <stdl ...
- SpringMVC的其他功能使用
一.SpringMVC支持在控制器的业务方法中写入参数作为传递过来的变量 @Controller @RequestMapping(value="/kaiye") public cl ...
- mysql基础测试
mysql基础测试 测试原因 为什么需要做性能测试 模拟比当前系统更高的负载,找出性能瓶颈 重现线上异常 测试不同硬件软件配置 规划未来的业务增长 测试分类 性能测试的分类 设备层的测试 ...
- [转]Algolia的分布式搜索网络架构
转自:http://www.csdn.net/article/2015-03-11/2824176-the-architecture-of-algolias-distributed-search-ne ...