LeetCode: Binary summation

Title description

Give you two binary strings and return their sum (in binary representation).

Input is  non-empty  string and contains only numbers  1 and  0.

Ideas

  • a is used to represent the longer string, b represents the other
  • Convert a into a character array ch1, and b into a character array ch2

Traverse the character arrays ch1 and ch2 from back to front. Since ch2 may be traversed before ch1, it is necessary to define a temporary character . ①If ch2 is not traversed, then the characters in ch2 are stored in the temporary character. ②If ch2 is traversed , Then '0' is stored in the temporary character

  • Define a character, which means carry , the initial value is '0'
  • The preliminary results stored in ch1 in

Situation ① The carry is '0'

  • The character in ch1 == '1' and the character in ch2 == '1', then modify the character of ch1 to '0', and modify the carry to '1'
  • The character in ch1 == '0' and the character in ch2 == '1', then modify the character in ch1 to '1'

Case ② The carry is '1'

  • The character in ch1 == '0' and the character in ch2 == '0', then modify the character of ch1 to '1', and modify the carry to '0'
  • Characters in ch1 == '1' and characters in ch2 == '1', continue
  • The character in ch1 or ch2 is '1', then the character in ch1 is changed to '0'

After all traversal is completed, a new StringBuffer object is used to store the final calculation result . ①If the carry is '1' and its content is “1”, then the elements in ch1 are spliced ​​in sequence; ②If the carry is not '1', then Just splice the elements in ch1 directly

Code

class Solution {
    public String addBinary(String a, String b) {
        if(a.length()<b.length()){
            String tem=a;
            a=b;
            b=tem;
        }
        char[] ch1=new char[a.length()];
        char[] ch2=new char[b.length()];
        for(int i=0;i<ch1.length;i++){
            ch1[i]=a.charAt(i);
        }
        for(int i=0;i<ch2.length;i++){
            ch2[i]=b.charAt(i);
        }
        int i=ch1.length-1;
        int j=ch2.length-1;
        char char2='0';
        char tem='0';
        for(;i>=0;i--){
            char2='0';
            if(j>=0){
                char2=ch2[j];
            }
            if(tem=='0'){
                if(ch1[i]=='1'&&char2=='1'){
                    ch1[i]='0';
                    tem='1';
                }else if(ch1[i]=='0'&&char2=='1'){
                    ch1[i]='1';
                }
            }else if(tem=='1'){
                if(ch1[i]=='0'&&char2=='0'){
                    ch1[i]='1';
                    tem='0';
                }else if(ch1[i]=='1'&&char2=='1'){
                    j--;
                    continue;
                }else if(ch1[i]=='1'||char2=='1'){
                    ch1[i]='0';
                }
            }
            j--;
        }
        StringBuffer re=new StringBuffer();
        if(tem=='1'){
            re.append('1');
        }
        for(int d=0;d<ch1.length;d++){
            re.append(ch1[d]);
        }
        return re.toString();
    }   
}

 

Guess you like

Origin blog.csdn.net/weixin_43939602/article/details/114236772