zoukankan      html  css  js  c++  java
  • leetcode — longest-common-prefix

    /**
     * Source : https://oj.leetcode.com/problems/longest-common-prefix/
     *
     * Created by lverpeng on 2017/7/10.
     *
     * Write a function to find the longest common prefix string amongst an array of strings.
     */
    public class LongestCommonPrefix {
    
        /**
         * 依次比较每个字符串的每个字符是否相同
         *
         *
         * @param strArr
         * @return
         */
        public String findLongestPrefix (String[] strArr) {
            String prefix = "";
            for (int i = 0; i < strArr[0].length(); i++) {
                boolean equal = true;
                for (int j = 0; j < strArr.length; j++) {
                    if (i >= strArr[j].length()) {
                        equal = false;
                        break;
                    }
                    if (j == 0) {
                        continue;
                    }
                    if (strArr[j].charAt(i) != strArr[j - 1].charAt(i)) {
                        equal = false;
                    }
                }
                if (!equal) {
                    break;
                } else {
                    prefix += strArr[0].charAt(i);
                }
            }
            return prefix;
        }
    
        public static void main(String[] args) {
            LongestCommonPrefix longestCommonPrefix = new LongestCommonPrefix();
            String[] strArr = new String[]{"abc", "a", "abcd"};
            System.out.println("a-------" + longestCommonPrefix.findLongestPrefix(strArr));
    
            strArr = new String[]{"abcsdfg", "abc", "abcdasdf"};
            System.out.println("abc-------" + longestCommonPrefix.findLongestPrefix(strArr));
        }
    }
    
  • 相关阅读:
    linux下的shell 快捷键
    Python3.x和Python2.x的区别
    [Python2.x] 利用commands模块执行Linux shell命令
    redis缓存策略
    高并发解决方案
    java8 lambda表达式应用
    java读写分离的实现
    数据库读写分离
    大数据量处理方案:分布式数据库
    google的CacheBuilder缓存
  • 原文地址:https://www.cnblogs.com/sunshine-2015/p/7329470.html
Copyright © 2011-2022 走看看