【LeetCode】67. Add Binary
题目:
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的更多相关文章
- 【LeetCode】67. Add Binary 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BigInteger类 模拟加法 日期 题目地址:h ...
- 【一天一道LeetCode】#67. Add Binary
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given t ...
- 【LeetCode】623. Add One Row to Tree 解题报告(Python)
[LeetCode]623. Add One Row to Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problem ...
- 【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 ...
- 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告
[LeetCode]106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告(Python) 标签: LeetCode ...
- 【LeetCode】95. Unique Binary Search Trees II 解题报告(Python)
[LeetCode]95. Unique Binary Search Trees II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzh ...
- 【LeetCode】99. Recover Binary Search Tree 解题报告(Python)
[LeetCode]99. Recover Binary Search Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/p ...
- 【leetcode❤python】 67. Add Binary
class Solution(object): def addBinary(self, a, b): """ :type a: str ...
- 【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 ...
随机推荐
- Git/Github 教程
转载自 *链接(http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000)*
- 在Caffe上运行Cifar10示例
准备数据集 在终端上运行以下指令: cd caffe/data/cifar10 ./get_cifar10.sh cd caffe/examples/cifar10 ./create_cifar10. ...
- springcloud(四):熔断器Hystrix
说起springcloud熔断让我想起了去年股市中的熔断,多次痛的领悟,随意实施的熔断对整个系统的影响是灾难性的,好了接下来我们还是说正事. 熔断器 雪崩效应 在微服务架构中通常会有多个服务层调用,基 ...
- Zookeer-- 上搭建 hbase
一.HBase的相关概念1.HBase的概念:大量数据进行随机近实时读写时使用Hbase.2.HBase是一个模仿Gootable's Bigtable的,开源的.分布式的.版本化的非关系型数据库.3 ...
- iOS storyBoard中tableViewCell传值方法
一般在storyboard中传值通过identifier的值来控制segue的跳转和传值,但是,如果在tableView中,由于cell特别多,不可能创建n个identifier标识符,这里通过NSI ...
- mac下eclipse安装svn插件-subclipse
目前Eclipse最常用的svn插件莫非subclipse,在windows系统下的安装svn client和subclipse比较简单.本文介绍如何在mac安装svn插件和subclipse. 一. ...
- 数据库MySQL纯净卸载
有些人在安装MySQL后,卸载后再次安装时,一直安装不上去,到最后不得不重装系统来安装MySQL.这里教大家如何将MySQL卸载干净,不影响下次安装. 卸载过程 1.停止mysql服务 2.进行卸载 ...
- mysql之 mysql 5.6不停机主主搭建(活跃双主基于日志点复制)
环境说明:版本 version 5.6.25-log 主库ip: 10.219.24.25从库ip:10.219.24.22os 版本: centos 6.7已安装热备软件:xtrabackup 防火 ...
- sql拼接,String和Stringbuffer的问题
首先提出来一个问题: 下边两种拼字符串的方式,哪种更好一些,或者还有更好的方式? StringBuffer hql=new StringBuffer(); hql.append("from ...
- js对手机软键盘的监听
js还没有办法对手机软键盘直接进行监听的,但是可以有其他角度来判断软键盘是否弹起.比如输入框是否获取焦点等.focusin和focusout支持冒泡,对应focus和blur, 使用focusin和f ...