【LeetCode字符串】--14.最长公共前缀

14.最长公共前缀

java 复制代码
class Solution {
    public String longestCommonPrefix(String[] strs) {
        if(strs == null || strs.length == 0){
            return "";
        }
        int length = strs[0].length();
        int count = strs.length;
        for(int i =0;i<length;i++){
            char c = strs[0].charAt(i);
            for(int j = 1;j<count;j++){
                if(i == strs[j].length() || strs[j].charAt(i) != c){
                    return strs[0].substring(0,i);
                }
            }
        }
        return strs[0];

    }
}
相关推荐
明朝百晓生34 分钟前
强化学习[chapter8] [page17] Value Function Methods
人工智能·算法
POLITE31 小时前
Leetcode 56.合并区间 JavaScript (Day 6)
算法·leetcode·职场和发展
历程里程碑1 小时前
滑动窗口秒解LeetCode字母异位词
java·c语言·开发语言·数据结构·c++·算法·leetcode
ghie90901 小时前
使用直接节点积分法进行无网格法2D悬臂梁计算
算法
Helibo441 小时前
2025年12月gesp3级题解
数据结构·c++·算法
p&f°1 小时前
垃圾回收两种算法
java·jvm·算法
点云SLAM2 小时前
点云配准算法之- GICP算法点云配准概率模型推导和最大似然求解(MLE)
算法·机器人·slam·点云配准·最大似然估计·点云数据处理·gicp算法
曹轲恒2 小时前
双栈实现队列/双队列实现栈
算法
AI科技星2 小时前
张祥前统一场论电荷定义方程分析报告
开发语言·经验分享·线性代数·算法·数学建模
Swift社区2 小时前
LeetCode 460 - LFU 缓存
算法·leetcode·缓存