14. Longest Common Prefix

来源:互联网 发布:全球一条线直销软件 编辑:程序博客网 时间:2024/06/15 15:56

Write a function to find the longest common prefix string amongst an array of strings.

找出所有字符串数组的最长公共前缀。

class Solution {    public String longestCommonPrefix(String[] strs) {        int len = strs.length;        if (len  == 0){            return "";        }        int maxLen = strs[0].length();        for (int i = 1; i < len; ++ i){            int j = 0;            for (j = 0; j < Math.min(maxLen, strs[i].length()); ++ j){                if (strs[i].charAt(j) != strs[i-1].charAt(j)){                    break;                }            }            maxLen = j;        }        return strs[0].substring(0, maxLen);    }}


原创粉丝点击