SHARE
TWEET

Untitled

a guest Jul 16th, 2019 83 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Solution from lx223
  2. public class Solution {
  3.     public String addBinary(String a, String b) {
  4.         StringBuilder sb = new StringBuilder();
  5.         int i = a.length() - 1, j = b.length() - 1, carry = 0;
  6.         while (i >= 0 || j >= 0) {
  7.             int sum = carry;
  8.             if (j >= 0) sum += b.charAt(j--) - '0';
  9.             if (i >= 0) sum += a.charAt(i--) - '0';
  10.             sb.append(sum % 2);
  11.             carry = sum / 2;
  12.         }
  13.         if (carry != 0) sb.append(carry);
  14.         return sb.reverse().toString();
  15.     }
  16. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top