C++ solution :
class Solution {
public:
string addBinary(string a, string b) {
int lena = a.size() - 1;
int lenb = b.size() - 1;
string s = "";
int sum = 0, carry = 0;
while(lena >= 0 || lenb >= 0 || carry==1) {
int n1 = lena >= 0 ? a[lena--] - '0' : 0;
int n2 = lenb >= 0 ? b[lenb--] - '0' : 0;
sum = n1^n2^carry;
carry = n1&n2|n1&carry|n2&carry;
char c = ((sum % 2) + '0');
s = c + s;
}
return s;
}
};
or
class Solution {
public:
string addBinary(string a, string b) {
int lena = a.size() - 1;
int lenb = b.size() - 1;
string s = "";
int sum = 0, carry = 0;
while(lena >= 0 || lenb >= 0 || carry==1) {
int n1 = lena >= 0 ? a[lena--] - '0' : 0;
int n2 = lenb >= 0 ? b[lenb--] - '0' : 0;
sum = n1 + n2 + carry;
carry = sum / 2;
char c = ((sum % 2) + '0');
s = c + s;
}
return s;
}
};
C solution :
char* addStrings(char* num1, char* num2) {
int len1 = 0;
int len2 = 0;
int i,j;
for(i=0;num1[i]!= NULL;i++)
len1++;
for(j=0;num2[j]!= NULL;j++)
len2++;
printf("len1 %d, len2 %d", len1, len2);
int finalSize = (len1 > len2) ? len1:len2;
char* result = malloc((finalSize+1)*sizeof(char));
int k = finalSize - 1;
int carry = 0;
int carry_temp = 0;
int sum = 0;
while(len1 && len2) { //584, 18
sum = (num1[len1 - 1] - '0') + (num2[len2 - 1] - '0');
if (sum >= 10) {
carry_temp = sum / 10;
sum = sum % 10;
}
if((sum+carry)>=10) {
sum = sum + carry;
carry_temp = sum / 10;
sum = sum % 10;
carry = 0;
}
result[k] = ((sum + carry) +'0');
carry = carry_temp;
carry_temp = 0;
len1--;
len2--;
k--;
}
while (len1) {
sum = ((num1[len1-1] - '0') + carry);
if (sum >= 10) {
carry_temp = sum / 10;
sum = sum % 10;
}
result[k] = (sum +'0');
carry = carry_temp;
carry_temp = 0;
k--;
len1--;
}
while (len2) {
sum = ((num2[len2-1] - '0') + carry);
if (sum >= 10) {
carry_temp = sum / 10;
sum = sum % 10;
}
result[k] = (sum +'0');
carry = carry_temp;
carry_temp = 0;
k--;
len2--;
}
if (carry) {
char *result_new = malloc((finalSize+2)*sizeof(char));
int l = 0;
while(l < finalSize-1) {
result_new[l+1] = result[l];
l++;
}
result_new[l+1] = result[l];
result_new[0] = carry + '0';
result_new[finalSize+1] = NULL;
return result_new;
}
result[finalSize] = NULL;
return result;
}
class Solution {
public:
string addBinary(string a, string b) {
int lena = a.size() - 1;
int lenb = b.size() - 1;
string s = "";
int sum = 0, carry = 0;
while(lena >= 0 || lenb >= 0 || carry==1) {
int n1 = lena >= 0 ? a[lena--] - '0' : 0;
int n2 = lenb >= 0 ? b[lenb--] - '0' : 0;
sum = n1^n2^carry;
carry = n1&n2|n1&carry|n2&carry;
char c = ((sum % 2) + '0');
s = c + s;
}
return s;
}
};
or
class Solution {
public:
string addBinary(string a, string b) {
int lena = a.size() - 1;
int lenb = b.size() - 1;
string s = "";
int sum = 0, carry = 0;
while(lena >= 0 || lenb >= 0 || carry==1) {
int n1 = lena >= 0 ? a[lena--] - '0' : 0;
int n2 = lenb >= 0 ? b[lenb--] - '0' : 0;
sum = n1 + n2 + carry;
carry = sum / 2;
char c = ((sum % 2) + '0');
s = c + s;
}
return s;
}
};
C solution :
char* addStrings(char* num1, char* num2) {
int len1 = 0;
int len2 = 0;
int i,j;
for(i=0;num1[i]!= NULL;i++)
len1++;
for(j=0;num2[j]!= NULL;j++)
len2++;
printf("len1 %d, len2 %d", len1, len2);
int finalSize = (len1 > len2) ? len1:len2;
char* result = malloc((finalSize+1)*sizeof(char));
int k = finalSize - 1;
int carry = 0;
int carry_temp = 0;
int sum = 0;
while(len1 && len2) { //584, 18
sum = (num1[len1 - 1] - '0') + (num2[len2 - 1] - '0');
if (sum >= 10) {
carry_temp = sum / 10;
sum = sum % 10;
}
if((sum+carry)>=10) {
sum = sum + carry;
carry_temp = sum / 10;
sum = sum % 10;
carry = 0;
}
result[k] = ((sum + carry) +'0');
carry = carry_temp;
carry_temp = 0;
len1--;
len2--;
k--;
}
while (len1) {
sum = ((num1[len1-1] - '0') + carry);
if (sum >= 10) {
carry_temp = sum / 10;
sum = sum % 10;
}
result[k] = (sum +'0');
carry = carry_temp;
carry_temp = 0;
k--;
len1--;
}
while (len2) {
sum = ((num2[len2-1] - '0') + carry);
if (sum >= 10) {
carry_temp = sum / 10;
sum = sum % 10;
}
result[k] = (sum +'0');
carry = carry_temp;
carry_temp = 0;
k--;
len2--;
}
if (carry) {
char *result_new = malloc((finalSize+2)*sizeof(char));
int l = 0;
while(l < finalSize-1) {
result_new[l+1] = result[l];
l++;
}
result_new[l+1] = result[l];
result_new[0] = carry + '0';
result_new[finalSize+1] = NULL;
return result_new;
}
result[finalSize] = NULL;
return result;
}
No comments:
Post a Comment