<> Title Description :

Given two binary strings , Back to their and ( In binary form ).
The input is a non empty string and contains only numbers 1 and 0.

<> Examples 1:

input : a = “11”, b = “1”
output : “100”

<> Examples 2:

input : a = “1010”, b = “1011”
output : “10101”

<> Question answer :

<> method 1: Backward Traversal

Record mark bit , Adds the tag bit to the corresponding bit of two strings , Analysis and , Determines the value of the current bit and whether carry is required .
Running time 0ms, The code is as follows .
#define max(a, b) ((a) > (b) ? (a) : (b)) char* addBinary(char* a, char* b) {
if(a == NULL || *a == NULL) return b; if(b == NULL || *b == NULL) return a; int
flag= 0; int len1 = strlen(a), len2 = strlen(b); int len = max(len1, len2) + 2;
char* result = (char*)malloc(len * sizeof(char)); result[len - 1] = '\0'; int i
= 0, j = 0, index = len - 2; while(len1 || len2 || flag) { int t = flag; if(len1
) t += (a[--len1] - '0'); if(len2) t += (b[--len2] - '0'); flag = t / 2; result[
index--] = '0' + t % 2; } if(index == 0) { char* temp = (char*)malloc((len - 1)
* sizeof(char)); memcpy(temp, result + 1, (len - 1) * sizeof(char)); free(result
); return temp; } return result; }

Technology
©2019-2020 Toolsou All rights reserved,
It's unexpected Python Cherry tree (turtle The gorgeous style of Library )html Writing about cherry trees , Writing about cherry trees java Four functional interfaces ( a key , simple ) Browser kernel ( understand )06【 Interpretation according to the frame 】 Data range filtering -- awesome HashMap Explain in detail os Simple use of module computer network --- Basic concepts of computer network ( agreement , system ) Some East 14 Pay change 16 salary , Sincerity or routine ?