leetCode-15. 3Sum-Medium

descrition

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]
]

解析

方法1

3 重循环,时间复杂度-O(n^3),空间复杂度-O(1)

for(int i=0; i<array.size(); i++){
for(int j=i+1; j<array.size(); j++){
for(int k=j+1; k<array.size(); k++){
// 检查是否合法
}
}
}

方法2

思考方向:是否可以减少方法 1 中的循环查找次数??

时间复杂度-O(n^2),空间复杂度-O(1)。

对数组进行排序,需要时间 O(nlog(n))。使用两重循环,最外层循环 a=array[i],内层循环使用双向指针进行遍历,b 从左到右,c 从右到左,思想和 two sum 一样。(参看代码)

code


#include <iostream>
#include <vector>
#include <algorithm>
#include <unordered_map> using namespace std; class Solution{
public:
vector<vector<int> > threeSum(vector<int>& nums){
// The solution set must not contain duplicate triplets.
vector<vector<int> > ans;
sort(nums.begin(), nums.end()); // ascending
for(int i=0; i<nums.size(); i++){
int target = -nums[i];
int ileft = i+1;
int iright = nums.size()-1;
while(ileft < iright){
int sum = nums[ileft]+nums[iright];
if( sum == target){
// answer
vector<int> temp(3);
temp[0] = nums[i];
temp[1] = nums[ileft];
temp[2] = nums[iright];
ans.push_back(temp);
// skip duplicate
while(ileft<iright && nums[ileft] == temp[1])
ileft++;
while(ileft<iright && nums[iright] == temp[2])
iright--;
}else if (sum < target){
ileft++;
}else{
// sum > target
iright--;
}
} // skip duplicate
while((i+1)<nums.size() && nums[i+1] == nums[i])
i++;
// i point to the same value, after the i++ in the for loop, i will point to next value
} return ans;
}
}; int main()
{
return 0;
}

[array] leetCode-15. 3Sum-Medium的更多相关文章

  1. LeetCode 15 3Sum [sort] <c++>

    LeetCode 15 3Sum [sort] <c++> 给出一个一维数组,找出其中所有和为零的三元组(元素集相同的视作同一个三元组)的集合. C++ 先自己写了一发,虽然过了,但跑了3 ...

  2. leetcode 15. 3Sum 二维vector

    传送门 15. 3Sum My Submissions Question Total Accepted: 108534 Total Submissions: 584814 Difficulty: Me ...

  3. 蜗牛慢慢爬 LeetCode 15. 3Sum [Difficulty: Medium]

    题目 Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all ...

  4. [LeetCode] 15. 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 ...

  5. LeetCode 15. 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 ...

  6. LeetCode——15. 3Sum

    一.题目链接:https://leetcode.com/problems/3sum/ 二.题目大意: 3和问题是一个比较经典的问题,它可以看做是由2和问题(见http://www.cnblogs.co ...

  7. LeetCode 15 3Sum(3个数求和为0的组合)

    题目链接 https://leetcode.com/problems/3sum/?tab=Description   Problem: 给定整数集合,找到所有满足a+b+c=0的元素组合,要求该组合不 ...

  8. leetCode 15. 3Sum (3数之和) 解题思路和方法

    3Sum  Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find ...

  9. Leetcode 15. 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 ...

  10. 【leetcode】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 un ...

随机推荐

  1. 应用Linux远程桌面(附视频)

    650) this.width=650;" border="0" alt="" src="http://img1.51cto.com/att ...

  2. ping---测试主机之间网络的连通性

    ping命令用来测试主机之间网络的连通性.执行ping指令会使用ICMP传输协议,发出要求回应的信息,若远端主机的网络功能没有问题,就会回应该信息,因而得知该主机运作正常. 选项 -d:使用Socke ...

  3. aop 中joinpoint的使用方法

    一.簡述下joinpoint在不同情況下的不同: 1.在around中可以用,此時可以執行被包裹的代碼,可以根據情況來判斷是否執行被包裹的代碼,以實現控制的作用. public void around ...

  4. hdu4336Card Collector 概率dp+状态压缩

    //给n个卡片每次出现的概率,求全部卡片都出现的须要抽的次数的期望 //dp[i]表示在状态的情况下到全部的卡片都出现的期望 //dp[i] = 1 + p1*dp[i] + ${p2[j]*dp[i ...

  5. C++ static 静态成员变量 和 静态成员函数

    静态(static) 成员 变量 1•  静态成员变量的初始化须要在类外完毕. 2•  静态成员不属于详细的某个对象,而属于整个类: 3•  全部对象共享本类中的静态成员: 4•  静态成员最好直接通 ...

  6. 【原创】k8s源代码分析-----EndpointController

    转自本人空间 http://user.qzone.qq.com/29185807/blog/1459325937 一.controller manager创建endpointController 代码 ...

  7. js06--利用js给数组去重

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...

  8. Day4晚笔记

    数据结构 并查集:捆绑两个点的信息,判断对错 倍增:LCA, 字符串 hash,模拟, 最小表示法 给定一个环状字符串,切开,使得字符串的字典序最小 图和树 割点,割边,强联通分量 点双联通分量 (把 ...

  9. C#异步编程的实现方式(4)——Task任务

    最基本的是知道怎么启动一个Task. 1.Task类构造函数 使用Task类的构造函数.实例化Task对象时,任务不会立即运行,而是指定Created状态.接着调用Task类的Start()方法来启动 ...

  10. node:json与csv互转

    [单个文件的转化]   1.安装json2csv模块将json转成csv   jsonToCSV.js var fs = require('fs'); const Json2csvParser = r ...