LeetCode 15 3Sum(3个数求和为0的组合)

 
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 == 0)    return "";//字符串数组为空或者长度为0
        String pre = strs[0];
        int i = 1;
        while(i < strs.length){//遍历所有字符串
            while(strs[i].indexOf(pre) != 0)//当前子串不满足前缀
                pre = pre.substring(0,pre.length()-1);//当前子串长度减一
            i++;
        }
        return pre;//返回前缀
    }
}
 
 
原文地址:https://www.cnblogs.com/zpfbuaa/p/6505754.html