• 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));
        }
    }
    
  • 相关阅读:
    oracle grant 授权语句
    c# dllimport c++数据类型映射关系
    DJ下载工具
    防抖和节流
    事件处理的三个阶段
    tomcat
    java 中的xml操作
    数据库连接池
    jdbc
    Java 注解
  • 原文地址:https://www.cnblogs.com/sunshine-2015/p/7329470.html
Copyright © 2020-2023  润新知