LeetCode 三数之和 — 改进解法

题目:给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组。

注意:答案中不可以包含重复的三元组。

例如, 给定数组 nums = [-1, 0, 1, 2, -1, -4],

满足要求的三元组集合为:

[ [-1, 0, 1], [-1, -1, 2] ]

 最开始做的解法是先将整个数组排序;然后遍历前两个数(a和b)的所有情况(n^2);对于第三个数,则从剩余的数中(即从第二个数下一个位置开始到末尾)利用二分查找出是否存在数字 -(a+b)即可。 复杂度O(n^2·logn) :

class Solution {
public List<List<Integer>> threeSum(int[] nums) { List<List<Integer>> ans = new ArrayList<List<Integer>>();
Arrays.sort(nums); Map<Integer,Integer> mi = new HashMap();
for(int i=0;i<nums.length-1;i++) {
if(mi.get(nums[i]) != null) continue; //记录i下标读过的数字
mi.put(nums[i],1); Map<Integer,Integer> mj = new HashMap();
for(int j=i+1;j<nums.length;j++) {
if(mj.get(nums[j]) != null) continue; //记录j下标读过的数字
mj.put(nums[j],1); int temp = -(nums[i]+nums[j]);
if(bSearch(nums,j+1,nums.length-1,temp) == false) continue; //二分搜索j下标之后的区间是否有数字temp
ans.add(Arrays.asList(nums[i],nums[j],temp));
}
} return ans; } //二分算法
public boolean bSearch(int[] nums,int s,int e,int key) {
int start=s,end=e,mid;
while(start<=end){
mid = (start+end)/2;
if(key < nums[mid]) end=mid-1;
else if(key > nums[mid]) start=mid+1;
else if(key == nums[mid]) return true;
}
return false;
}
}

里面有两个用了哈希的地方,所以时间复杂度应该还要乘上一个常数K...(解数组相关的题感觉总有些依赖哈希的方法=_= ...)


 最近做了另一个数组区间相关的题目,受其解法的启发,打算对这道题解法进行优化。

 还是先对数组进行排序;对第一个数字(a)进行遍历,而然后在剩余的数中用前后指针的方法找出两个和为-a的数字:两个指针left和right;left初始化为数字a的下一位置,right为最后一个位置。比较nums[left]+nums[right]+a和0的大小;如果大于0则right--,小于就left++。 其中在添加结果集时需考虑去重问题,用个哈希判断是否有重复就行了,复杂度O(n^2·K) :

class Solution {
public List<List<Integer>> threeSum(int[] nums) { List<List<Integer>> ans = new ArrayList<List<Integer>>();
Arrays.sort(nums); Map<Integer,Integer> mi = new HashMap();
for(int i=0;i<nums.length-2;i++) {
if(mi.get(nums[i]) != null) continue; //记录i下标读过的数字
mi.put(nums[i],1); Map<Integer,Integer> mj = new HashMap();
int l = i+1, r = nums.length-1, temp;
while(l<r) {
temp = nums[i] + nums[l] + nums[r];
if(temp < 0) {
l++;
} else if(temp > 0) {
r--;
} else {
if((mj.get(nums[l])) == null) {
ans.add(Arrays.asList(nums[i], nums[l], nums[r]));
mj.put(nums[l], 1); }
l++; r--;
}
}
} return ans; }
}

LeetCode 三数之和 — 优化解法的更多相关文章

  1. [leetcode]三数之和

    三数之和 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以包含重复 ...

  2. [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 ...

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

  4. [LeetCode] 923. 3Sum With Multiplicity 三数之和的多种情况

    Given an integer array A, and an integer target, return the number of tuples i, j, k  such that i &l ...

  5. [LeetCode] 16. 3Sum Closest 最近三数之和

    Given an array nums of n integers and an integer target, find three integers in nums such that the s ...

  6. [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 ...

  7. LeetCode:两数之和、三数之和、四数之和

    LeetCode:两数之和.三数之和.四数之和 多数之和问题,利用哈希集合减少时间复杂度以及多指针收缩窗口的巧妙解法 No.1 两数之和 给定一个整数数组 nums 和一个目标值 target,请你在 ...

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

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

  9. LeetCode(15):三数之和

    Medium! 题目描述: 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答 ...

随机推荐

  1. 后台管理进程GameMaster

    初步准备在后台加一个进程,用来控制进程中的各种数据,修改,查看,删除玩家数据类似于后端的gm控制平台

  2. rabbitmq 简单示例(Hello World)

    一:消息中间件: AMQP,即Advanced Message Queuing Protocol,高级消息队列协议,是应用层协议的一个开放标准,为面向消息的中间件设计 RabbitMQ是实现AMQP( ...

  3. oracle添加联合主键

    1 alter table tablename add constraint unionkeyname primary key (column1,column2); 上面语句中: tablename为 ...

  4. 携程transform放大效果

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

  5. 【笔记JS/HTML/CSS】web中的HTTP协议(1)

    最近都在coursera刷课,加上自己课业也忙起来了,总是忘记写学习笔记ORZ 自省ing... 在写HTML的时候,form表单需要通过HTTP协议向服务器提交.查询数据(如下图) 客户端通过HTT ...

  6. nginx_rewrite规则介绍

    rewrite功能就是,使用nginx提供的全局变量或自己设置的变量,结合正则表达式和标志位实现url重写以及重定向.rewrite只能放在server{},location{},if{}中,并且只能 ...

  7. HTML5网页如何调用浏览器APP的微信分享功能?

    if (/AppleWebKit.*Mobile/i.test(navigator.userAgent) || (/MIDP|SymbianOS|NOKIA|SAMSUNG|LG|NEC|TCL|Al ...

  8. Django 缓存之配置Redis

    一.cache介绍 由于Django是动态网站,所有每次请求均会去数据进行相应的操作,当程序访问量大时,耗时必然会更加明显,最简单解决方式是使用:缓存. 缓存工作原理:缓存是将一些常用的数据保存内存或 ...

  9. Billboard (HDU 2795)

    Billboard (HDU 2795) Hdu 2795 注意到每个广告的长度是1,因此可以将每这一张广告牌当成一个数列表示,每个初始值为w.使用线段树维护这个数列,每次查询为找到这个数列第一个大于 ...

  10. 中文情感分析 glove+LSTM

    最近尝试了一下中文的情感分析. 主要使用了Glove和LSTM.语料数据集采用的是中文酒店评价语料 1.首先是训练Glove,获得词向量(这里是用的300d).这一步使用的是jieba分词和中文维基. ...