首页 > 其他 > 详细

Longest Common Prefix

时间:2015-05-26 02:10:08      阅读:258      评论:0      收藏:0      [点我收藏+]

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

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

?

public class Solution {
    public String longestCommonPrefix(String[] strs) {
        if (strs.length == 0) {
        	return "";
        }
        int i = 0;
        StringBuilder sb = new StringBuilder();
        while (true) {
        	char ch = 0;
        	for (String s : strs) {
        		if (i == s.length()) {
        			return sb.toString();
        		}
        		if (ch == 0) {
        			ch = s.charAt(i);
        		}
        		if (ch != s.charAt(i)) {
        			return sb.toString();
        		}
        	}
        	sb.append(ch);
        	i++;
        }
    }
}

?

Longest Common Prefix

原文:http://hcx2013.iteye.com/blog/2213978

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