您的位置:首页 > 其它

LeetCode 14. Longest Common Prefix 找字符串数组最长相同前缀

2017-04-19 14:49 459 查看
题目:

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==null||strs.length==0)
return "";
String result = strs[0];
int i=1;
while(i<strs.length){
while(!strs[i].startsWith(result)){
result = result.substring(0, result.length()-1);
}
i++;
}
return result;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: