首页 > 其他 > 详细

leetcode--Longest Common Prefix

时间:2014-06-25 17:02:44      阅读:264      评论:0      收藏:0      [点我收藏+]

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

public class Solution {
    public String longestCommonPrefix(String[] strs) {
        String prefix = "";
		int length = strs.length;
		if(length > 0){
			prefix = strs[0];
			int prefixLength = strs[0].length();
			for(int i = 1; i < length; ++i){
				int k = 0;
				int strLength = strs[i].length();
				for(int j = 0; j < Math.min(prefixLength, strLength); ++j){
					if(prefix.charAt(j) == strs[i].charAt(j))
						++k;
					else
						break;
				}
				prefix = strs[i].substring(0, k);
				prefixLength = k;
	            if( k == 0)
	                break;
			}
		}
		return prefix;  
    }
}

  

leetcode--Longest Common Prefix,布布扣,bubuko.com

leetcode--Longest Common Prefix

原文:http://www.cnblogs.com/averillzheng/p/3806156.html

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