zoukankan      html  css  js  c++  java
  • 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;//返回前缀
        }
    }
     
     
  • 相关阅读:
    go里面redis缓存池
    go语言操作redis
    goredis的操作
    go安装redis扩展
    go协程聊天室
    go里面os包的使用案例
    用php入门网络编程
    使用go里面实现了一个并发的时钟服务器
    案例一:协程创建和使用
    系统工程笔记
  • 原文地址:https://www.cnblogs.com/zpfbuaa/p/6505754.html
Copyright © 2011-2022 走看看