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. bzoj 2938 AC自动机 + dfs判环

    #include<bits/stdc++.h> #define LL long long #define ll long long #define fi first #define se ...

  2. 【笔试题】精选30道Java笔试题解答

    转自于:精选30道Java笔试题解答 精选30道Java笔试题解答 1. 下面哪些是Thread类的方法() A. start() B. run() C. exit() D. getPriority( ...

  3. 一个通用的php正则表达式匹配或检测或提取特定字符类

      在php开发时,日常不可或缺地会用到正则表达式,可每次都要重新写,有时忘记了某一函数还要翻查手册,所以,抽空写了一个关于日常所用到的正则表达式区配类,便于随便移置调用.(^_^有点偷懒). /*/ ...

  4. AttributeError: 'ForeignKey' object has no attribute 're' 解决办法

    使用 field_object.rel.model.objects.filter(**db_condition) 报错 forekey中存在rel,为什么不能调用? 通过以下语句观察 print(fi ...

  5. 面向对象编程课程(OOP)第一单元总结

    漫长旅程中还算不错的开头 在本学期开始之前,我按照助教们所给的寒假作业指导书自学了Java语言的相关知识,了解了Java语言的基本语法,输出一句“Hello World!”,掌握了基本的一些输入输出方 ...

  6. 单能X射线产生方法

    主要是荧光 利用布拉格准则, 关键词如下.. 国内有些专利 monochromating crystal spectrometer 物理实验设备名称翻译 ... 单色光检糖计 monochromati ...

  7. 数据库SQL归纳(二)

    数据定义功能 对象 创建 修改 删除 架构 CREATE SCHEMA DROP SCHEMA 表 CREATE TABLE ALTER TABLE DROP TABLE 视图 CREATE VIEW ...

  8. BZOJ 2738 矩阵乘法(整体二分+二维树状数组)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2738 [题目大意] 给出一个方格图,询问要求求出矩阵内第k小的元素 [题解] 我们对答 ...

  9. [xsy2123]毛毛虫

    题意:有一棵带点权的树,链修改是把$(x,y)$这条链和与其相邻的节点都加上一个数,查询是问$(x,y)$这条链和与其相邻的节点的权值和 学到了一个新姿势? 考虑树链剖分,在剖重链时每次给当前节点的儿 ...

  10. 【序列莫队+二分答案+树状数组】POJ2104-K-th Number

    [题目大意] 给出一个长度为n的序列和m组查询(i,j,k),输出[i,j]中的第k大数. [思路] 先离散化然后莫队分块.用树状数组来维护当前每个值的个数,然后对于每次询问二分答案即可. 又一次实力 ...