-
-
Save manishsat/c934e04427369437d01faaf6aa380a2c to your computer and use it in GitHub Desktop.
Revisions
-
bittib revised this gist
May 13, 2013 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -16,9 +16,9 @@ public String addBinary(String a, String b) { while (i >= 0 || j >= 0 || carry > 0){ int ck = carry; if (i >= 0) ck += a.charAt(i--) - '0'; if (j >= 0) ck += b.charAt(j--) - '0'; if (ck > 1){ ck -= 2; carry = 1; -
bittib revised this gist
May 13, 2013 . 1 changed file with 7 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,12 @@ /** * LeetCode Add Binary Probem Given two binary strings, return their sum (also a binary string). For example, a = "11" b = "1" Return "100". */ public String addBinary(String a, String b) { int n = a.length(), m = b.length(); -
bittib created this gist
May 13, 2013 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,26 @@ /** * LeetCode Add Binary Probem */ public String addBinary(String a, String b) { int n = a.length(), m = b.length(); char[] chs = new char[Math.max(n, m) + 1]; int i = n-1, j = m-1, k = chs.length-1, carry = 0; while (i >= 0 || j >= 0 || carry > 0){ int ck = carry; if (i >= 0) ck += a.charAt(i) - '0'; if (j >= 0) ck += b.charAt(j) - '0'; if (ck > 1){ ck -= 2; carry = 1; }else carry = 0; chs[k--] = (char)('0' + ck); } k++; while (k < chs.length-1 && chs[k] == '0') k++; return new String(chs, k, chs.length - k); }