【一天一道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 ...
随机推荐
- ELK学习记录一 :初识ELK
ELK是elastic公司提供的一套完整的收集日志并分析展示的产品,分别表示Elasticsearch.Logstash和kibana. (官网截个图) 先来一段个人粗浅的认识: Elasticsea ...
- Spring完全基于Java配置和集成Junit单元测试
要点: 配置继承WebApplicationInitializer的类作为启动类,相当于配置web.xml文件 使用@Configuration注解一个类,在类中的方式使用@Bean注解,则表名该方法 ...
- Java第8次实验(IO流)
参考资料 本次作业参考文件 正则表达式参考资料 第1次实验 1. 字符流与文本文件:使用 PrintWriter(写),BufferedReader(读) 参考文件:基础代码目录Student.jav ...
- Openstack: aborted: Block Device Mapping is Invalid
Issue: When you create an instance, you may encounter following exception: aborted: Block Device Map ...
- ngx.ctx
https://github.com/openresty/lua-nginx-module#ngxctx 要点 生命周期和请求一致 每个请求的ngx.ctx是相互独立的,包括ngx.location. ...
- 数据库查询优化——Mysql索引
工作一年了,也是第一次使用Mysql的索引.添加了索引之后的速度的提升,让我惊叹不已.隔壁的老员工看到我的大惊小怪,平淡地回了一句"那肯定啊". 对于任何DBMS,索引都是进行优化 ...
- Dynamics CRM 安装CRM程序系统检查界面报未将对象引用设置到对象的实例的解决方法
今天在安装CRM的时候,在系统检查阶段遇到了如下的错误,咋看之下直接是懵逼的 但不要着急,界面上有两个按钮,一个是详细信息,一个是帮助,详细信息不用看了就那一行字也看不出什么,咱们点下帮助看看,定位到 ...
- Dynamics CRM2016 Web API之获取查找字段的text及选项集的text
本篇再来介绍个web api的功能,关于lookup的text这里只是略带,因为有expand,现有的web api就能实现,主要提的是选项集的text,我们通过基本的查询api查出来的字段值只带有v ...
- 高通开发笔记---Yangtze worknote
点击打开链接 1. repo init -u git://review.sonyericsson.net/platform/manifest -b volatile-jb-mr1-yangtze 2. ...
- 十六进制字符串转化为十进制值strtoul函数
eg: NSString *strtest =@"7fffffff"; NSUInteger val = strtoul([[strtest substringWithRange: ...