LeetCode集锦(十六) - 第67题 Add Binary

问题

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" 


复制代码

翻译:

给定两个二进制字符串,返回它们的和(也是一个二进制字符串)。 输入字符串都是非空的,并且只包含字符1或0。 示例1: 输入:a = "11", b = "1" 输出:“100” 示例2: 输入:a = "1010", b = "1011" 输出:“10101”


解题思路

本题是用字符串模拟2精制的加法,就按照逢2进1的方式遍历一遍,如果长度不同,则在把长的覆盖上去。

解题方法

  1. 按照我们的思路来编辑,代码如下

    if (a == null || b == null) {
            return a == null ? b : a;
        }
        StringBuilder stringBuilder = new StringBuilder();
        int lenA = a.length() - 1;
        int lenB = b.length() - 1;
        int add = 0;
        while (lenA >= 0 || lenB >= 0 || add > 0) {
            int result = (lenA >= 0 ? a.charAt(lenA) - '0' : 0) + (lenB >= 0 ? b.charAt(lenB) - '0' : 0) + add;
            add = result / 2;
            stringBuilder.append(result % 2);
            lenA--;
            lenB--;
        }
    
        return stringBuilder.reverse().toString();
    复制代码

    时间复杂度: 该方案用了循环m所以f(n)=(n)=n;所以O(f(n))=O(n),即T(n)=O(n)

    空间复杂度: 该方案使用了没有使用额外空间,所以空间复杂度是O(n)=O(1);

总结

本题的大致解法如上所诉, 之前用StringBuilder的insert方法,发现速度很慢,看了下源码后,它都会移动数组,也就是正常的数组扩容拷贝,所以特别慢,再次就直接用append,然后反转一下,比之前方式要快很多。

转载于:https://juejin.im/post/5cfa8a046fb9a07ecc447716

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值