Clean java code


  • 0
    A
    public List<String> restoreIpAddresses(String s) {
    	List<String> list = new LinkedList<String>();
    	for (int i = 1; i <= 3 && i < s.length(); i++) {
    		if (!isValid(s.substring(0, i))) continue;
    		for (int j = i + 1; j <= 3 + i && j < s.length(); j++) {
    			if (!isValid(s.substring(i, j))) continue;
    			for (int k = j + 1; j <= 3 + j && k < s.length(); k++) {
    				if (!isValid(s.substring(j, k))) continue;
    				if (k < s.length() && isValid(s.substring(k)))  
    				    list.add(s.substring(0, i) + "." 
    				           + s.substring(i, j) + "." 
    				           + s.substring(j, k) + "." 
    				           + s.substring(k));
    			}
    		}
    	}
    	return list;
    }
    
    boolean isValid(String s) {
    	if ((s.charAt(0) == '0' && s.length() > 1) || s.length() > 3 || Integer.parseInt(s) > 255) 
    	    return false;
    	return true;
    }

Log in to reply
 

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