【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];

    }
}
相关推荐
TL滕13 分钟前
从0开始学算法——第十二天(KMP算法练习)
笔记·学习·算法
Math_teacher_fan16 分钟前
第二篇:核心几何工具类详解
人工智能·算法
汉克老师17 分钟前
CCF-NOI2025第二试题目与解析(第二题、集合(set))
c++·算法·noi·子集卷积·sos dp·mod 异常
mit6.82444 分钟前
presum|
算法
不穿格子的程序员1 小时前
从零开始写算法——链表篇2:从“回文”到“环形”——链表双指针技巧的深度解析
数据结构·算法·链表·回文链表·环形链表
guygg881 小时前
基于Matlab的压缩感知信道估计算法实现
开发语言·算法·matlab
诺....2 小时前
C语言不确定循环会影响输入输出缓冲区的刷新
c语言·数据结构·算法
Yuroo zhou2 小时前
采矿定向技术演进:MEMS定向短节的崛起
算法·硬件架构·硬件工程·石油·钻井