question,Help!What's wrong with my Code?Thanks


  • 0
    U
    
    	public String longestCommonPrefix(String[] strs) {
    		if (strs.length == 0 || strs == null) {
    			return "";
    		}
    		String ans = "";
    		String min = strs[0];
    
    		for (int i = 1; i < strs.length; i++) {// strs数组中字符串的个数
    			if (strs[i].length() < min.length()) {
    				min = strs[i];
    			}
    
    			while (strs[i].indexOf(min) != 0) {
    				ans = min.substring(0, min.length() - 1);
    			}
    			i++;
    			System.out.println(ans);
    		}
    		return ans;
    	}
    
    	public static void main(String[] args) {
    		L14 l14 = new L14();
    		String[] strs = { "a", "ab" };
    		l14.longestCommonPrefix(strs);
    	}
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.