LeetCode----3 Sum
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:
- 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)
分析:
这道题原本不难,但是题目要求结果不能够包含重复的 triplets, 往往容易出错。
思路1:
step 1: 先排序
step 2: 枚举前两个数,如果要满足 sum = 0, 那么第三个数必须是 前两个数和的负数。
通过二分搜索去查找。。复杂度 是 O(N^2 * lgN)
思路2:
O(N^2)的算法。
借助于 2 sum的思路:如果在一个已经排序的数组中,寻找两个数的和为给定的sum, 其实有 O(N)的算法。
指针 i, j 分别指向数组的首尾,如果 num[i] + num[j] < 0, 则 i++; 如果 > 0, j--; 直到和等于sum.
因此,本题可以
step 1: 排序
step 2: 先枚举第一个数,然后在排好序的数组中寻找 两个数和等于 第一个数的负数。
Note: 为了避免重复的 triplets, 有一些小细节需要考虑。具体见下面的代码。
基本的思想就是 如果碰到相邻的数相同,可以跳过。
class Solution {
public:
vector<vector<int> > threeSum(vector<int> &num) {
vector<vector<int> > result;
if(num.size() < 3) return result;
sort(num.begin(), num.end());
//处理以num[k]开头的和
for(int k=0; k<num.size()-2; ++k){
if(k > 0 && num[k] == num[k-1]) continue; //避免重复
int i=k+1, j=num.size()-1; //分别指向首尾
while(i < j){
if(i > k+1 && num[i] == num[i-1]){
++i;
continue;
}
if(j < num.size()-1 && num[j] == num[j+1]){
--j;
continue;
}
if(num[k] + num[i] + num[j] < 0)
++i;
else if(num[k] + num[i] + num[j] > 0)
--j;
else{
result.push_back(vector<int>{num[k], num[i++], num[j--]});
}
}
}
return result;
}
};
LeetCode----3 Sum的更多相关文章
- LeetCode:Path Sum I II
LeetCode:Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such ...
- 剑指offer 65. 不用加减乘除做加法(Leetcode 371. Sum of Two Integers)
剑指offer 65. 不用加减乘除做加法(Leetcode 371. Sum of Two Integers) https://leetcode.com/problems/sum-of-two-in ...
- [LeetCode] Path Sum III 二叉树的路径和之三
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
- [LeetCode] Combination Sum IV 组合之和之四
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- [LeetCode] Max Sum of Rectangle No Larger Than K 最大矩阵和不超过K
Given a non-empty 2D matrix matrix and an integer k, find the max sum of a rectangle in the matrix s ...
- [LeetCode] Range Sum Query 2D - Mutable 二维区域和检索 - 可变
Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...
- [LeetCode] Range Sum Query - Mutable 区域和检索 - 可变
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...
- [LeetCode] Range Sum Query 2D - Immutable 二维区域和检索 - 不可变
Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...
- [LeetCode] Range Sum Query - Immutable 区域和检索 - 不可变
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...
- [LeetCode] Combination Sum III 组合之和之三
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
随机推荐
- cf------(round 2)A. Winner
A. Winner time limit per test 1 second memory limit per test 64 megabytes input standard input outpu ...
- Anagrams [LeetCode]
Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be ...
- ScrollView嵌套StackView提示需要宽度和高度限制
场景: 在一个xib的view中,添加一个ScrollView,再在这个ScrollView中添加一个StackView,StackView中不加控件(用代码动态加). 问题: 提示ScrollVie ...
- Play framework 2.0 -应用程序全局设置(转)
转载自: http://shenbai.iteye.com/blog/1517366 1.全局对象 在工程中定义全局对象可以允许你操作你的应用程序的全局设置.这个全局对象必须定义在根包下. impor ...
- gitlab配置邮件通知
配置用户提交评论.添加issue等的邮件通知: Gitlab邮件提醒方便跟踪项目进度,在这里介绍两种方式,一种是用系统的sendmail发送邮件,另一种是GMAIL的stmp来发送邮件 第一种 用系统 ...
- jQuery.Autocomplete实现自动完成功能(详解)
1.jquery.autocomplete参考地址 http://bassistance.de/jquery-plugins/jquery-plugin-autocomplete/ http://do ...
- Servlet+JSP+JavaBean开发模式(MVC)介绍
好伤心...写登陆注册之前看见一篇很好的博文,没有收藏,然后找不到了. 前几天在知乎上看见一个问题,什么时候感觉最无力. 前两天一直想回答:尝试过google到的所有solve case,结果bug依 ...
- 为什么你总是学不好Linux技术?这是我的答案。
摘要: 我们为什么要学习Linux,最近几年Linux发展迅速,特别服务器领域,带来了很多新技术,云计算,虚拟化,大数据等技术,还有安全方面都有了很大的发展同时也给了Linux运维工作带来了,更多的要 ...
- bzoj 1856: [Scoi2010]字符串
#include<cstdio> #include<iostream> #define Q 20100403 ; int main() { scanf("%lld%l ...
- ros与下位机通信常用的c++ boost串口应用
一.首先移植c++ boost 库: 1. 先去 Boost官网 下载最新的Boost版本, 我下载的是boost_1_6_0版本, 解压. 2. 进入解压后目录: cd boost_1_6_0, 执 ...