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

    }
}
相关推荐
风止何安啊6 分钟前
递归 VS 动态规划:从 “无限套娃计算器” 到 “积木式解题神器”
前端·javascript·算法
踢球的打工仔14 分钟前
前端html(2)
前端·算法·html
CoderYanger21 分钟前
动态规划算法-子数组、子串系列(数组中连续的一段):21.乘积最大子数组
开发语言·算法·leetcode·职场和发展·动态规划·1024程序员节
CoderYanger28 分钟前
A.每日一题——3432. 统计元素和差值为偶数的分区方案
java·数据结构·算法·leetcode·1024程序员节
TL滕44 分钟前
从0开始学算法——第八天(堆排序)
笔记·学习·算法·排序算法
Ayanami_Reii1 小时前
进阶数据结构-AC自动机
数据结构·算法·动态规划·字符串·ac自动机
报错小能手1 小时前
数据结构 AVL二叉平衡树
数据结构·算法
l1t1 小时前
利用Duckdb求解Advent of Code 2025第5题 自助餐厅
数据库·sql·mysql·算法·oracle·duckdb·advent of code
List<String> error_P1 小时前
C语言枚举类型
算法·枚举·枚举类型
liu****1 小时前
20.预处理详解
c语言·开发语言·数据结构·c++·算法