《leetCode》:Longest Common Prefix

来源:互联网 发布:网络整合营销 编辑:程序博客网 时间:2024/06/06 03:10

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

大意就是寻找字符串数组中所有字符的最大公共子串。


public String longestCommonPrefix(String[] strs) {    if(strs == null || strs.length == 0)    return "";    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;}