[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;

    }

}

你可能感兴趣的:(LeetCode)