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 ...
随机推荐
- [Hadoop 周边] 浅谈大数据(hadoop)和移动开发(Android、IOS)开发前景【转】
原文链接:http://www.d1net.com/bigdata/news/345893.html 先简单的做个自我介绍,我是云6期的,黑马相比其它培训机构的好偶就不在这里说,想比大家都比我清楚: ...
- POJ1780-Code(欧拉路径求解)
题目链接:poj1780-Code 题意:有个保险箱子是n位数字编码,当正确输入最后一位编码后就会打开(即输入任意多的数字只有最后n位数字有效)……要选择一个好的数字序列,最多只需按键10n+n-1次 ...
- 通过class和id获取DOM元素的区别
1.通过id获取DOM元素的方法:document.getElementById("id名") 2.通过class获取DOM元素的方法:document.getElementsBy ...
- 230. Kth Smallest Element in a BST ——迭代本质:a=xx1 while some_condition: a=xx2
Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Not ...
- 当进入log文件后就卡机
问题:一个目录打开,终端就卡死不动了 Ctrl+c也没用,cat一样没用? 解决办法:用的时间或用的数量删除(时间已经否决掉) ls -t |tail -1000 |xargs rm 原因: log ...
- json_encode时中文编码转正常状态
function json_encode_cn($data) { $data = json_encode($data); return preg_replace("/\\\u([0-9a-f ...
- 项目构建工具Gradle的使用入门(参考,只表明地址)
Gradle入门介绍:简介 http://blog.jobbole.com/71999/ Gradle入门介绍:第一个Java项目 http://blog.jobbole.com/72558/ Gra ...
- 安卓/res/menu/的使用
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http:/ ...
- 构建之法<第四章>之感悟
第四章:两人合作内容出处:4.6 两人合作的不同阶段和技巧 本章主要是讲关于合作方面的,文章以刚刚认识的两个人为例!也就是说,他们之前的关系是陌生人,然而在现实当中两人合作也可以有其它的关系,比如说合 ...
- Adriod—— DVM
Android 运行环境主要指的虚拟机技术——Dalvik.Android中的所有Java程序都是运行在Dalvik VM上的.Android上的每个程序都有自己的线程,DVM只执行.dex的Dalv ...