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. iwebshop 自动给css js链接加版本信息

    lib/core/tag_class.php case 'theme:': $path = $matches[4]; $exts = strtolower(substr($matches[4], st ...

  2. 运行python程序不显示cmd的方法

    运行python程序的时候会在背景显示一个cmd,要想不显示其实很简单(虽然是我找了1个小时...才了解的基本知识) 方法1:pythonw xxx.py 方法2:将.py改成.pyw (这个其实就是 ...

  3. 【转载】FloatingActionButton源码解析

    原文地址:https://github.com/Rowandjj/my_awesome_blog/blob/master/fab_anlysis/README.md loatingActionButt ...

  4. [HDU1542]Atlantis(扫描线+线段树)

    Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...

  5. 【高精度】【找规律】Gym - 101243B - Hanoi tower

    题意:给你一个经典的汉诺塔递归程序,问你最少几步使得三个柱子上的盘子数量相同.(保证最开始盘子数量可以被3整除) 规律:ans(n)=2^(2*n/3-1)+t(n/3). t(1)=0. t(n)= ...

  6. 【贪心】【堆】bzoj2590 [Usaco2012 Feb]Cow Coupons

    每个物品有属性a,b 考虑在仅仅用光优惠券时的最优方案. 显然是按照b排序,取前K个. 但是我们还要尽可能去取剩余的. 假设朴素地取剩余的话,应该把剩余的对a排序,然后尽量去取. 但是有可能对其用优惠 ...

  7. 【manacher】HDU4513-吉哥系列故事——完美队形II

    [题目大意] 求最长回文队伍且队伍由中间向两边递减. [思路] 和字符串一样的做法,在递推的时候增加判断条件:a[i-p[i]]<=a[i-p[i]+2]. #include<iostre ...

  8. POJ 2139 Six Degrees of Cowvin Bacon (弗洛伊德最短路)

    题意:奶牛拍电影,如果2个奶牛在同一场电影里演出,她们的合作度是1,如果ab合作,bc合作,ac的合作度为2,问哪一头牛到其他牛的合作度平均值最小再乘100 思路:floyd模板题 #include& ...

  9. KVM工具libvirt、virsh、virt-manager的简单介绍

    KVM虚拟化中libvirt是目前使用最为广泛的对KVM虚拟机进行管理的工具和应用程序接口,而且一些常用的虚拟机管理工具(virsh.virt-install.virt-manager等)和云计算框架 ...

  10. 为什么fis没有freemarker的解决方案啊?_前端吧_百度贴吧

    为什么fis没有freemarker的解决方案啊?_前端吧_百度贴吧 fis-plus:适用于PHP+Smarty后端选型jello:适用于Java+Velocity后端选型goiz:适用于go+ma ...