思路:每次从字符数组中读取两个字符串比较。需要注意输入字符串为空,等细节。

     public String longestCommonPrefix(String[] strs) {
if(strs==null||strs.length==0){
return "";
}
int count=strs[0].length();
for(int i=1;i<strs.length;i++){
String str1=strs[i-1];
String str2=strs[i];
int len=Math.min(str1.length(),str2.length());
if(len<count){
count=len;
}
int comNum=0;
for(int j=0;j<count;j++){
if(str1.charAt(j)==str2.charAt(j)){
comNum++;
}else{
break;
}
}
if(comNum<count){
count=comNum;
} }
if(count==0){
return "";
}
return strs[0].substring(0, count);
}

【Leetcode-easy】Longest Common Prefix的更多相关文章

  1. leetcode【14题】Longest Common Prefix

    题目:Longest Common Prefix 内容: Write a function to find the longest common prefix string amongst an ar ...

  2. C# 写 LeetCode easy #14 Longest Common Prefix

    14.Longest Common Prefix Write a function to find the longest common prefix string amongst an array ...

  3. 【leetcode】Longest Common Prefix (easy)

    Write a function to find the longest common prefix string amongst an array of strings. 思路:找最长公共前缀 常规 ...

  4. 【LeetCode OJ 14】Longest Common Prefix

    题目链接:https://leetcode.com/problems/longest-common-prefix/ 题目:Write a function to find the longest co ...

  5. 【leetcode】Longest Common Prefix

    题目简述: Write a function to find the longest common prefix string amongst an array of strings. 解题思路: c ...

  6. 【LeetCode算法-14】Longest Common Prefix

    Write a function to find the longest common prefix string amongst an array of strings. If there is n ...

  7. 【LeetCode每天一题】Longest Common Prefix(最长前缀)

    Write a function to find the longest common prefix string amongst an array of strings. If there is n ...

  8. 【LeetCode】 Longest Common Prefix

    Longest Common Prefix Write a function to find the longest common prefix string amongst an array of ...

  9. 【leetcode刷题笔记】Longest Common Prefix

    Write a function to find the longest common prefix string amongst an array of strings. 题解:以strs[0]为模 ...

  10. [LeetCode][Python]14: Longest Common Prefix

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/longest ...

随机推荐

  1. Linux下ntp时间同步

    在root用户下执行 先安装同步时间软件,每台机器执行 yum install -y ntp 然后执行以下命令: crontab -e */10 * * * * /usr/sbin/ntpdate - ...

  2. mock平台架构及实现

    转载: http://blog.csdn.net/xkhgnc_6666/article/details/51757209 在测试过程中有些情况通过手工测试是无法测试出来的或是非常难复现,比如网络异常 ...

  3. 使用REST-Assured对API接口进行自动化测试

    转载:http://blog.csdn.net/u012050416/article/details/50674612 准备 目标 开始编码 总结   说明:本文只是一个getStart示例,关键在于 ...

  4. Solidworks如何圆周阵列

    如图所示,我要把一个圆孔分布八个,切记要选择边线,选择等间距,然后输入8,则自动会变成360度.   最后效果如图所示

  5. Solidworks如何绘制螺纹

    1 随便画一个圆柱   2 在原来的地方画一个一摸一样的圆(草图2)   3 在特征选项卡中点击曲线-螺旋线/涡状线   4 设置螺距和圈数,画螺旋线   5 建立一个基准面,第一参考是点,第二参考是 ...

  6. HashSet和SortSet对比--c#学习笔记

    微软在 .NET 3.5 新增了一个 HashSet 类,在 .NET 4 新增了一个 SortedSet 类. .NET Collection 函数库的 HashSet.SortedSet 这两个泛 ...

  7. Async.js解决Node.js操作MySQL的回调大坑

    因为JavaScript语言异步特性.在使用Node.js运行非常多操作时都会使用到回调函数,当中就包含訪问数据库.假设代码中的业务逻辑略微复杂一点,回调一层层嵌套.那么代码非常easy进入Callb ...

  8. sprint3 【每日scrum】 TD助手站立会议第四天

    站立会议 组员 昨天 今天 困难 签到 刘铸辉 (组长) 和楠哥学习了通过AlarmManager 来实现闹钟,由于要用到BroadcastReceiver广播协议,所以正在学习中,暂时只是按照教程写 ...

  9. 写一段代码,判断一个包括'{','[','(',')',']','}'的表达式是否合法(注意看样例的合法规则。) 给定一个表达式A,请返回一个bool值,代表它是否合法。

    这道题比较奇怪,它的匹配规则并不是我们平时想想的那种匹配规则,例如:平时的匹配规则是().{}.[]才能匹配,本题中(和} .].)都能匹配.所以做题时要好好审题.另外,本题中给的测试用例是错误的. ...

  10. php开启pathinfo 模式

    pathinfo 模式 需要 php.ini 开启下面这个参数 cgi.fix_pathinfo=1 path_info模式:http://www.xxx.com/index.php/模块/方法   ...