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

For example,

a = "11"

b = "1"

Return "100".

Show Tags

Have you been asked this question in an interview?

public class Solution {
public String addBinary(String a, String b) {
if (a == null || a.length() == 0) {
return b;
}
if (b == null || b.length() == 0) {
return a;
}
int addBits = Math.min(a.length(), b.length());
StringBuffer sb = new StringBuffer();
int bitPlus = 0;
for (int i = 0; i < addBits; i++) {
int aBit = a.charAt(a.length() - 1 - i) - '0';
int bBit = b.charAt(b.length() - 1 - i) - '0';
int cur = aBit + bBit + bitPlus;
bitPlus = cur / 2;
cur = cur % 2;
sb.insert(0, String.valueOf(cur));
}
return doOther( a, b, addBits, bitPlus, sb); }
public String doOther(String a, String b, int addBits,int bitPlus,StringBuffer sb) {
if (b.length() > a.length()) {
doOther( b, a, addBits, bitPlus, sb);
} else {
for (int i = addBits; i < a.length();i++)
{
int aBit = a.charAt(a.length() - 1 - i ) - '0';
int cur = aBit + bitPlus;
bitPlus = cur / 2;
cur = cur % 2;
sb.insert(0,String.valueOf(cur));
}
if (bitPlus == 1) {
sb.insert(0,"1");
}
return sb.toString();
}
return sb.toString(); }
}

LeetCode Add Binary |My Solution的更多相关文章

  1. LeetCode: Add Binary 解题报告

    Add BinaryGiven two binary strings, return their sum (also a binary string). For example,a = "1 ...

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

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

  3. [leetcode]Add Binary @ Python

    原题地址:https://oj.leetcode.com/problems/add-binary/ 题意: Given two binary strings, return their sum (al ...

  4. Leetcode Add Binary

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

  5. [Leetcode] add binary 二进制加法

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

  6. LeetCode——Add Binary

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

  7. LeetCode Add Binary 两个二进制数相加

    class Solution { public: string addBinary(string a, string b) { if(a==""&&b==" ...

  8. LeetCode 面试:Add Binary

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

  9. [LeetCode] 415 Add Strings && 67 Add Binary && 43 Multiply Strings

    这些题目是高精度加法和高精度乘法相关的,复习了一下就做了,没想到难住自己的是C++里面string的用法. 原题地址: 415 Add Strings:https://leetcode.com/pro ...

随机推荐

  1. Mybatis框架-2

    1.Mybatis中的接口形式 在Mybatis中使用接口形式将通过代理对象调用方法,从而实现sql的执行 1)定义一个接口 package mapper; import java.util.List ...

  2. nodejs安装sharp出错的问题

    PS D:\report\source\lpd-planning-allocation> yarn yarn install v1.3.2 [/] Resolving packages... [ ...

  3. 【已解决】php本地环境超级慢

    打开 C:\Windows\System32\drivers\etc去掉 #127.0.0.1 localhost 前面的#保留 #::1 localhost 前面的#

  4. jQuery筛选

    1.filter筛选出与指定表达式匹配的元素集合 html: <p>Hello</p><p>Hello Again</p><p class=&qu ...

  5. Oracle REGEXP

    ORACLE中的支持正则表达式的函数主要有下面四个: 1,REGEXP_LIKE :与LIKE的功能相似 2,REGEXP_INSTR :与INSTR的功能相似 3,REGEXP_SUBSTR :与S ...

  6. EasyUI学习总结(六)——EasyUI布局(转载)

    本文转载自:http://www.cnblogs.com/xdp-gacl/p/4088198.html 一.EasyUI布局介绍 easyUI布局容器包括东.西.南.北.中五个区域,其中中心面板是必 ...

  7. VB查询数据库之报表——机房收费系统总结(六)

    我们要用一个软件做报表的模板.然后在VB里面添加部件.代码调用模板,详细步骤如下. 一.下载安装 首先做报表要下载安装Grid++Report 4.5 报表设计器 点击下载(内含破解补丁) 二.制作模 ...

  8. 【二项式定理】【推导】计蒜客17115 2017 ACM-ICPC 亚洲区(西安赛区)网络赛 B. Coin

    题意:投一枚硬币向上的概率是q/p.问你投K枚硬币,向上的枚数为偶数枚的概率是? 要求的即为. 这个东西是个二项展开式的偶数项系数和,来,我们复习一下高中数学,设f(x)=(ax+b)^n,则其偶数项 ...

  9. 【树形dp】Codeforces Round #405 (rated, Div. 1, based on VK Cup 2017 Round 1) B. Bear and Tree Jumps

    我们要统计的答案是sigma([L/K]),L为路径的长度,中括号表示上取整. [L/K]化简一下就是(L+f(L,K))/K,f(L,K)表示长度为L的路径要想达到K的整数倍,还要加上多少. 于是, ...

  10. GIL,queue,进程池与线程池

    GIL 1.什么是GIL(这是Cpython解释器) GIL本质就是一把互斥锁,既然是互斥锁,原理都是一样的,都是让多个并发线程同一时间只能有一个执行 即:有了GIL的存在,同一进程内的多个线程同一时 ...