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. TCP握手协议简述

    TCP握手协议简述在TCP/IP协议中,TCP协议提供可靠的连接服务,采用三次握手建立一个连接.第一次握手:建立连接时,客户端发送syn包(syn=j)到服务器,并进入SYN_SEND状态,等待服务器 ...

  2. 《深入理解Android2》读书笔记(八)

    接上篇<深入理解Android2>读书笔记(七) AMS中的进程管理 AMS对进程的管理仅涉及两个方面 1.调节进程的调度优先级和调度策略 2.调节进程的oom值 调度优先级和调度策略 1 ...

  3. 最全python面试题

    Python语言特性 1 Python的函数参数传递 看两个例子: a = 1 def fun(a): a = 2 fun(a) print a # 1 a = [] def fun(a): a.ap ...

  4. NOIP2017酱油记

    分数线终于出了,于是大胆地写下了这篇博文. 提高组Day1: T1: 打开PDF就感觉到一股骚气,不忘初心什么鬼啊??T1是数论??好一个不忘初心... 看一下样例1:3 7:11 莫不是3*7-10 ...

  5. Linux基础系列-Day2

    基础命令(文件内容管理) 1.cat:在当前终端显示文本文件内容 格式:cat [文件路径] -n 从1开始对所有输出的行数编号 -b 和-n相似,只不过对于空白行不编号:2.head:从文件内容开头 ...

  6. [xsy2289]B

    题意:给一棵树,一次操作定义为删掉一条树边再加一条边,并且满足加完边后这还是一棵树,问在进行不超过$k$次操作后能构造出多少种不同的树 首先...矩阵树定理在边有边权的时候同样适用,这时可以把它看成重 ...

  7. [BZOJ3529]数表

    假设$n\leq m$,我们先不考虑$\leq a$的限制 $\sum\limits_{i=1}^n\sum\limits_{j=1}^m\sigma((i,j))=\sum\limits_{T=1} ...

  8. 【Java】【滚动数组】【动态规划】UVA - 11137 - Ingenuous Cubrency

    滚动数组优化自己画一下就明白了. http://blog.csdn.net/u014800748/article/details/45849217 解题思路:本题利用递推关系解决.建立一个多段图,定义 ...

  9. Tomcat上java.lang.IllegalStateException: Optional int parameter 'id' is not present

    今日, 本人在tomcat+spring mvc平台的服务器上遇到java.lang.IllegalStateException: Optional int parameter 'id' is not ...

  10. 在XC2440上实现使用U盘自动更新系统的uboot

    转:http://blog.chinaunix.net/uid-22030783-id-3347599.html 此版本uboot在XC2440_uboot_V1.3版本上修改得来,是为我们的一个大客 ...