LeetCode 题解之Add Binary
1、题目描述

2、题目分析
使用string 的逆向指针,做二进制加法,注意进位问题就可以。
3、代码
string addBinary(string a, string b) {
string::reverse_iterator it_a = a.rbegin() ;
string::reverse_iterator it_b = b.rbegin() ;
string s;
string s0 = "";
string s1 = "";
int up = ;
while( it_a != a.rend() && it_b != b.rend() ){
if( *it_a - '' + *it_b - '' + up == ){
s = s1 + s;
up = ;
}else if( *it_a - '' + *it_b - '' + up == ){
s = s0 + s;
up = ;
}else if( *it_a - '' + *it_b - '' + up == ){
s = s0 + s;
up=;
}else if( *it_a - '' + *it_b - '' + up == ){
s = s1 + s;
up = ;
}
++it_a ;
++it_b ;
}
while( it_a != a.rend() ){
if( *it_a - '' + up == ){
s = s0 + s;
up = ;
}else if( *it_a - '' + up == ){
s = s1 + s;
up = ;
}else{
s = s0 + s;
up = ;
}
++it_a ;
}
while( it_b != b.rend() ){
if( *it_b - '' + up == ){
s = s0 + s;
up = ;
}else if(*it_b - '' + up == ){
s = s1 + s;
up =;
}else{
s = s0 + s;
up = ;
}
++it_b;
}
if( up == ){
s = s1 + s;
}
return s;
}
LeetCode 题解之Add Binary的更多相关文章
- # Leetcode 67:Add Binary(二进制求和)
Leetcode 67:Add Binary(二进制求和) (python.java) Given two binary strings, return their sum (also a binar ...
- leetcode解题:Add binary问题
顺便把之前做过的一个简单难度的题也贴上来吧 67. Add Binary Given two binary strings, return their sum (also a binary strin ...
- leetcode笔记:Add Binary
一.题目描写叙述 Given two binary strings, return their sum (also a binary string). For example, a = "1 ...
- 【LeetCode】67. Add Binary 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BigInteger类 模拟加法 日期 题目地址:h ...
- 【一天一道LeetCode】#67. Add Binary
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given t ...
- leetcode题解2. Add Two Numbers
题目: You are given two non-empty linked lists representing two non-negative integers. The digits are ...
- 【LeetCode】67. Add Binary
题目: Given two binary strings, return their sum (also a binary string). For example,a = "11" ...
- LeetCode算法题-Add Binary(Java实现)
这是悦乐书的第157次更新,第159篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第16题(顺位题号是67).给定两个二进制字符串,返回它们的总和(也是二进制字符串).输 ...
- 《LeetBook》LeetCode题解(2):Add Two Numbers [M]
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
随机推荐
- SQLite使用入门
什么是SQLite SQLite是一款非常轻量级的关系数据库系统,支持多数SQL92标准.SQLite在使用前不需要安装设置,不需要进程来启动.停止或配置,而其他大多数SQL数据库引擎是作为一个单独的 ...
- VUE之文字跑马灯效果
VUE之文字跑马灯效果 1.效果演示 2.相关代码 <!DOCTYPE html> <html lang="en"> <head> <me ...
- android studio生成aar包并在其他工程引用aar包
1.aar包是android studio下打包android工程中src.res.lib后生成的aar文件,aar包导入其他android studio 工程后,其他工程可以方便引用源码和资源文件 ...
- HTTP状态码列表
经常用的http状态码 1xx消息——请求已被服务器接收,继续处理2xx成功——请求已成功被服务器接收.理解.并接受3xx重定向——需要后续操作才能完成这一请求4xx请求错误——请求含有词法错误或者无 ...
- NoSQL之Cassandra
http://www.cnblogs.com/LBSer/p/3328841.html 9月初听了一个讲座,演讲者是张月同学,他给我们分享了Cassandra nosql数据库,讲得很精彩,听完之后收 ...
- Node.js最新Web技术栈(2016年4月)
Node.js是比较简单的,只有你有前端js基础,那就按照我的办法来吧!一周足矣,虽然这版上了es语法,但依然是可以简单写,也可以难写,参见<全栈工程师之路-Node.js>,里面讲了No ...
- rake aborted! You have already activated rake 10.1.0, but your Gemfile requires rake 10.0.3. Using bundle exec may solve this.
问题: wyy@wyy:~/moumentei-master$ rake db:createrake aborted!You have already activated rake 10.1.0, b ...
- mysql/mariadb 数据库配置
1. 启动mariadb systemctl start mariadb 2. 设置开机启动mariadb systemctl enable mariadb 一.修改用户密码,以root为例 1. ...
- c#基础学习(0628)之使用进程打开指定的文件、模拟磁盘打开文件
使用进程打开指定的文件 模拟磁盘打开文件 class Program { static void Main(string[] args) { while(true) { Console.WriteLi ...
- JavaScript push()函数追加数组数据
将数据追加到一个数组末尾的最简单的方法是通过 push() 函数. .push() 允许有一个或多个参数,并把它“push”到数组的末尾. var arr = [1,2,3];arr.push(4); ...