3Sum 

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)

思路:此题解法上不算难,可是通过率并不高。仅仅有16.9%。显然在其它地方存在限制。果然第一次提交測试的时候。果断TLE(超时)。代码效率上须要更快的速度。

第一次代码本地測试一组8ms左右。不能过。后面上网參看资料,写出改进的方法,同样的数据,本地測试2ms左右,效率约提高了4倍。

第一种方法代码:

public class Solution {
public List<List<Integer>> threeSum(int[] nums) {
if(nums.length < 2){
return null;
}
//System.out.println(nums);
Arrays.sort(nums);
List<List<Integer>> list = new ArrayList<List<Integer>>();
for(int i = 0; i < nums.length - 2; i++){
if(nums[i] > 0)
break; if(i > 1 && nums[i] == nums[i-1]){
continue;
} for(int j = nums.length - 1; j > i + 1; j--){
if(nums[j] < 0)
break; if(j < nums.length -1 && nums[j] == nums[j+1]){
continue;
}
//System.out.println(nums[i]);
int c = -(nums[i] + nums[j]);
int k = search(c,nums,i+1,j-1);
if(k > 0){
List<Integer> al = new ArrayList<Integer>();
al.add(nums[i]);
al.add(nums[k]);
al.add(nums[j]);
list.add(al);
}
}
}
return list;
}
//二分查找数值c的位置,找到返回位置。找不到返回-1
public static int search(int c,int[] nums,int start,int end){
if(c < nums[start] || c > nums[end])
return -1; int k = 0;
while(start <= end){
k = (start + end)/2;
System.out.println(k);
if(c > nums[k]){
start = k + 1;
}else if(c < nums[k]){
end = k - 1;
}
else{
return k;
}
}
return -1;
}
}

另外一种方法代码:

public class Solution {
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> list = new ArrayList<List<Integer>>();
if(nums.length < 2){
return list;
}
Arrays.sort(nums);//数组排序
int j,k,m;
int len = nums.length;
for(int i = 0; i < len - 2; i++){
//假设最小值依旧大于0或者最大值小于0,肯定没有符合要求的值。直接返回
if(nums[i] > 0 || nums[len-1] < 0)
break;
//假设如今的数字和之前的数字反复,直接跳出,继续下一下
if(i > 0 && nums[i] == nums[i-1]){
continue;
}
//初始值
j = i + 1;
k = len - 1; while(j < k){
m = nums[i] + nums[j] + nums[k];//三者之和
if(m == 0){//=0。满足条件
List<Integer> al = new ArrayList<Integer>();
al.add(nums[i]);
al.add(nums[j]);
al.add(nums[k]);
list.add(al);
j++;
k--;
//假设相邻数字相等。则直接跳过,此处重要
while(j < k && nums[j] == nums[j-1]){
j++;
}
while(j < k && nums[k] == nums[k+1]){
k--;
}
}else{
if(m > 0)//这里也是非常重要的点,分情况位置标记变动
k--;
else
j++;
}
}
}
return list;
}
}

leetCode 15. 3Sum (3数之和) 解题思路和方法的更多相关文章

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

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

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

  3. 【LeetCode】15. 3Sum 三数之和

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:3sum, 三数之和,题解,leetcode, 力扣,P ...

  4. LeetCode :1.两数之和 解题报告及算法优化思路

    最近开始重拾算法,在 LeetCode上刷题.顺便也记录下解题报告以及优化思路. 题目链接:1.两数之和 题意 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 ...

  5. [LeetCode] 15. 3Sum ☆☆☆(3数和为0)

    描述 Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Fi ...

  6. 【LeetCode 15】三数之和

    题目链接 [题解] 先把n个数字升序排个序. 然后枚举三元组最左边的那个数字是第i个数字. 之后用两个指针l,r移动来获取三元组的第2个和第3个数字. (初始值,l=i+1,r = n-1); 如果a ...

  7. leetCode 86.Partition List(分区链表) 解题思路和方法

    Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...

  8. leetCode 75.Sort Colors (颜色排序) 解题思路和方法

    Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...

  9. leetCode 57.Insert Interval (插入区间) 解题思路和方法

    Insert Interval  Given a set of non-overlapping intervals, insert a new interval into the intervals ...

随机推荐

  1. [BZOJ1069][SCOI2007]最大土地面积 凸包+旋转卡壳

    1069: [SCOI2007]最大土地面积 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 3669  Solved: 1451[Submit][Sta ...

  2. 洛谷——P1755 斐波那契的拆分

    P1755 斐波那契的拆分 题目背景 无 题目描述 已知任意一个正整数都可以拆分为若干个斐波纳契数,现在,让你求出n的拆分方法 输入输出格式 输入格式: 一个数t,表示有t组数据 接下来t行,每行一个 ...

  3. Flash3D学习计划(二)——理解世界,取景,投影变换,并理解投影坐标系

    各种坐标系都是有用的,因为某些信息只能在特定场景中才有意义. 一. 世界坐标系 世界坐标系是一个特殊的坐标系,它建立了描述其他坐标系所需要的参考框架.这就意味着,能够用世界坐标系描述其他坐标系的位置, ...

  4. iOS 自定义字体设置

    有时候客户都要求使用方正兰亭刊黑字体,可是iOS没有自带这个字体,肿么办 ,只能自己自定义字体了,下面是自定义字体的几个重要步骤: 1.下载字体资源文件(.ttf或.otf格式的文件) 比如说你要使用 ...

  5. ios禁用多按钮同时按下操作

    [button setExclusiveTouch:YES]; 设置每个button的setExclusiveTouch:YES,可避免同时按下多个的问题

  6. Eclipse常用小知识汇总

    原文:http://blog.csdn.net/jinzhencs/article/details/50462370 1.修改注释 自动出来的author

  7. 学号20175313 《数据库MySQL(课下作业,必做)》第十周

    目录 一.题目要求 二.需求分析 三.关键代码以及运行结果截图 任务一 任务二 任务三 任务四 四.代码实现过程中遇到的问题及其解决方法 五.码云链接 六.心得体会 一.题目要求 下载附件中的worl ...

  8. 本地缓存localstorage使用

    最近做项目遇到一个问题,即从“个人中心”点击进入“修改支付宝”,需要自动获取用户手机号怎么做? 修改支付宝的api不提供用户手机号数据,但是发现个人中心提供,于是想通过localstorage在个人中 ...

  9. uVa 12563 Jin Ge Jin Qu

    分析可知,虽然t<109,但是总曲目时间大于t,实际上t不会超过180*n+678.此问题涉及到两个目标信息,首先要求曲目数量最多,在此基础上要求所唱的时间尽量长.可以定义 状态dp[i][j] ...

  10. remmina rdp远程连接windows

    一.remmina rdp远程连接windows sudo apt-get install remmina 二.ubuntu设置桌面快捷方式 ①找到Remmina远程桌面客户端 比如在[搜索您的本地和 ...