这个题目相对有点奇怪,题目如下:

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

For example,

a = "11"

b = "1"

Return "100".

可能是我思路的问题吧,我之前是在考虑先将string转成int或者double,然后将二进制转为十进制进行计算,最后再将这个十进制转为二进制再转为string返回。这个思路本来是没问题的。但是。。。。。在最后一个测试用例中,他给了一个超长的二进制串,其实也是,一个长度为100的string是很正常的,但是如果这个长度为100的string是一个二进制数,这个数字就已经挺可怕的了。我又不想强行引入big num,所以我写了一套string的二进制加法器。

题解如下:

class Solution {
public:
string addBinary(string a, string b)
{
reverse(a.begin(), a.end());
reverse(b.begin(), b.end()); int i = 0;
int flag = 0;
int lenA = a.length();
int lenB = b.length(); string c = "";
while (i < lenA && i < lenB)
{
if (a[i] - '0' + b[i] - '0' + flag >= 2)
{
c += (a[i] - '0' + b[i] - '0' + flag) % 2 + '0';
flag = 1;
}
else
{
c += a[i] - '0' + b[i] - '0' + flag + '0';
flag = 0;
}
i++;
} while (i < lenA)
{
if (a[i] - '0' + flag == 2)
{
flag = 1;
c += "0";
}
else
{
c += a[i] - '0' + flag + '0';
flag = 0;
}
i++;
} while (i < lenB)
{
if (b[i] - '0' + flag == 2)
{
flag = 1;
c += "0";
}
else
{
c += b[i] - '0' + flag + '0';
flag = 0;
}
i++;
} if (flag == 1)
{
c += "1";
} reverse(c.begin(), c.end()); return c;
}
};

在这里用flag来表示前一位是否进位,这里要注意的就是有可能本身的两位加上进位的flag会出现3的情况,所以要考虑取余。其余的都很简单。

[leetcode] 16. Add Binary的更多相关文章

  1. LeetCode 面试:Add Binary

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

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

    Given two binary strings, return their sum (also a binary string). The input strings are both non-em ...

  3. LeetCode 67. Add Binary (二进制相加)

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

  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. FP真验货客户的成品和半成品编码部分没有带尾续,导致FP规划错误 IN_SALES_ORDER数据不带CZ

    错误描述:真验货客户的成品和半成品编码部分没有带尾续,导致FP规划错误 IT角度: IN_SALES_ORDER数据不带CZ 现时前台页面数据: 现时后台数据: 一.跟进情况 1.执行SAP_SALE ...

  2. springboot CommandLineRunner

    @SpringBootApplicationpublic class Application implements CommandLineRunner { public static void mai ...

  3. 百度BAE的一些使用心得

    休眠会释放单元资源停止收费,那就不用被百度收费了

  4. 45. Jump Game II (Array; Two-Pointers,Greedy)

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  5. Javascript读写CSS属性

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  6. Python调shell

    os.system(cmd) 函数返回cmd的结束状态码,阻塞调用. os.popen(cmd) 函数返回cmd的标准输出,阻塞调用. (status, output) = commands.gets ...

  7. springmvc使用list集合实现商品列表的批量修改

    1将表单的数据绑定到List 1.1 需求 实现商品数据的批量修改. 1.2 需求分析 要想实现商品数据的批量修改,需要在商品列表中可以对商品信息进行修改,饼干且可以批量提交修改后的商品数据. 1.3 ...

  8. JTemplate学习(三)

    另一种模板写法 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xh ...

  9. 解决Pycharm添加虚拟解释器的报错问题

    问题背景: 新添加一个virtualenv环境时,需要安装指定的django==1.9.8,但是在添加解释器时,总报一个fuck egg的问题!! 解决方式如下: 1. 2. 3.搞定

  10. Codeforces 612B. Wet Shark and Bishops 模拟

    B. Wet Shark and Bishops time limit per test: 2 seconds memory limit per test: 256 megabytes input: ...