首页 > 其他 > 详细

Longest Common Prefix

时间:2015-03-15 18:09:33      阅读:323      评论:0      收藏:0      [点我收藏+]

Longest Common Prefix

问题:

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

思路:

  简单的string运算

我的代码:

技术分享
public class Solution {
    public String longestCommonPrefix(String[] strs) {
        if(strs == null || strs.length == 0) return "";
        String comm = strs[0];
        for(int i = 1; i < strs.length; i++)
        {
            comm = getCommon(comm,strs[i]);
        }
        return comm;
    }
    public String getCommon(String left, String right)
    {
        int i = 0;
        while(i < left.length() && i < right.length())
        {
            if(left.charAt(i) == right.charAt(i)) i++;
            else    break;
        }
        return left.substring(0,i);
    }
}
View Code

 

Longest Common Prefix

原文:http://www.cnblogs.com/sunshisonghit/p/4340038.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!