What is the difference between if(A&&B) and if (A) { if(B){}} in Java?


  • 0
    N

    Maybe a dump question, but is there any difference between if(A&&B){...} and if(A){if(B){...}}? Since Java uses short-circuiting, I thought the two statement should be the same.

    But in DFS, with the first one I got Time Limit Exceeded while the second one works fine.

    Please see the two solutions. The only difference is the first if-condition check in 'dfs()' method.

    Here is my working code:

    public class Solution {
    	
        public static List<String> restoreIpAddresses(String s) {
            List<String> result = new ArrayList<String>();
            StringBuilder address = new StringBuilder();
            
            dfs(result, s, address, 0);
            
            return result;
        }
        
        private static void dfs(List<String> result, String s, StringBuilder address, int counter) {
                
                // *********** The only difference is here! *******************
    
        	if (counter == 3) {
        		if(isValidIP(s)) { 
        			result.add(address.toString() + s);
        		}
        	} else {
        		
        		int len = address.length();
        		for (int i = 1; i <= 3 && i < s.length(); i++) {
    				String tmp = s.substring(0,i);
        			if (isValidIP(tmp)) {
        				address.append(tmp);
        				address.append(".");
        				dfs(result, s.substring(i), address, counter+1);
        				address.delete(len, address.length());
        			}
        		
        		}
        	}
        	
        }
        
        private static boolean isValidIP(String s) {
        	
        	if (s == null || s.length() == 0 ||s.length() > 3) {
        		return false;
        	}
        	
        	if (s.charAt(0) == '0') {
        		if (s.length() == 1) {
        			return true;
        		} else {
        			return false;
        		}
        	}
        	
        	if (Integer.parseInt(s) > 0 && Integer.parseInt(s) <= 255) {
        		return true;
        	} else {
        		return false;
        	}
        }
    }
    

    Here is the code with TLE error:

    public class Solution {
    
       public static List<String> restoreIpAddresses(String s) {
            List<String> result = new ArrayList<String>();
            StringBuilder address = new StringBuilder();
            
            dfs(result, s, address, 0);
            
            return result;
        }
        
        private static void dfs(List<String> result, String s, StringBuilder address, int counter) {
        	if (counter == 3 && (isValidIP(s)) ) {
        		result.add(address.toString() + s);
        		
        	} else {      		
        		int len = address.length();
        		for (int i = 1; i <= 3 && i < s.length(); i++) {
    				String tmp = s.substring(0,i);
        			if (isValidIP(tmp)) {
        				address.append(tmp);
        				address.append(".");
        				dfs(result, s.substring(i), address, counter+1);
        				address.delete(len, address.length());
        			}
        		
        		}
        	}
        	
        }
        
        private static boolean isValidIP(String s) {
        	
        	if (s == null || s.length() == 0 ||s.length() > 3) {
        		return false;
        	}
        	
        	if (s.charAt(0) == '0') {
        		if (s.length() == 1) {
        			return true;
        		} else {
        			return false;
        		}
        	}
        	
        	if (Integer.parseInt(s) > 0 && Integer.parseInt(s) <= 255) {
        		return true;
        	} else {
        		return false;
        	}
        }
        
    }

  • 1

    By themselves, they are equivalent. But you follow them with an else branch, and

        if (counter == 3) {
            if(isValidIP(s)) { 
                result.add(address.toString() + s);
            }
        } else {
    

    and

        if (counter == 3 && (isValidIP(s)) ) {
            result.add(address.toString() + s);
        } else {            
    

    are of course not equivalent. Think through what happens in each version when counter == 3 is true but isValidIP(s) is false.


  • 0
    N

    got it ! thanks!


Log in to reply
 

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