LeetCode(67)题解: Add Binary
https://leetcode.com/problems/add-binary/
题目:
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 c="",d="";
if(a==""&&b=="")
return "";
int a_t=a.size(),b_t=b.size(),min_t=min(a_t,b_t),max_t=max(a_t,b_t);
int tmp_a,tmp_b,jinyi=;
for(int i=;i<max_t;i++){
if(i<a_t)
tmp_a=a[a_t-i-]-'';
else
tmp_a=;
if(i<b_t)
tmp_b=b[b_t-i-]-'';
else
tmp_b=;
c.push_back((tmp_a+tmp_b+jinyi)%+'');
if(tmp_a+tmp_b+jinyi>=)
jinyi=;
else
jinyi=;
}
if(jinyi==)
c.push_back('');
for(int i=c.size()-;i>=;i--)
d.push_back(c[i]);
return d;
}
};
LeetCode(67)题解: 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
题目 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).给定两个二进制字符串,返回它们的总和(也是二进制字符串).输 ...
- LeetCode OJ:Add Binary(二进制相加)
Given two binary strings, return their sum (also a binary string). For example,a = "11"b = ...
- [LeetCode] 415 Add Strings && 67 Add Binary && 43 Multiply Strings
这些题目是高精度加法和高精度乘法相关的,复习了一下就做了,没想到难住自己的是C++里面string的用法. 原题地址: 415 Add Strings:https://leetcode.com/pro ...
- 67. Add Binary【LeetCode】
67. Add Binary Given two binary strings, return their sum (also a binary string). For example,a = &q ...
- LeetCode练题——67. Add Binary
1.题目 67. Add Binary——easy Given two binary strings, return their sum (also a binary string). The inp ...
随机推荐
- Disqus 升级到3.0以上版本的评论同步问题
Disqus从2.*升级3.*时,Knowlege Base的文章不显示Disqus评论, 解决方法:在Disqus的Advanced Settings中勾选Render Comments JavaS ...
- mysql 处理数据库中的重复行
1.对于主键和唯一索引,可以用IGNORE关键字,遇到重复记录会直接忽略插入记录,返回0.insert ignore into table_name ('id','name') values (1,' ...
- Snmp的学习总结(二)
一.SNMP简介 SNMP指的是简单网络管理协议.它属于TCP/IP五层协议中的应用层协议.它提供了一种简单和方便的模式来管理网络中的各个元素.这里的元素就是各个被管理的对象,可以是因特网中的某个硬件 ...
- uva 11798 相对运动的最小最大距离
C Dog Distance Input Standard Input Output Standard Output Two dogs, Ranga and Banga, are running ra ...
- poj 2987 Firing
Firing Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 10696 Accepted: 3226 Descript ...
- 纯干货,Mysql innodb的ACID特性是怎么实现的?以及高并发情况下会出现的问题
首先说说什么是ACID: 它们分别是Atomicity(原子性),Consistency(一致性),Isolation(隔离性),Transaction(持久性) 原子性: 意为单个事务里的多个操作要 ...
- Security arrangements for extended USB protocol stack of a USB host system
Security arrangements for a universal serial bus (USB) protocol stack of a USB host system are provi ...
- Compose
安装与卸载 Compose 支持 Linux.macOS.Windows 10 三大平台. Compose 可以通过 Python 的包管理工具 pip 进行安装,也可以直接下载编译好的二进制文件使用 ...
- JS基础之BOM对象
BOM 对象 JavaScript分为 ECMAScript,DOM,BOM. BOM(浏览器对象模型),可以对浏览器窗口进行访问和操作.使用 BOM,开发者可以移动窗口.改变状态栏中的文本以及执行其 ...
- Educational Codeforces Round 51 (Rated for Div. 2) The Shortest Statement
题目链接:The Shortest Statement 今天又在群里看到一个同学问$n$个$n$条边,怎么查询两点直接最短路.看来这种题还挺常见的. 为什么最终答案要从42个点的最短路(到$x,y$) ...