[leetcode]Longest Common Prefix

水题。一个一个字符串比较就是了。

public class Solution {
    public String longestCommonPrefix(String[] strs) {
        // Start typing your Java solution below
        // DO NOT write main() function
        if (strs == null || strs.length == 0) return "";
    	String s = strs[0];
    	for (int i = 1; i < strs.length; i++) {
    		int min = Math.min(s.length(), strs[i].length());
    		int j = 0;
    		while (j < min && s.charAt(j) == strs[i].charAt(j)) { j++; }
    		s = s.substring(0, j);
    	}
        return s;
    }
}
posted @ 2013-07-29 15:34  阿牧遥  阅读(203)  评论(0编辑  收藏  举报