这个问题是我目前的知识回答,不来,只有良好的网上搜索解决方案,发现 K Sum 它是一类问题,但是,互联网是没有更简洁的代码,我想对于谁刚开始学习的人。您可能仍然想看看这个问题该怎么解决,然后看看他们的利益扩展它。

【称号】

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)

【解析】

K Sum问题是一个系列,博客 http://tech-wonderland.net/blog/summary-of-ksum-problems.html 总结得比較完整,有兴趣能够去看。

作为刚開始学习的人。我们用最简洁的思路来说一下这道题怎么解。

暴力解决法是每一个人都能想到的。三层for循环,时间复杂度是O(n^3),并且还要处理反复的问题,显然不是题目想要的解法。

那能不能降到O(n^2)?排序算法的时间复杂度为O(nlgn),小于O(n^2),那么我们最好还是先对数组排个序。

排序之后,我们就能够对数组用两个指针分别从前后两端向中间扫描了,假设是 2Sum,我们找到两个指针之和为target就OK了。那 3Sum 类似。我们能够先固定一个数,然后找另外两个数之和为第一个数的相反数就能够了。

代码不难。先看了再说。

【Java代码】O(n^2)

public class Solution {
List<List<Integer>> ret = new ArrayList<List<Integer>>(); public List<List<Integer>> threeSum(int[] num) {
if (num == null || num.length < 3) return ret; Arrays.sort(num); int len = num.length;
for (int i = 0; i < len-2; i++) {
if (i > 0 && num[i] == num[i-1]) continue;
find(num, i+1, len-1, num[i]); //寻找两个数与num[i]的和为0
} return ret;
} public void find(int[] num, int begin, int end, int target) {
int l = begin, r = end;
while (l < r) {
if (num[l] + num[r] + target == 0) {
List<Integer> ans = new ArrayList<Integer>();
ans.add(target);
ans.add(num[l]);
ans.add(num[r]);
ret.add(ans); //放入结果集中
while (l < r && num[l] == num[l+1]) l++;
while (l < r && num[r] == num[r-1]) r--;
l++;
r--;
} else if (num[l] + num[r] + target < 0) {
l++;
} else {
r--;
}
}
}
}

注意,对于 num[i],寻找另外两个数时,仅仅要从 i+1 開始找就能够了。

这样的写法,能够避免结果集中有反复,由于数组时排好序的。所以当一个数被放到结果集随着时间的推移,它的背后,其相当于直接跳过。

版权声明:本文博主原创文章,博客,未经同意不得转载。

【LeetCode】3Sum 解决报告的更多相关文章

  1. LeetCode Merge k Sorted Lists 解决报告

    https://oj.leetcode.com/problems/merge-k-sorted-lists/ 归并K已经整理阵列,和分析算法的复杂. 解决报告:无论是不考虑优化,最简单的实现是要重新走 ...

  2. LeetCode: Permutations 解题报告

    Permutations Given a collection of numbers, return all possible permutations. For example,[1,2,3] ha ...

  3. 【LeetCode】LRU Cache 解决报告

    插话:只写了几个连续的博客,博客排名不再是实际"远在千里之外"该.我们已经进入2一万内. 再接再厉.油! Design and implement a data structure ...

  4. 【LeetCode】Triangle 解决报告

    [称号] Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjac ...

  5. [LeetCode] 3Sum Smaller 三数之和较小值

    Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 < ...

  6. [LeetCode] 3Sum Closest 最近三数之和

    Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...

  7. [LeetCode] 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 ...

  8. LeetCode:3Sum, 3Sum Closest, 4Sum

    3Sum Closest Given an array S of n integers, find three integers in S such that the sum is closest t ...

  9. Leetcode 3Sum Closest

    Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...

随机推荐

  1. java 采用MD5加密解密

    MD5加密解密 package endecrypt; import java.io.UnsupportedEncodingException; import java.security.Message ...

  2. Cocos2d-x 2地图步行实现:SPFA算法

    本文乃Siliphen原创,转载请注明出处:http://blog.csdn.net/stevenkylelee 上一节<Cocos2d-x 地图行走的实现1:图论与Dijkstra算法> ...

  3. WPF换肤之八:创建3D浏览效果

    原文:WPF换肤之八:创建3D浏览效果 上节中,我们展示了WPF中的异步以及界面线程交互的方式,使得应用程序的显示更加的流畅.这节我们主要讲解如何设计一个具有3D浏览效果的天气信息浏览器. 效果显示 ...

  4. adt-bundle-windows-x86_32-20140702

    adt-bundle-windows-x86_32-20140702 Windows > Preference,在Android里没有NDK选项 法一:help->install new ...

  5. UI僵死分析

    原因剖析 UI僵死无非只是因为UI线程因繁忙而无法去接受用户的响应.详细说来内在原因有以下两个: 正常的业务代码写在UI线程中执行,业务代码的任务繁重导致UI线程无法分身去接受用户的界面输入 UI控件 ...

  6. gdb经常使用命令总结

    1: 对于在应用程序中增加參数进行调试的方法:    直接用 gdb app -p1 -p2 这样进行调试是不行的.    须要像下面这样使用:     #gdb app     (gdb) r -p ...

  7. MyEclipse建立JVM内存大小

    1.你为什么要设置JVM它的内存大小? 因为假定程序执行过JVM默认的内存大小.他们不会报告传说中的内存溢出异常. Exception in thread "main" java. ...

  8. 修改easyui datebox默认日期格式

    问题描述: 根据jquery easyui datebox demo中给的示例,导入和使用datebox, 发现日期格式为: 6/22/2011, 其他的今天和关闭也是 Today, Close, 对 ...

  9. hdu1964之插头DP求最优值

    Pipes Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Subm ...

  10. skyeye安装+arm-elf-gdb安装+模拟s3c44b0x+执行ucos4skyeye

    [假设你要引用.请阅读所有,这里是我的为期两天的过程只是一个记录] skyeye安装:ubuntu12.0432 llvm2.8 skyeye1.3.3 http://blog.chinaunix.n ...