although long code, but no math compute


  • 0
    S

    because list all case , so there is no division, mod math compute here

    class Solution {
    public:
        string addBinary(string a, string b) {
             int f = 0;
            int i = a.size()-1;
            int j = b.size()-1;
    
            int nf = 0;
            string c3;
            string result = "";
            while(i>=0 && j>=0) {
                nf = 0;
                if (a[i] == b[j]) {
                    c3 = "0";
                    if (a[i] == '1') {
                        nf = 1;
                    }
                } else {
                    c3 = "1";
                }
    
                if (f==1) {
                    if (c3 == "1") {
                        c3 = "0";
                        nf = 1;
                    } else {
                        c3 = "1";
                    }
                }
                result =  c3+result;
                f = nf;
    
                --i;
                --j;
            }
    
     while (i>=0){
                if (f==1){
                    if (a[i] == '0'){
                        c3 = "1";
                        f = 0;
                    } else {
                        c3 = "0";
                        f = 1;
                    }
                } else {
                    c3 = a[i];
                }
    
                result =  c3+result;
                --i;
            }
    
            while (j>=0){
                if (f==1){
                    if (b[j] == '0'){
                        c3 = "1";
                        f = 0;
                    } else {
                        c3 = "0";
                        f = 1;
                    }
                } else {
                    c3 = b[j];
                }
    
                result = c3+result;
                --j;
            }
    
            if (f==1){
                result = "1"+result;
            }
    
    
            return result;
    
        }
    };
    ```>! Spoiler

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.