每天刷一道牛客题霸-第26天- 最长公共前缀

题目

https://www.nowcoder.com/practice/28eb3175488f4434a4a6207f6f484f47?tpId=190&&tqId=35964&rp=1&ru=/activity/oj&qru=/ta/job-code-high-rd/question-ranking

import java.util.*;


public class Solution {
    /**
     * 
     * @param strs string字符串一维数组 
     * @return string字符串
     */
    public String longestCommonPrefix (String[] str) {
        StringBuilder result = new StringBuilder();
        int index = 0;
        if(str.length == 0){
            return result.toString();
        }
        boolean count = true;
        char item;
        while (count){
            if (str[0].length() == 0){
                return result.toString();
            }
            if (index >= str[0].length()){
                break;
            }else {
                item = str[0].charAt(index);
            }
            for (int i = 1 ; i < str.length ; i++){
                if (index >= str[i].length()){
                    count = false;;
                    break;
                }
                if (item != str[i].charAt(index)){
                    count = false;
                    break;
                }
            }
            if (count){
                result.append(item);
            }
            index++;
        }
        return result.toString();
        // write code here
    }
}
#牛客题霸##题解#
全部评论

相关推荐

友友们,中小厂,设计模式,一般咋问呀,会问的很深吗
June丶:1. 按照设计模式分类回答:创建(工厂,单例),结构(组合,代理,外观),行为(策略,模版方法)。 2. 常见的应用:工厂+单例+策略(解耦,提高灵活性,扩展性),组合(解耦合,灵活,,提高代码复用),外观(提高安全性),模版(提高代码复用,灵活性) 3. 在回答出来这些常见实用场景+使用的优点基本就是满意回答了 4. 如果要是优质回答:可以提出你看过的源码中例如springboot中一些设计模式(不常用的代理,责任链,发布订阅),但是展现了你的钻研与热爱
点赞 评论 收藏
分享
King987:这不就是力扣的算法题吗?
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务