最长公共前缀 (Longest Common Prefix)

 

思路:

// @Title: 最长公共前缀 (Longest Common Prefix)
// @Author: qisiii
// @Date: 2024-04-11 23:52:45
// @Runtime: 1 ms
// @Memory: 40.5 MB
// @comment: 
// @flag: 
class Solution {
    public String longestCommonPrefix(String[] strs) {
        StringBuilder stringBuilder=new StringBuilder();
        String str=strs[0];
        for(int i=0;i<str.length();i++){
            for(int j=1;j<strs.length;j++){
                if(i>=strs[j].length()||str.charAt(i)!=strs[j].charAt(i)){
                    return stringBuilder.toString();
                }
            }
            stringBuilder.append(str.charAt(i));
        }
        return stringBuilder.toString();
    }
}
Licensed under CC BY-NC-SA 4.0
最后更新于 2024-10-18