1 题目

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

For example,
a = "11"
b = "1"
Return "100".

接口

String addBinary(String a, String b)

2 思路

处理二进制求和和进位。从低位开始,一直相加并且维护进位。和Add Two Numbers的区别是这个题目低位在后面,所以要从string的尾部往前加。
代码采用Add Two Number的思路,先把Input String逆序,让低位在前面,最后把result在逆序一次。

复杂度

Time: O(n)
Space: O(n)

3 代码

     public String addBinary(String a, String b) {
char[] ac = new StringBuilder(a).reverse().toString().toCharArray();
char[] bc = new StringBuilder(b).reverse().toString().toCharArray();
int alen = ac.length;
int blen = bc.length;
StringBuilder res = new StringBuilder();
int max = alen > blen ? alen : blen;
int carry = 0;
for (int i = 0; i < max; i++) {
int ai = i < alen ? ac[i] - '0' : 0;
int bi = i < blen ? bc[i] - '0' : 0;
int sum = ai + bi + carry;
carry = sum / 2;
res.append((char) (sum % 2 + '0'));
}
if (carry == 1)
res.append('1');
return res.reverse().toString();
}

4 总结

  • 关键是处理相加和进位
  • 采用StringBuilder reverse()
  • 和Add Two Number类似

5 扩展

String、StringBuilder、StringBuffer的区别?

6 参考

  1. Add Binary
  2. Add Binary Leetcode java
  3. LeetCode:Add Binary
  4. Add Binary -- LeetCode

LeetCode 面试:Add Binary的更多相关文章

  1. LeetCode 面试:Add Two Numbers

    1 题目 You are given two linked lists representing two non-negative numbers. The digits are stored in ...

  2. LeetCode 67. Add Binary (二进制相加)

    Given two binary strings, return their sum (also a binary string). For example,a = "11"b = ...

  3. [LeetCode] 67. Add Binary 二进制数相加

    Given two binary strings, return their sum (also a binary string). The input strings are both non-em ...

  4. 【leetcode】Add Binary

    题目简述: Given two binary strings, return their sum (also a binary string). For example, a = "11&q ...

  5. Java for LeetCode 067 Add Binary

    Given two binary strings, return their sum (also a binary string). For example, a = "11" b ...

  6. LeetCode 67. Add Binary

    Given two binary strings, return their sum (also a binary string). For example,a = "11"b = ...

  7. Java [Leetcode 67]Add Binary

    题目描述: Given two binary strings, return their sum (also a binary string). For example,a = "11&qu ...

  8. LeetCode(56)-Add Binary

    题目: Given two binary strings, return their sum (also a binary string). For example, a = "11&quo ...

  9. (String) leetcode 67. Add Binary

    Given two binary strings, return their sum (also a binary string). The input strings are both non-em ...

随机推荐

  1. iOS UIKit:Navigation Controllers

    navigation controller是一种层次结构的container view controller,即其通过一个view controllers栈来管理内部的content view con ...

  2. angularjs 根据变量改变 动态加载模板

    directive return { restrict: 'E', replace: true, templateUrl: 'app/view/order.html', link: function ...

  3. Linux强制踢出登录用户(断线账户剔除)

    首先,用w查看登录用户 :: up days, :, users, load average: 1.00, 1.01, 1.00 USER TTY FROM LOGIN@ IDLE JCPU PCPU ...

  4. compareTo()

    从字面意思可知这个方法就是比较的意思. 所以该方法有如下两种情况: 1.比较前后的两个字符不相同: (1)     String str = "Hello World"; Stri ...

  5. python np.linspace

    该函数的形式为: linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None) 作用为:在规定的时间内,返回固定间隔的 ...

  6. Java写一个简单学生管理系统

    其实作为一名Java的程序猿,无论你是初学也好,大神也罢,学生管理系统一直都是一个非常好的例子,初学者主要是用数组.List等等来写出一个简易的学生管理系统,二.牛逼一点的大神则用数据库+swing来 ...

  7. Android Studio美化之优雅的logcat

    博客: 安卓之家 微博: 追风917 CSDN: 蒋朋的家 简书: 追风917 博客园: 追风917 先来个图,图样吐sexy: 很简单,跟我走吧,两步: 1. 引入Logger库 首先,这个sexy ...

  8. 《将博客搬至CSDN》的文章

    我的CSDN地址 博客园应该以后会很少来了.

  9. wcf入门教程

    一.概述 Windows Communication Foundation(WCF)是由微软发展的一组数据通信的应用程序开发接口,可以翻译为Windows通讯接口,它是.NET框架的一部分.由 .NE ...

  10. 尽量不要用select into 复制表

    select into 复制表会带来灾难后果,因为只是复制了一个外壳,就像克隆人,有躯体没意识,像原表的主键 外键 约束 触发器 索引都不会被复制过来, 创建一个表:CREATE TABLE [dbo ...