LeetCode:3Sum_15
LeetCode:三数之和【15】
题目描述
给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组。
注意:答案中不可以包含重复的三元组。
例如, 给定数组 nums = [-1, 0, 1, 2, -1, -4],
满足要求的三元组集合为:
[
[-1, 0, 1],
[-1, -1, 2]
]
题目分析
我试了很多方法尝试去解决这个问题,但是都无果。后来在一次算法课上讲到了这个问题,3Sum问题,当时讨论出的解决方法如下:
List<List<Integer>> mylist = new ArrayList<>();
Arrays.sort(nums);
for(int i=0;i<nums.length;i++) {
for (int j = i+1;j<nums.length;j++) {
int k=Arrays.binarySearch(nums,-(nums[i]+nums[j]));
if(k>j)
{
ArrayList<Integer> al = new ArrayList<>();
al.add(nums[i]);al.add(nums[j]);al.add(nums[k]);
if(!mylist.contains(al))
mylist.add(al);
}
}
}
return mylist;
}
当时我对这个结局方案非常满意,可是现在不这么认为了。如果给出的数组是[0,0,0,0],那么就无法解决。
因为第三个值为0,无论如何到找到的都是中间那个,K不可能大于J。何况J还在自增。但是无论如何这是我们思考后的东西,都是有价值的。
Java题解
public List<List<Integer>> threeSum(int[] nums) {
/*
思路:从数组序列0开始依次取数作为第一个数字,剩下的两个数字指针从 数组的序列两端开始 相向取数字
并且每次都计算3个数的和,如为0则添加到列表,不为0,则根据和的大小,分别移动左右指针。
*/
List<List<Integer>> result = new ArrayList<>();
if(nums.length < 3) return result;
Arrays.sort(nums);
int i = 0;
while(i < nums.length - 2) {
if(nums[i] > 0) break;
int j = i + 1; //左指针
int k = nums.length - 1; //右指针
while(j < k) {
int sum = nums[i] + nums[j] + nums[k];
if(sum == 0) result.add(Arrays.asList(nums[i], nums[j], nums[k]));
if(sum <= 0) while(nums[j] == nums[++j] && j < k); //实现越过重复数字的功能
if(sum >= 0) while(nums[k--] == nums[k] && j < k); //同样实现越过重复数字的功能
}
while(nums[i] == nums[++i] && i < nums.length - 2);//同上
}
return result;
}
反思
1.对 while循环的进一步理解:
while(nums[i]=nums[++j]&&j<k)
根据这条命令即可实现越过重复数字的功能.
2.双指针技术的应用。
LeetCode:3Sum_15的更多相关文章
- 我为什么要写LeetCode的博客?
# 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
- [LeetCode] Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子字符串
Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...
- Leetcode 笔记 113 - Path Sum II
题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- Leetcode 笔记 100 - Same Tree
题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
随机推荐
- VS2008 工程只生成dll不生成lib的解决方案
http://topic.csdn.net/u/20081216/22/b12d1450-7585-4c9f-867a-7c181737c328.html 问题:vs2008版本的,不知道为什么只生成 ...
- iOS CGContextRef画图时的常用方法
UIView的drawRect方法 CoreGraphics绘图 综述:描述系统会调用UIView的drawRect方法,所以coreGraphics的所有实现代码放在该函数内,setNeedsDis ...
- 原创 Datareader 导出为csv文件方法
DataReader 是游标只读数据, 如果是大数据导出,用Datatable 将耗费巨大内存资源.因为Datatable 其实就是内存中的一个数据表 代码如下 /// <summary> ...
- 【转载】NuGet镜像上线试运行
原文链接: http://www.cnblogs.com/cmt/p/nuget-mirror.html 在Visual Studio中的添加方法: 由于目前带宽有限,如果出现下面的问题,说明当前带宽 ...
- 微软正式发布Visual Studio 2013 Update 3 (2013.3) RTM
昨天微软的Visual Studio 2013 Update 3(Visual Studio 2013.3)正式发布(RTM)了,做为微软认证金牌合作的葡萄城控件,我们组织力量第一时间进行翻译.分享给 ...
- java实现输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
package JingDian; import java.util.Scanner; public class charKind { public static void main(String[] ...
- Android多线程分析之四:MessageQueue的实现
Android多线程分析之四:MessageQueue的实现 罗朝辉 (http://www.cnblogs.com/kesalin/) CC 许可,转载请注明出处 在前面两篇文章<Androi ...
- Django集成百度富文本编辑器uEditor
UEditor是由百度web前端研发部开发所见即所得富文本web编辑器,具有轻量,可定制,注重用户体验等特点,开源基于MIT协议,允许自由使用和修改代码. 首先从ueEditor官网下载最新版本的包, ...
- 在 Win10 命令行使用 Consolas + 微软雅黑
这个过程挺神奇的,步骤参考了下面两篇文章,但是过程很曲折: 1. 使用Monaco和微软雅黑字体美化cmd和PowerShell 2. [zz]Windows的cmd.exe使用consolas加中文 ...
- JavaScript获取两个数之间的任意随机数
通过JavaScript的Math.random()方法可以获取0到1之间的任意随机数,那如何获取任意给定的两个数之间的随机数呢?如获取2和5之间的随机数,5和10之间的随机数等. 由于Math.ra ...