Problem: 给定整数集合,找到所有满足a+b+c=0的元素组合,要求该组合不重复。
 
首先对给出的数组进行排序 Arrays.sort()
从第0个元素开始进行判断,对应的有最大值num[high]
1、当num[low]+num[high]== -num[i]时,此时可以将num[low],num[high],num[i]添加至list中
    a. 当low<high并且num[low]==num[low+1]时,需要考虑到重复问题,因此要进行 low++操作
    b. 当low<high并且num[high]==num[high—]时,同样需要进行去重复操作,进行high—操作
    c. 上述操作结束后需要对low++ ,high—
 
2、当num[low]+num[high]< -num[i]时,需要移动最小值指针 low++
3、当num[low]+num[high]< -num[i]时,需要移动最大值指针 high--
 
参考代码:
package leetcode_50;

/***
*
* @author pengfei_zheng
* 最长公共前缀
*/
public class Solution14 {
public String longestCommonPrefix(String[] strs) {
if(strs == null || strs.length == ) return "";//字符串数组为空或者长度为0
String pre = strs[];
int i = ;
while(i < strs.length){//遍历所有字符串
while(strs[i].indexOf(pre) != )//当前子串不满足前缀
pre = pre.substring(,pre.length()-);//当前子串长度减一
i++;
}
return pre;//返回前缀
}
}
 
 

LeetCode 15 3Sum(3个数求和为0的组合)的更多相关文章

  1. 【LeetCode】15. 3Sum 三个数和为0

    题目: Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find al ...

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

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

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

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

  5. leetcode 15. 3Sum 二维vector

    传送门 15. 3Sum My Submissions Question Total Accepted: 108534 Total Submissions: 584814 Difficulty: Me ...

  6. LeetCode 15. 3Sum 16. 3Sum Closest 18. 4Sum

    n数求和,固定n-2个数,最后两个数在连续区间内一左一右根据当前求和与目标值比较移动,如果sum<target,移动较小数,否则,移动较大数 重复数处理: 使i为左至右第一个不重复数:while ...

  7. LeetCode——15. 3Sum

    一.题目链接:https://leetcode.com/problems/3sum/ 二.题目大意: 3和问题是一个比较经典的问题,它可以看做是由2和问题(见http://www.cnblogs.co ...

  8. 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 ...

  9. Java [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 ...

随机推荐

  1. yii2 页面渲染方法解析

    render渲染.renderPartial渲染部分.renderContent.renderAjax.renderFile ① render显示view和layout ② renderPartial ...

  2. vue的安装

    第一步:环境的搭建 : vue推荐开发环境: Node.js: javascript运行环境(runtime),不同系统直接运行各种编程语言(https://nodejs.org/zh-cn/down ...

  3. 正則表達式re中的贪心算法和非贪心算法 在python中的应用

    之前写了一篇有关正則表達式的文章.主要是介绍了正則表達式中通配符 转义字符 字符集 选择符和子模式 可选项和反复子模式 字符串的開始和结尾 ,有兴趣的能够查看博客内容. 此文章主要内容将要介绍re中的 ...

  4. C++ 著名程序库 概览

          本文转载自: http://ace.acejoy.com/thread-3777-1-1.html   1.C++各大有名库的介绍--C++标准库 2.C++各大有名库的介绍--准标准库B ...

  5. 【Linux】理解分区

    http://blog.csdn.net/aaronychen/article/details/2270048 主分区逻辑分区设置 http://forum.ubuntu.org.cn/viewtop ...

  6. MTK 强制横屏

    frameworks\base\policy\src\com\android\internal\policy\impl目录下的PhoneWindowManager.java的rotationForOr ...

  7. SpringMVC-----使用Maven创建Web项目

    1.创建一个Maven的project 2.不使用骨架,去掉勾 3.这里的Packing 选择 war的形式 由于packing是war包,那么下面也就多出了webapp的目录 4.由于我们的项目要使 ...

  8. My Apple Developer Library Catalog

    Objective-C & Memory Management:Programming with Objective-CConcepts in Objective-C ProgrammingM ...

  9. python commands模块在python3.x被subprocess取代

    subprocess 可以执行shell命令的相关模块和函数有: os.systemos.spawnos.popen --废弃popen2.* --废弃commands.* --废弃,3.x中被移除 ...

  10. 递归的几个demo

    /** * Created by root * Description : 递归函数 */ object RecursionTest { def main(args: Array[String]): ...