Leetcode in Java && Oracle
420 subscribers
8 photos
397 files
400 links
Second channel: @codeforces_java

Let's Develop Together!
Download Telegram
image_2021-10-28_01-48-53.png
41.1 KB
#N67. Add Binary
problem link=>https://leetcode.com/problems/add-binary/

#solution
class Solution {
public String addBinary(String a, String b) {
int lena = a.length();
int lenb = b.length();
int i =0, qarz = 0;
String res = "";
while(i<lena || i<lenb || qarz!=0){
int x = (i<lena) ? Integer.parseInt(""+a.charAt(lena - 1 - i)) : 0;
int y = (i<lenb) ? Integer.parseInt(""+b.charAt(lenb - 1 - i)) : 0;
res = (x + y + qarz)%2 + res;
qarz = (x + y + qarz)/2;
i++;
}

return res;
}
}
1