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. Java基础知识强化之网络编程笔记09:TCP之客户端键盘录入服务器写到文本文件中

    1. TCP之客户端键盘录入服务器写到文本文件中 (1)客户端: package cn.itcast_09; import java.io.BufferedReader; import java.io ...

  2. RedHat7安装Tomcat

    编译安装Tomcat 下载jdk (http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.htm ...

  3. user.table.column, table.column 或列说明无效

    Oracle统计采用别名出错(user.table.column, table.column 或列说明无效) >>>>>>>>>>>& ...

  4. Python实战:美女图片下载器,海量图片任你下载

    Python应用现在如火如荼,应用范围很广.因其效率高开发迅速的优势,快速进入编程语言排行榜前几名.本系列文章致力于可以全面系统的介绍Python语言开发知识和相关知识总结.希望大家能够快速入门并学习 ...

  5. 使用linq语句获取指定条数的记录

    //获得指定个数的子文件夹,用于分页 var pageAlbums = (from SPFolder pf in lstSubAlbums select pf)                     ...

  6. sql - 选出指定范围的行

    Select no=Identity(int,1,1),* Into #temptable From dbo.tName order by fName --利用Identity函数生成记录序号 Sel ...

  7. 分享一张oracle scan图

  8. Delphi 类方法和普通方法的区别 .

    //类声明  TMyClass = class  public    class procedure MyProc;  //类方式    constructor Create;      //Crea ...

  9. 中文翻译:pjsip文档(四)之ICE Session的使用方法

    1:pjsip教程(一)之PJNATH简介 2:pjsip教程(二)之ICE穿越打洞:Interactive Connectivity Establishment简介 3:pjsip教程(三)之ICE ...

  10. [Lua]cocos framework

    package_support function cc.register(name, package) function cc.load(...) function cc.bind(target, . ...