您的位置:首页 > 其它

【LeetCode】14.Longest Common Prefix(Easy)解题报告

2017-12-19 10:04 435 查看
【LeetCode】14.Longest Common Prefix(Easy)解题报告

tags:String

题目地址:https://leetcode.com/problems/longest-common-prefix/description/

题目描述:

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

  找出字符串序列中重复的字符串。提交了好几次,因为越界问题,要提前取到最小长度就可以了。还有不要忘异常判断。

Solutions:

class Solution {
public String longestCommonPrefix(String[] strs) {
if(strs.length==0){
return "";
}
int len=strs[0].length();
for(int i=0;i<strs.length;i++){
len=Math.min(strs[i].length(),len);
}
for(int i=0;i<strs.length-1;i++){
for(int j=0;j<len;j++){
if(strs[i].charAt(j)!=strs[i+1].charAt(j)){
len=j;
}
}
}
return strs[0].substring(0,len);
}
}


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