开始刷题leetcode day3: Longest Common Prefix
来源:互联网 发布:微子网络 编辑:程序博客网 时间:2024/05/17 04:26
Write a function to find the longest common prefix string amongst an array of strings.
public class Solution {
public String longestCommonPrefix(String[] strs) {
if(strs.length == 0) return "";
String prefix = strs[0];
for(int i=1; i<strs.length; i++)
{
for(int j=0; j< Math.min(strs[i].length(),prefix.length()); j++)
{
if(prefix.charAt(j) != strs[i].charAt(j))
{
if(j == 0) return "";
prefix = prefix.substring(0,j);
break;
}
}
if(strs[i].length() < prefix.length())
prefix = prefix.substring(0,strs[i].length());
}
return prefix;
}
}
注意处理string比prefix长度小的时候情况
- 开始刷题leetcode day3: Longest Common Prefix
- [leetcode刷题系列]Longest Common Prefix
- LeetCode刷题笔录Longest Common Prefix
- LeetCode: Longest Common Prefix
- LeetCode Longest Common Prefix
- LeetCode : Longest Common Prefix
- [Leetcode] Longest Common Prefix
- [LeetCode] Longest Common Prefix
- [LeetCode]Longest Common Prefix
- LeetCode-Longest Common Prefix
- [LeetCode] Longest Common Prefix
- LeetCode - Longest Common Prefix
- leetcode Longest Common Prefix
- LeetCode | Longest Common Prefix
- Leetcode: Longest Common Prefix
- LeetCode: Longest Common Prefix
- 【LeetCode】Longest Common Prefix
- [LeetCode] Longest Common Prefix
- vim的调试插件——gdbmgr
- 开始刷题leetcode day3:Factorial Trailing Zeroes
- 扬声器切换
- 大数据流式处理一个不能忽视的问题
- 游戏引擎/GUI的设计与实现-主题
- 开始刷题leetcode day3: Longest Common Prefix
- 在线预览
- 使用L脚本语言开发一个XML访问库
- L脚本语言的Helloworld
- [LeetCode]House Robber
- 股票价格与利息率之间的关系
- Cocos v2.2.5 发布-手机一键发布,支持3D控件扩展
- 【剑指Offer学习】【面试题9 : 斐波那契数列】
- ARM指令集