Add Binary | leetcode 67 | Hindi

preview_player
Показать описание
Рекомендации по теме
Комментарии
Автор

after getting stucked at the same problem and watching many complicated videos, i found the right video...

anujgajbhiye
Автор

Nice explanation!! Keep uploading more videos on leetcode problems

pratyushasarangi
Автор

Awesome Explanation Sir, ....Sir Multiply Strings ka ek explanation video upload krdijiye

rosonerri-faithful
Автор

u need to use String Builder and reverse it like this;

class Solution {
public String addBinary(String a, String b) {
int carry = 0;
int aLen = a.length();
int bLen = b.length();
int i = 0;
StringBuilder ans = new StringBuilder();


while(i<aLen || i<bLen || carry != 0){
int x = 0;
if(i <aLen && a.charAt(aLen -1-i) == '1'){
x = 1;
}
int y = 0;
if(i <bLen && b.charAt(bLen -1-i) == '1'){
y = 1;
}

ans.append((x+y+carry)%2);
carry = (x+y+carry)/2;
i++;

}


return ans.reverse().toString();
}
}

davinder
Автор

Another video well explained. Please keep up the good work..Thank you so much.

meghawadhera
Автор

Working C++ code ✅🙂

class Solution {
public:
string addBinary(string a, string b) {

int aSize = a.size();
int bSize = b.size();
int i = 0;
int carry = 0;
string ans = "";

while( i<aSize || i<bSize || carry==1 ){

int x = 0;
if( i < aSize && a[aSize-1-i]=='1' )
x = 1;

int y = 0;
if( i < bSize && b[bSize-1-i]=='1' )
y = 1;

string sum = to_string((x+y+carry)%2);
carry = (x+y+carry)/2;

ans = sum + ans;
i++;

}

return ans;

}
};

anshumaan
Автор

string addBinary(string a, string b) {

string s = "";
int n1 = a.length() - 1, n2 = b.length() - 1;
int carry = 0;

while(n1 >= 0 && n2 >= 0)
{
if(a[n1] == '0' && b[n2] == '0')
{
if(carry == 0)
{
s = '0' + s;
}
else
{
s = '1' + s;
carry = 0;
}
}
else if(a[n1] == '1' && b[n2] == '1')
{
if(carry == 0)
{
s = '0' + s;
carry = 1;
}
else
{
s = '1' + s;
}
}
else
{
if(carry == 0)
{
s = '1' + s;
}
else
{
s = '0' + s;
}
}

--n1;
--n2;
}

while(n1 >= 0)
{
if(carry == 0)
{
s = a[n1] + s;
}
else
{
if(a[n1] == '0')
{
s = '1' + s;
carry = 0;
}
else
{
s = '0' + s;
}
}
--n1;
}

while(n2 >= 0)
{
if(carry == 0)
{
s = b[n2] + s;
}
else
{
if(b[n2] == '0')
{
s = '1' + s;
carry = 0;
}
else
{
s = '0' + s;
}
}
--n2;
}

if(carry == 1)
{
s = '1' + s;
}

return s;
}

danddentertainment-np
Автор

your explaination is great but this code is not passing all the test cases ! please check before your explain

tanmesh
join shbcf.ru