题目:

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

For example,
a = "11"
b = "1"
Return "100".

提示:

此题我的第一反应是把输入的两个字符串转化为数字,相加以后再把结果转化为二进制输出,但是测试用例中会有很大的输入,此时即使是long long型也会造成溢出,所以只能用最传统的由低位到高位逐位相加的方法去做。

代码:

class Solution {
public:
string addBinary(string a, string b) {
int len_a = a.size() - , len_b = b.size() - ;
int dif = abs(len_a - len_b);
string prev = "";
for (int i = ; i < dif; ++i) prev += '';
if (len_a < len_b) a = prev + a;
else b = prev + b;
int len = a.size(), step = ;
vector<int> v;
for (int i = len - ; i > -; --i) {
step += a[i] + b[i] - ('' << );
v.push_back(step % );
step = step >> ;
}
if (step) v.push_back(step % );
string res;
for (int i = v.size() - ; i > -; --i) {
res += ('' + v[i]);
}
return res;
}
};

【LeetCode】67. Add Binary的更多相关文章

  1. 【LeetCode】67. Add Binary 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BigInteger类 模拟加法 日期 题目地址:h ...

  2. 【一天一道LeetCode】#67. Add Binary

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given t ...

  3. 【LeetCode】623. Add One Row to Tree 解题报告(Python)

    [LeetCode]623. Add One Row to Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problem ...

  4. 【leetcode】998. Maximum Binary Tree II

    题目如下: We are given the root node of a maximum tree: a tree where every node has a value greater than ...

  5. 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告

    [LeetCode]106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告(Python) 标签: LeetCode ...

  6. 【LeetCode】95. Unique Binary Search Trees II 解题报告(Python)

    [LeetCode]95. Unique Binary Search Trees II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzh ...

  7. 【LeetCode】99. Recover Binary Search Tree 解题报告(Python)

    [LeetCode]99. Recover Binary Search Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/p ...

  8. 【leetcode❤python】 67. Add Binary

    class Solution(object):    def addBinary(self, a, b):        """        :type a: str  ...

  9. 【LeetCode】 99. Recover Binary Search Tree [Hard] [Morris Traversal] [Tree]

    Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...

随机推荐

  1. Visual Studio Package 插件开发之自动生成实体工具

    前言 这一篇是VS插件基于Visual Studio SDK扩展开发的,可能有些朋友看到[生成实体]心里可能会暗想,T4模板都可以做了.动软不是已经做了么.不就是读库保存文件到指定路径么…… 我希望做 ...

  2. 学习spring前,先了解了解代理模式

    什么是代理模式 举个例子,我是一个包租公,我现在想卖房,但是我不想麻烦,每天被电话骚扰,所以这个时候我找了楼下一个中介,让他帮我代理这些事,那么他自然有租房的方法.以后如果有人想租房,直接找中介就行了 ...

  3. mac上解决Resource temporarily unavailable

    Resource temporarily unavailable这种问题一般是因为当前的进程数或者文件数不够 fork: Resource temporarily unavailable 修改最大进程 ...

  4. php的八大数据类型

    1. 八大数据: bool 布尔类型:0,1:真假 integer 整形 float 浮点型 string 字符串 array 数组 object 对象,类,class resource 文件,图片, ...

  5. Spring学习笔记——02 Bean的命名及实例化

    一.Bean的命名 前一篇讲到IoC是一个管理Bean的容器,Bean多数情况下都是通过XML文件进行配置的,其中Bean的命名有以下几种方式,现在梳理一下. 1. 不指定id,只配置类名 <b ...

  6. SQL SERVER大话存储结构(4)_复合索引与包含索引

              索引这块从存储结构来分,有2大类,聚集索引和非聚集索引,而非聚集索引在堆表或者在聚集索引表都会对其 键值有所影响,这块可以详细查看本系列第二篇文章:SQL SERVER大话存储结构 ...

  7. Zepto源码分析-动画(fx fx_method)模块

    源码注释 // Zepto.js // (c) 2010-2015 Thomas Fuchs // Zepto.js may be freely distributed under the MIT l ...

  8. canvas动画——粒子系统(1)

    这个动画在很早之前就见过,当时就没迷住了.最近在学canavs动画,动手实现了一下.代码在这里.展示效果在这里. 这属于粒子系统的一种,粒子系统就是需要管理一堆粒子嘛,动画实现的关键在于,遍历这些粒子 ...

  9. iOS11和机器学习CoreML库

    随着iOS11的发布,苹果公司也正式加入了机器学习的战场.在新的iOS11中内置了CoreML,虽然还是Beta版本,但是功能已经非常强大了. 在这个CoreML库里面,已经集成了一些训练好的模型,可 ...

  10. 如何修改"DEDECMS 提示信息!"方法!

    dedecms程序使用过程中,经常有一些跳转提示信息会出现“DEDECMS 提示信息!”这几个字样. 很多朋友都想对他进行修改,改为自己网站的提示信息,其实方法也是很简单的,方法如下: 用编辑器打开i ...