【一天一道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 ...
随机推荐
- 【阿里聚安全·安全周刊】Google“手枪”替换 | 伊朗中央银行禁止加密货币
本周七个关键词:Google"手枪"替换丨IOS 漏洞影响工业交换机丨伊朗中央银行禁止加密货币丨黑客针对医疗保健丨付费DDoS攻击丨数据获利的8种方式丨MySQL 8.0 正式版 ...
- Android POJO 转换器 —> RapidOOO
博客搬迁至https://blog.wangjiegulu.com RSS订阅:https://blog.wangjiegulu.com/feed.xml 原文链接:https://blog.wang ...
- asp.net使用session完成: 从哪个页面进入登录页面,登录成功还回到那个页面
1.在Login.aspx页面Load中加入 if (!IsPostBack && Request.UrlReferrer != null) { Session[ " ...
- Android简易实战教程--第四十四话《ScrollView和HorizontalScrollView简单使用》
一.ScrollView 由于手机屏幕的高度有限,当普通布局放不下现实和的内容时,ScrollView视图(滚动视图)就会派上用场,因为数据可以往下滚动显示. 二.HorizontalScrollVi ...
- Linux 高性能服务器编程——高级I/O函数
重定向dup和dup2函数 #include <unistd.h> int dup(int file_descriptor); int dup2(int file_descriptor_o ...
- PLSQL程序编写杂烦数据表信息编写批量排版
--PLSQL程序编写杂烦数据表信息编写批量排版 SELECT 'cra.' || lower(t.column_name) ||',' FROM dba_tab_columns t WHERE t. ...
- Github上的Android项目介绍之ListViewAnimation(针对listView item的侧滑菜单)(1)
demo源码,需要可以下载 1.这是一个github开源项目,先去github上面下载,github下载地址. 2.将SwipeMenuListView项目,导入,然后新建项目如果要引用,要设置为相应 ...
- activity的启动模式和栈管理
在学习Android的过程中,Intent是我们最常用Android用于进程内或进程间通信的机制,其底层的通信是以Binder机制实现的,在物理层则是通过共享内存的方式实现的. Intent ...
- Gazebo機器人仿真學習探索筆記(一)安裝與使用
Gazebo提供了多平臺的安裝和使用支持,大部分主流的linux,Mac以及Windows,這裏結合ROS以Ubuntu爲例進行介紹. 首先是參考資料:http://gazebosim.org/tut ...
- Dynamics CRM 非声明验证方式下连接组织服务的两种方式的性能测试
今天看了勇哥的博文"http://luoyong0201.blog.163.com/blog/static/1129305201510153391392/",又认识到了一种新的连接 ...