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 ...
随机推荐
- 2017-2018 ACM-ICPC, NEERC, Southern Subregional Contest (Online Mirror, ACM-ICPC Rules, Teams Preferred)
先把代码扔上来 E. Field of Wonders time limit per test 3 seconds memory limit per test 256 megabytes input ...
- poj 3617Best Cow Line
Description FJ is about to take his N (1 ≤ N ≤ 2,000) cows to the annual"Farmer of the Year&quo ...
- 九度oj 题目1536:树的最小高度
题目描述: 给定一棵无向树, 我们选择不同的节点作为根节点时,可以得到不同的高度(即树根节点到叶子节点距离的最大值), 现在求这棵树可能的最低高度. 输入: 输入可能包含多个测试案例. 对于每个测试案 ...
- The BLOB and TEXT Types
官网参考:https://dev.mysql.com/doc/refman/5.7/en/blob.html 字符串类型对应的存储需求 Data Type Storage Required CHAR( ...
- 14-new和this
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- Goldbach
Description: Goldbach's conjecture is one of the oldest and best-known unsolved problems in number t ...
- 「CodePlus 2018 4 月赛」最短路
$n \leq 100000$,$m \leq 500000$的有向图,两点之间还可以以$a \ \ xor \ \ b$的代价从$a$到$b$,问$s$到$t$的最短路. 被自己蠢哭QAQ 首先两个 ...
- python socket非阻塞及python队列Queue
一. python非阻塞编程的settimeout与setblocking+select 原文:www.th7.cn/Program/Python/201406/214922.shtml 侧面认证Py ...
- json常见用法-loads、jumps、load、jump
这一篇博客的目的主要是想说明一个问题:干什么事情要抓住重点,不要力求完美,不要追求那种'大而全'的办事方式,因为时间是有限的,而客观事物(这里主要指技术方面的知识)是无限的,so,anyway! 1. ...
- CPU 内存 硬盘的区别
第一点:CPU 是处理器,内存和硬盘是存储器,受CPU 的控制. 第二点:由于内存的速度很快,在电脑运行的过程中,CPU通常只与内存交换数据,但内存断电数据就会全部丢失,因此电脑使用硬盘作为主要的存 ...