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. poj 3348(凸包面积)

    Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8063   Accepted: 3651 Description ...

  2. ros结合catkin_make和qtcreator

    首先是ros官网关于IDE的教程: http://wiki.ros.org/IDEs#QtCreator 1.qtcreator安装 从官网上下载.run文件, https://info.qt.io/ ...

  3. Python与正则表达式[0] -> re 模块的正则表达式匹配

    正则表达式 / Regular Expression 目录 正则表达式模式 re 模块简介 使用正则表达式进行匹配 正则表达式RE(Regular Expression, Regexp, Regex) ...

  4. Python的功能模块[3] -> binascii -> 编码转换

    binascii模块 / binascii Module binascii模块包含很多在二进制和 ASCII 编码的二进制表示之间的转换方法.关于进制转换可以参考一些内置函数. hexlify 与 u ...

  5. 洛谷——P2026 求一次函数解析式

    P2026 求一次函数解析式 题目背景 做数学寒假作业的怨念…… 题目描述 给定两个整点的坐标,求它们所在直线的函数解析式(一次函数). 输入输出格式 输入格式: 输入共两行. 第一行有两个整数x1, ...

  6. 洛谷——P1107 最大整数

    P1107 最大整数 题目描述 设有n个正整数 (n<=20), 将它们连接成一排, 组成一个最大的多位整数. 例如: n=3时, 3个整数13, 312, 343连接成的最大整数为: 3433 ...

  7. c#作业(2班)

    第二章 1.编写一个控制台程序,要求: 接受从控制台输入的姓名,如:张三 程序响应:你好,张三. 在代码中使用规范的注释,说明程序的功能 编译程序,并执行. 程序执行效果如下图: using Syst ...

  8. mysql悲观锁中的共享锁和排他锁

    概述: 共享锁又称为读锁,简称S锁,顾名思义,共享锁就是多个事务对于同一数据可以共享一把锁,都能访问到数据,但是只能读不能修改. 排他锁又称为写锁,简称X锁,顾名思义,排他锁就是不能与其他所并存,如一 ...

  9. 设置并删除Dreamweaver自动生成的_notes文件夹

    在使用Dreamweaver做项目时站点下面的每个文件夹里面都会自动生成一个_notes文件夹,删除之后马上又会再次生成.最近做项目时,有童鞋一不小心把所有的_notes文件夹全部存回到SVN上面了, ...

  10. 关于使用ueditor时候遇到的情况

    在使用百度ueditor的时候遇到的一下情况 1.点击图片之后图片无法在编辑器内显示 2.从数据库取出图片的时候无法在编辑器内显示 3.内容存放入数据库取出来之后,HTML效果不显示 流程: 1.引入 ...