编写一个函数来查找字符串数组中最长的公共前缀字符串。

详见:https://leetcode.com/problems/longest-common-prefix/description/

实现语言:Java

class Solution {
    public String longestCommonPrefix(String[] strs) {
        if(strs==null||strs.length==0){
            return "";
        }
        String res=new String();
        for(int j=0;j<strs[0].length();++j){
            char c=strs[0].charAt(j);
            for(int i=1;i<strs.length;++i){
                if(j>=strs[i].length()||strs[i].charAt(j)!=c){
                    return res;
                }
            }
            res+=Character.toString(c);
        }
        return res;
    }
}

 参考:https://www.cnblogs.com/grandyang/p/4606926.html

相关文章:

  • 2021-09-14
  • 2021-10-02
  • 2021-04-19
  • 2021-09-17
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-01-15
  • 2022-02-07
  • 2021-12-22
  • 2022-12-23
  • 2022-12-23
  • 2021-10-27
  • 2022-12-23
相关资源
相似解决方案