Leetcode——67. Add Binary

题目原址

https://leetcode.com/problems/add-binary/description/

题目描述

Given two binary strings, return their sum (also a binary string).

The input strings are both non-empty and contains only characters 1 or 0.

Example 1:

Input: a = “11”, b = “1”
Output: “100”

Example 2:

Input: a = “1010”, b = “1011”
Output: “10101”

解题思路

这个题浪费了好长时间啊 !!!!!
这就就是一个计算二进制的题,然而我用了一个非常智障的解法,虽然智障,但是很好理解。这个题还是考研基本功的一个题,主要是两点:一是整数型转换为字符型进行存储。二是要有一个变量用来存储进位的值

  • 首先判断两个字符串的长度,因为是要用长的字符串的末尾+短的字符串,然后剩下的长的字符串再直接将字符复制来就可以了。我这里使用StringBuilder 这个很好用的类来存储新的字符串
  • 这里要根据长短字符串来进行相反的处理,就是下面的 if(lengtha > lengthb) ...else部分,这两个部分是对称的,所以理解一个就可以了。
  • 两个for循环,第一个for循环用来计算两个字符串加在一起的操作,因为会涉及进位,两个二进制相加的结果可能是0,1,2,3,因为0.1不需要处理直接存储就可以,所以主要注意当相加的结果是2,3时,要多一个变量pre来存储进位值,还要更爱当前的存储值,即如果A串是“101”,B串是“1”,那在计算的时候A的后面的1和B的第一个1相加后,pre的值变为1,temp的值要变为0,然后再继续计算。
  • 第二个for循环是为了直接将长字符串中的多字符复制到新的字符串中。

AC代码

class Solution {
    public String addBinary(String a, String b) {
        StringBuilder sb = new StringBuilder();
        StringBuilder sb1 = new StringBuilder();
        char pre = '0';
        int lengtha = a.length();
        int lengthb = b.length();
        char temp = '0';
        if(lengtha > lengthb) {
            for(int i = lengtha - 1, j = lengthb - 1; j >= 0 ; i--,j--) {
                temp = (char) ((a.charAt(i) + b.charAt(j)) - 96 + pre);

                if(temp == ('2')) {
                    temp = '0';
                    pre = '1';
                }else if(temp == '3'){
                    temp = '1';
                    pre = '1';
                }else
                    pre = '0';
                sb.append(temp);

            }

            for(int i = lengtha - lengthb - 1; i >= 0; i--) {
                temp = (char) (pre + a.charAt(i ) - 48) ;
                if(temp == ('2')) {
                    temp = '0';
                    pre = '1';
                }else if(temp == '3'){
                    temp = '1';
                    pre = '1';
                }else
                    pre = '0';
                sb.append(temp);
            }
            if(pre == '1')
                sb.append(pre);
        }else {
            for(int i = lengtha - 1, j = lengthb - 1; i >= 0 ; i--,j--) {
                temp = (char) ((a.charAt(i) + b.charAt(j)) - 96 + pre);
                if(temp == ('2')) {
                    temp = '0';
                    pre = '1';
                }else if(temp == '3'){
                    temp = '1';
                    pre = '1';
                }else
                    pre = '0';
                sb.append(temp);

            }

            for(int j = lengthb - lengtha - 1; j >= 0; j--) {
                temp = (char) (pre + b.charAt(j ) - 48) ;

                if(temp == ('2')) {
                    temp = '0';
                    pre = '1';
                }else if(temp == '3'){
                    temp = '1';
                    pre = '1';
                }else
                    pre = '0';

                sb.append(temp);
            }
            if(pre == '1')
                sb.append(pre);
        }

        for(int i = sb.length() -1; i >= 0 ; i--) {
            sb1.append(sb.charAt(i));
        }
        return new String(sb1);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值