Java Solution


  • 0
    C

    ...
    public static String addBinary(String a, String b) {
    char[] aCharArray = a.toCharArray();
    char[] bCharArray = b.toCharArray();
    String c = "";
    int grow = 0;
    for (int i = aCharArray.length - 1, j = bCharArray.length - 1, k = 0; i >= 0 || j >= 0; i--, j--, k++) {

    		int x = i >= 0 ? Integer.parseInt(aCharArray[i] + "") : 0;
    		int y = j >= 0 ? Integer.parseInt(bCharArray[j] + "") : 0;
    
    		int z = (x + y + grow) % 2;
    		grow = (x + y + grow) / 2;
    
    		c = z + c;
    
    		System.out.println(c);
    
    	}
    	if (grow == 1) {
    		c = "1" + c;
    	}
    	if (c.charAt(0) == '2') {
    
    		c = "10" + c.substring(1, c.length());
    
    	}
    
    	return c;
    }
    

    ...


Log in to reply
 

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