[Leetcode][015] 3Sum (Java)
题目在这里: https://leetcode.com/problems/3sum/
【标签】 Array; Two Pointers
【个人分析】
老实交待,这个题卡半天,第一次做不会,抄别人的。过了很久,第二次做,还是不会……。好几次都是Time Limited Error。在看过正确答案之后,才知道是用的Two Pointers + sort 做的优化。
怎么优化? 简单说,就是通过 排序 + 跳过重复(利用Two Pointers) 来达到题目中避免 duplicates的要求。
核心思路: 我们给最终的由3个数组成的小数组叫 triplet。
1. 先锁定triplet中的第一个数字的index为 i (Line 12),
2. 然后我们希望从 [ nums[i + 1], nums[end]] 中找到两个数字,使得两个数字的和为 (0 - nums[i])
关键步骤:1. 在锁定第一个数字index的时候,跳过所有重复的数字 (Line 16处)。直到我们找到下一个不重复的数字作为triplet中的第一个数字
2. 在从[nums[i + 1], nums[end]]中找两个数字的时候,利用left pointer, right pointer。如果 left, right两个数的和比 twoSumTarget (Line 39)
小的话,只能通过将left pointer向右移动,来使得left + right的和更大一些,从而更接近twoSumTarget。
【一点心得】
在O(N^2)的算法中,考虑加入O(NlgN) 的排序操作。特别是遇到需要去除重复的时候,利用排序 + Two Pointers 来达到 HashSet的效果。
public class Solution {
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> result = new ArrayList<List<Integer>>();
int len = nums.length;
if (len < 3) {
return result;
}
Arrays.sort(nums);
// for all number that can be the 1st number of triplet
for (int i = 0; i < len - 1; i++) {
int firstNumber = nums[i];
// skip all duplicated first number
if (i == 0 || firstNumber != nums[i - 1]) {
int leftIndex = i + 1;
int rightIndex = len - 1;
int twoSumTarget = 0 - firstNumber;
// try to find two numbers that sum up to twoSumTarget
while (leftIndex < rightIndex) {
int twoSum = nums[leftIndex] + nums[rightIndex];
if (twoSum == twoSumTarget) {
// one valid triplet found!!
result.add(Arrays.asList(firstNumber, nums[leftIndex], nums[rightIndex]));
// skip duplicated nums[leftIndex]
while (leftIndex < rightIndex && nums[leftIndex] == nums[leftIndex + 1]) {
leftIndex++;
}
// skip duplicated nums[rightIndex]
while (leftIndex < rightIndex && nums[rightIndex] == nums[rightIndex - 1]) {
rightIndex--;
}
// move to next non-duplicates
leftIndex++;
rightIndex--;
} else if (twoSum < twoSumTarget) {
// move left towards right to
// make twoSum larger to get closer to twoSumTarget
leftIndex++;
} else {
rightIndex--;
}
}
}
}
return result;
}
}
[Leetcode][015] 3Sum (Java)的更多相关文章
- 【JAVA、C++】LeetCode 015 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 ...
- [Leetcode]015. 3Sum
public class Solution { public List<List<Integer>> threeSum(int[] num) { Arrays.sort(num ...
- [Leetcode][016] 3Sum Closest (Java)
题目: https://leetcode.com/problems/3sum-closest/ [标签]Array; Two Pointers [个人分析] 这道题和它的姊妹题 3Sum 非常类似, ...
- [LeetCode] 259. 3Sum Smaller 三数之和较小值
Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 < ...
- No.015 3Sum
15. 3Sum Total Accepted: 131800 Total Submissions: 675028 Difficulty: Medium Given an array S of n i ...
- 求和问题总结(leetcode 2Sum, 3Sum, 4Sum, K Sum)
转自 http://tech-wonderland.net/blog/summary-of-ksum-problems.html 前言: 做过leetcode的人都知道, 里面有2sum, 3sum ...
- LeetCode 15 3Sum [sort] <c++>
LeetCode 15 3Sum [sort] <c++> 给出一个一维数组,找出其中所有和为零的三元组(元素集相同的视作同一个三元组)的集合. C++ 先自己写了一发,虽然过了,但跑了3 ...
- LeetCode--No.015 3Sum
15. 3Sum Total Accepted: 131800 Total Submissions: 675028 Difficulty: Medium Given an array S of n i ...
- LeetCode 16. 3Sum Closest(最接近的三数之和)
LeetCode 16. 3Sum Closest(最接近的三数之和)
随机推荐
- ucenter 通信原理个人总结
用户登陆discuz,调用on_login() , on_login ()里调用了userlogin() 验证用户信息,正确的话,调用uc_user_synlogin(),然后调用uc_api_pos ...
- laravel中StartSession中间件的问题
今天使用了laravel的dingoapi插件做了一些功能,但是最后遇到一个问题,我在页面和api的路由组中都加了一个相同的以session为基础的身份验证中间件,然后我以管理员身份登录页面时通过了验 ...
- 浅谈 “空指针、野指针、void*”
Author: JW. Zhou Date: 2014/7/2 一.空指针(0/NULL) 返回NULL和返回0是完全等价的,因为NULL和0都表示空指针,换句话说:空指针是什么,就是 ...
- Toast——提醒方式
Toast是Android系统提供的一种非常好的提醒方式,在程序中可以使用它将一些短小的信息通知给用户,这些信息会在一段时间后自动消失,并且不会占用任何屏幕空间,我们现在就尝试一下如何在活动中使用To ...
- java调优随记-java对象大小
在java中,基本数据类型的大小是固定.但是java对象的大小是不固定的,需要通过计算. 在java中,一个空对象(没有属性和方法的对象)在堆中占用8byte,比如 Object obj = new ...
- 黑马程序员_JavaIO流(四)
File概述 File类 用来将文件或者文件夹封装成对象. 方便对文件与文件夹的属性信息进行操作. File对象可以作为参数传递给流的构造函数. 了解File类中的常用方法. 字段:static St ...
- RenderBody, RenderPage and RenderSection methods in MVC 3
原文地址:http://www.codeproject.com/Articles/383145/RenderBody-RenderPage-and-RenderSection-methods-in R ...
- ACM2039_三角形三边关系
#include <iostream> using namespace std; int main(int argc, char* argv[]) { double a,b,c; int ...
- for
1,cout在显示bool值之前将他们转换为int,但cout.setf(ios::boolalpha)函数调用设置了一个标记,标记命令cout显示true 和 false 而不是 1 和0;
- Label 添加表情图片
// 添加表情 NSTextAttachment *attch = [[NSTextAttachment alloc] init]; // 表情图片 attch.image = [UIImage im ...