leetcode笔记:Add Binary
一.题目描写叙述
Given two binary strings, return their sum (also a binary string).
For example,
a = “11”
b = “1”
Return ”100”.
二.解题技巧
这道题考察两个二进制数相加,考虑到输入的是两组string,同一时候注意在运算时从左到右各自是从低位到高位,因此须要考虑对输入进行翻转处理,中间二进制树相加部分没有过多的设计障碍。主要是计算进位;在两组数据相加完毕后,还须要考虑最高位的进位问题。
三.演示样例代码
#include <iostream>
#include <string>
using namespace std;
class Solution
{
public:
string AddBinary(string a, string b)
{
size_t size = a.size() > b.size() ? a.size() : b.size();
reverse(a.begin(), a.end());
reverse(b.begin(), b.end());
int CarryBit = 0; // 进位
string result; // 用于存放结果
for (size_t i = 0; i < size; i++)
{
int a_num = a.size() > i ? a[i] - '0' : 0;
int b_num = b.size() > i ?
b[i] - '0' : 0;
int val = (a_num + b_num + CarryBit) % 2;
CarryBit = (a_num + b_num + CarryBit) / 2; // 进位
result.insert(result.begin(), val + '0');
}
if (CarryBit == 1)
result.insert(result.begin(), '1');
return result;
}
};
測试代码:
#include "AddBinary.h"
using std::cout;
using std::cin;
int main()
{
string a, b;
cout << "Input the first string: ";
cin >> a;
cout << "\nInput the second string: ";
cin >> b;
Solution s;
string result = s.AddBinary(a, b);
cout << "\nThe Add Binary result is : " << result;
return 0;
}
几个測试结果:
leetcode笔记:Add Binary的更多相关文章
- LeetCode 面试:Add Binary
1 题目 Given two binary strings, return their sum (also a binary string). For example,a = "11&quo ...
- 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】Add Binary
题目简述: Given two binary strings, return their sum (also a binary string). For example, a = "11&q ...
- Java for LeetCode 067 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). 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 ...
- LeetCode(56)-Add Binary
题目: Given two binary strings, return their sum (also a binary string). For example, a = "11&quo ...
- (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 ...
随机推荐
- [BZOJ1191][HNOI2006]超级英雄Hero 类似二分图的最大匹配
1191: [HNOI2006]超级英雄Hero Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 4740 Solved: 2162[Submit][ ...
- linux文件名匹配
* 匹配文件名中的任何字符串,包括空字符串. ? 匹配文件名中的任何单个字符. [...] 匹配[ ]中所包含的任何字符. [!...] 匹配[ ]中非感叹号!之后的字符. 如: s* ...
- [Python Cookbook] Pandas: Indexing of DataFrame
Selecting a Row df.loc[index] # if index is a string, add ' '; if index is a number, no ' ' or df.il ...
- java.sql.SQLException: Access denied for user 'roo'@'localhost' (using password: YES)
初学mysql,安装了mysql8.0.11,激动的用jdbc连接数据库,出现error,折腾了三天依旧无解,最后无奈装了比较稳定的mysql5.5,问题得以解决,很迷,但只要error没了就开心. ...
- 用kermit通过串口往nandflash任意地址里烧写任何文件!
1.安装kermit #apt-get install ckermit 2.使用kermit之前,在用户宿主目录下(/home/用户名/)创建一个名为.kermrc的配置文件,内容如下 : set l ...
- python3使用configparser解析配置文件
http://www.jb51.net/article/87402.htm 需要注意的是每一个字段后面的值外面没有引号,切记,自己第一次配置时,加了引号,搞了半天 没找到错误,, 在用Python做开 ...
- python软件工程知识
软件工程知识 3.1 程序设计过程中,常用伪代码来"思考"一个程序,在将伪代码程序转换成python程序. 3.2 所有python程序都可以给予6类控制结构来创建(顺序,if, ...
- 在c++代码中执行bat文件 【转】
我想在c++代码中执行磁盘上的一个bat文件. 这个bat文件的完整路径是:E:\\7z\\my7z.bat. 方法一: system("E:\\7z\\my7z.bat"); s ...
- osgconv使用指南(转)
osgconv是一种用来读取3D数据库以及对它们实施一些简单的操作的实用应用程序,同时也被称作 一种专用3D数据库工具. 用osgconv把其他格式的文件转换为OSG所支持的格式 osgconv是一种 ...
- 线性判别分析(Linear Discriminant Analysis, LDA)算法初识
LDA算法入门 一. LDA算法概述: 线性判别式分析(Linear Discriminant Analysis, LDA),也叫做Fisher线性判别(Fisher Linear Discrimin ...