题目意思:给一个乱序数组,在里面寻找三个数之和为0的所有情况,这些情况不能重复,增序排列

思路:前面2sum,我用的是map,自然那道题map比双指针效率高,这道题需要先排序,再给三个指针,i、j、k

   对于i指针从前往后遍历,对于一个固定的i指针,其实就是2Sum的情况,给定一前一后两个指针进行遍历,

   值大了,就把后面的指针往前移,值小了就把前面的指针往后移。

   比较麻烦的地方在于去重,首先是i指针的去重,j和k只用一个去重就可以了

   ps:这是数组中一种十分常用的方法。

 class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
vector<vector<int>> ans;
vector<int> temp();
int size=nums.size();
int j,k;
sort(nums.begin(),nums.end());
for(int i=;i<size-;++i){ //注意这种位置可以写nums.size(),就是不能写nums.size()-2,什么原因我还没搞明白
//while(i>0&&nums[i]==nums[i-1])++i; 注意比较两种写法
if(i>&&nums[i]==nums[i-])continue;
j=i+;
k=size-;
while(j<k){
if(j>i+&&nums[j]==nums[j-]){
++j;
continue;
}
if(nums[j]+nums[k]>-nums[i])--k;
else if(nums[j]+nums[k]<-nums[i])++j;
else{
temp[]=nums[i];
temp[]=nums[j];
temp[]=nums[k];
ans.push_back(temp);
++j;
--k;
}
}
}
return ans;
}
};

    复杂度:O(n2)

15 3Sum(寻找三个数之和为指定数的集合Medium)的更多相关文章

  1. 18 4Sum(寻找四个数之和为指定数的集合Medium)

    题目意思:给一个乱序数组,在里面寻找三个数之和为target的所有情况,这些情况不能重复,增序排列 思路:采用3Sum的做法 ps:有见一种用hash的,存任意两个元素的和,然后变成3sum问题,需要 ...

  2. 15. 3Sum[M]三数之和

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

  3. 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 ...

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

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

  5. JS使用三元运算符判断三个数中最大的数

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  6. [LeetCode] 3Sum Smaller 三数之和较小值

    Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 < ...

  7. LeetCode 16 3Sum Closest (最接近target的3个数之和)

    题目链接 https://leetcode.com/problems/3sum-closest/?tab=Description     Problem : 找到给定数组中a+b+c 最接近targe ...

  8. 【LeetCode-面试算法经典-Java实现】【015-3 Sum(三个数的和)】

    [015-3 Sum(三个数的和)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given an array S of n integers, are there ...

  9. Java [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 ...

随机推荐

  1. 【转】Any way to implement BLE notifications in Android-L preview----不错

    原文网址:http://stackoverflow.com/questions/24865120/any-way-to-implement-ble-notifications-in-android-l ...

  2. Validate Binary Search Tree——LeetCode

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

  3. [Locked] Find the Celebrity

    Find the Celebrity Suppose you are at a party with n people (labeled from 0 to n - 1) and among them ...

  4. H - Pots

    题目大意: 有一个瓶子A和一个瓶子B,可以有三种操作倒满,倒空,或者把瓶子A倒向瓶子B(或者把瓶子B倒向瓶子A),可以扩展出6种操作,没什么简单的写法,只能一种一种的写..... 当然是使用广搜... ...

  5. [置顶] android 图片库的封装

    大家在做安卓应用的时候  经常要从网络中获取图片 都是通过URL去获取 可是如果本地有图片数据 从本地获取数据不更加快一些  自己在工作中遇到这个问题 所以采用了一个URL和本地图片的一个映射关系  ...

  6. Java 流的概述及操作(转)

    一.什么是流? 流就是字节序列的抽象概念,能被连续读取数据的数据源和能被连续写入数据的接收端就是流,流机制是Java及C++中的一个重要机制,通过流我们可以自由地控制文件.内存.IO设备等数据的流向. ...

  7. Manacher思想 SCOI2013 密码

    关于$\mathrm{Manacher}$算法,网上介绍已经很全面 这里说一下自己的理解 这里的$rad$数组:$rad_i$表示以以位置i为中心的最长回文串的回文半径(不包括i这个点). 朴素的思想 ...

  8. Project Euler:Problem 55 Lychrel numbers

    If we take 47, reverse and add, 47 + 74 = 121, which is palindromic. Not all numbers produce palindr ...

  9. Android xml 解析

    XML 经常使用的三种解析方式: DOM: 所有载入到内存,生成一个树状结构,占用内存比較大. SAJ: 採用事件驱动,速度快,效率高,不支持回退. PULL:也是採用事件驱动,语法简洁. 步骤: 1 ...

  10. Referenced file contains errors (http://tiles.apache.org/dtds/tiles-config_3_0.dtd)

    java开发时遇到的问题,之前还是好好的,没有错误提示.可是今天一打开项目就出现这种问题.真不知道是怎么回事,在这里求助.错误如下: Referenced file contains errors ( ...