【一天一道LeetCode】#67. Add Binary
一天一道LeetCode
本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github
欢迎大家关注我的新浪微博,我的新浪微博
欢迎转载,转载请注明出处
(一)题目
Given two binary strings, return their sum (also a binary string).
For example,
a = “11”
b = “1”
Return “100”.
(二)解题
题意很简单,实现二进制加法逻辑
具体细节问题见代码注释
class Solution {
public:
string addBinary(string a, string b) {
string ret;
int alen = a.length();
int blen = b.length();
int i = alen-1 , j = blen-1;//从后往前加
int carry = 0;
while(i>=0 &&j>=0)//同位上都有数
{
int sum = a[i]-'0'+b[j]-'0'+carry;
ret+=sum%2+'0';
carry = sum>=2?1:0;//考虑进位
i--;j--;
}
while(i==-1&&j>=0)//a到最高位了,b还有
{
int sum = b[j]-'0'+carry;
ret+=sum%2+'0';
carry = sum==2?1:0;//考虑进位
j--;
}
while(i>=0&&j==-1)//b到最高位了,a还有
{
int sum = a[i]-'0'+carry;
ret+=sum%2+'0';
carry = sum==2?1:0;//考虑到进位
i--;
}
if(carry==1) ret+='1';
reverse(ret.begin(),ret.end());//注意对结果进行翻转,才是正确的结果
return ret;
}
};
【一天一道LeetCode】#67. Add Binary的更多相关文章
- LeetCode 67. Add Binary (二进制相加)
Given two binary strings, return their sum (also a binary string). For example,a = "11"b = ...
- [LeetCode] 67. Add Binary 二进制数相加
Given two binary strings, return their sum (also a binary string). The input strings are both non-em ...
- LeetCode 67. Add Binary
Given two binary strings, return their sum (also a binary string). For example,a = "11"b = ...
- Java [Leetcode 67]Add Binary
题目描述: Given two binary strings, return their sum (also a binary string). For example,a = "11&qu ...
- (String) leetcode 67. Add Binary
Given two binary strings, return their sum (also a binary string). The input strings are both non-em ...
- [leetcode]67. Add Binary 二进制加法
Given two binary strings, return their sum (also a binary string). The input strings are both non-em ...
- LeetCode - 67. Add Binary(4ms)
Given two binary strings, return their sum (also a binary string). The input strings are both non-em ...
- leetcode 67. Add Binary (高精度加法)
Given two binary strings, return their sum (also a binary string). For example,a = "11"b = ...
- LeetCode 67 Add Binary(二进制相加)(*)
翻译 给定两个二进制字符串,返回它们的和(也是二进制字符串). 比如, a = "11" b = "1" 返回 "100". 原文 Give ...
- leetCode 67.Add Binary (二进制加法) 解题思路和方法
Given two binary strings, return their sum (also a binary string). For example, a = "11" b ...
随机推荐
- jquery插件存档
1.选择插件selectMenu github地址:https://github.com/josiaho/selectMenu 2.选择插件bootstrap_multiselect 官方地址:htt ...
- 关于Application_End 与 Application_Start事件触发情况的测试(待续)
测试项目搭建 定义一个简单的Mvc项目,有如下文件: (1) public class Startup { public void Configuration(IAppBuilder app) { a ...
- delphi 微信(WeChat)多开源代码
在网上看到一个C++代码示例: 原文地址:http://bbs.pediy.com/thread-217610.htm 觉得这是一个很好的调用 windows api 的示例,故将其转换成了 delp ...
- sshpass笔记
sshpass简介 ssh登录的时候使用的是交互式输入,不能预先在命令行使用参数指定密码,sshpass就是为了解决这个问题的.sshpass提供非交互式输入密码的方式,可以用在shell脚本中自动输 ...
- 给大家安利一个学习angular2的视频网站
本文地址:http://blog.csdn.net/sushengmiyan 本文作者:苏生米沿 视频地址: https://egghead.io/courses/angular-2-fundamen ...
- ZooKeeper之(六)应用实例
6.1 Java API 客户端要连接 Zookeeper服务器可以通过创建 org.apache.zookeeper.ZooKeeper 的一个实例对象,然后调用这个类提供的接口来和服务器交互. Z ...
- python模块collections中namedtuple()的理解
Python中存储系列数据,比较常见的数据类型有list,除此之外,还有tuple数据类型.相比与list,tuple中的元素不可修改,在映射中可以当键使用.tuple元组的item只能通过index ...
- Android Lollipop 5.0 经典新特性回顾
*Tamic 专注移动开发! 更多文章请关注 http://blog.csdn.net/sk719887916 虽然Android已到了7.0 ,但是我们还是不能忘怀视觉革命性改变的5.0,今天回顾下 ...
- mysql 远程连接配置
近期买了阿里云服务器,服务器 安装了mysql,需要远程操作mysql数据库,但是远程不配置的话,连接不上去的.需要配置 .具体的配置如下: 先看看my.cnf是否绑定了本机,如果绑定了地址就解绑吧. ...
- zk日常运维管理
清理数据目录 dataDir目录指定了ZK的数据目录,用于存储ZK的快照文件(snapshot).另外,默认情况下,ZK的事务日志也会存储在这个目录中.在完成若干次事务日志之后(在ZK中,凡是对数据有 ...