首页 > 其他 > 详细

[LeetCode]14 Longest Common Prefix

时间:2015-01-02 16:11:39      阅读:210      评论:0      收藏:0      [点我收藏+]

https://oj.leetcode.com/problems/longest-common-prefix/

http://fisherlei.blogspot.com/2012/12/leetcode-longest-common-prefix.html

public class Solution {
    public String longestCommonPrefix(String[] strs)
    {
        // Validations
        if (strs == null || strs.length == 0)
            return "";
            
        if (strs.length == 1)
            return strs[0];

        // Use strs[0] as initial common string.
        String common = strs[0];
    
        // Testify all others
        for (int i = 1 ; i < strs.length ; i ++)
        {
            while (!strs[i].startsWith(common))
            {
                common = common.substring(0, common.length() - 1);
                if (common.isEmpty())
                    return "";
            }
        }
        return common;
    }
}


[LeetCode]14 Longest Common Prefix

原文:http://7371901.blog.51cto.com/7361901/1598413

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