LeetCode:Longest Common Prefix

来源:互联网 发布:aisino金税盘开票软件 编辑:程序博客网 时间:2024/06/05 17:15

问题描述:

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

思路:

以第一个模板,按照一位一位去比较即可。

代码:

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


0 0