您的位置:首页 > 其它

14. Longest Common Prefix

2016-01-26 15:11 302 查看
Write a function to find the longest common prefix string amongst an array of strings.

Subscribe to see which companies asked this question


  

}public class Solution {
public String longestCommonPrefix(String[] strs) {
if (strs.length == 0)                //想换成字符数组来着,这样空间代价有点大的感觉,直接暴力法了。。。
return "";
String str = strs[0];
int mark = 0;
int count=str.length();
while (mark != strs.length && count>=0) {
mark = 0;
str=str.substring(0, count--);              //傻逼怎么能String,记住用StringBuilder
for (int i = 0; i < strs.length; i++) {
if (strs[i].startsWith(str))
mark++;
}
}
return str;
}


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: