题目在这里: 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)的更多相关文章

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

  2. [Leetcode]015. 3Sum

    public class Solution { public List<List<Integer>> threeSum(int[] num) { Arrays.sort(num ...

  3. [Leetcode][016] 3Sum Closest (Java)

    题目: https://leetcode.com/problems/3sum-closest/ [标签]Array; Two Pointers [个人分析] 这道题和它的姊妹题 3Sum 非常类似, ...

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

  5. No.015 3Sum

    15. 3Sum Total Accepted: 131800 Total Submissions: 675028 Difficulty: Medium Given an array S of n i ...

  6. 求和问题总结(leetcode 2Sum, 3Sum, 4Sum, K Sum)

    转自  http://tech-wonderland.net/blog/summary-of-ksum-problems.html 前言: 做过leetcode的人都知道, 里面有2sum, 3sum ...

  7. LeetCode 15 3Sum [sort] <c++>

    LeetCode 15 3Sum [sort] <c++> 给出一个一维数组,找出其中所有和为零的三元组(元素集相同的视作同一个三元组)的集合. C++ 先自己写了一发,虽然过了,但跑了3 ...

  8. LeetCode--No.015 3Sum

    15. 3Sum Total Accepted: 131800 Total Submissions: 675028 Difficulty: Medium Given an array S of n i ...

  9. LeetCode 16. 3Sum Closest(最接近的三数之和)

    LeetCode 16. 3Sum Closest(最接近的三数之和)

随机推荐

  1. PHP判断文章里是否有图片

    用preg_match来检查内容里是否有匹配的“<img”,其实我们还用preg_match来判断很多东西,比如邮箱地址里是否有“@”等,下面用一小段代码来演示具体用法. $content=&q ...

  2. WebStorm 使用快捷键大全

    1. ctrl + shift + n: 打开工程中的文件,目的是打开当前工程下任意目录的文件. 2. ctrl + j: 输出模板 3. ctrl + b: 跳到变量申明处 4. ctrl + al ...

  3. 《EM-PLANT仿真技术教程》读书笔记(持续更新中)

    1.在系统分析过程中,必须考虑系统所处的环境,因此划分系统与环境的边界是系统分析的首要任务 2.模型可以分为物理模型和数学模型.数学模型可以分为解析模型.逻辑模型.网络模型以及仿真模型.模型可以分为离 ...

  4. APKTool用法

    APKTool是GOOGLE提供的APK编译工具,需要JAVA运行环境,推荐使用JDK1.6或者JDK1.7. 如果你想对APK文件进行修改,那么就不可避免的要使用到APKTool.论坛里有很多关于R ...

  5. SQL Server 2012数据库还原所遇到的问题

    在SQL Server2005及以下版本做数据库备份还原时,需要首先建立数据库,然后才能进行数据库还原操作:而在SQL Server2005以上版本做数据库还原时,不需要建立数据库,可以直接进行数据库 ...

  6. Altium Designer多图纸原理图设计方法探讨

    1 图纸结构 包括层次式图纸的连接关系是纵向的,也就是某一层次的图纸只能和相邻的上级或下级有关系,另一种即扁平式图纸的连接关系是横向的,任何两张图纸之间都可以建立信号连接. 2 网络连接方式 Alti ...

  7. 是用VLC API将RTSP流convert为视频文件

    一直在文档中找不到,接口设计的也不大好.不过终于在stackoverflow上找到解决方案了. 原来在LIbVLC库实例化的时候就需要传递相关的参数.我的代码大致是这样实现: const char * ...

  8. Windows下重启指定名称的服务

    // 重启指定服务 void CPSSDPrinterCtrlPlug::RestartService(const wchar_t* nswServiceName) { SC_HANDLE schSC ...

  9. POJ3729 Facer’s string 后缀数组

                                                                                                      Fa ...

  10. 数据库中的schema概念

    原文地址:http://blog.sina.com.cn/s/blog_7952e89001010jlj.html 数据库的初学者往往会对关系型数据库模式(schema).数据库(database). ...