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 ...
随机推荐
- [BZOJ1069][SCOI2007]最大土地面积 凸包+旋转卡壳
1069: [SCOI2007]最大土地面积 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 3669 Solved: 1451[Submit][Sta ...
- 计蒜客 25985.Goldbach-米勒拉宾素数判定(大素数) (2018 ACM-ICPC 中国大学生程序设计竞赛线上赛 B)
若干年之前的一道题,当时能写出来还是超级开心的,虽然是个板子题.一直忘记写博客,备忘一下. 米勒拉判大素数,关于米勒拉宾是个什么东西,传送门了解一下:biubiubiu~ B. Goldbach 题目 ...
- 计蒜客 28206.Runway Planning (BAPC 2014 Preliminary ACM-ICPC Asia Training League 暑假第一阶段第一场 F)
F. Runway Planning 传送门 题意简直就是有毒,中间bb一堆都是没用的,主要的意思就是度数大于180度的就先减去180度,然后除以10,四舍五入的值就是答案.如果最后结果是0就输出18 ...
- servlet之request
1. request的setAttribute与getAttribute方法一般都是成对出现的,首先通过setAttribute方法设置属性与属性值,然后通过getAttribute方法根据属性获取到 ...
- AppScan入门工作原理详解
AppScan,即 AppScan standard edition.其安装在 Windows 操作系统上,可以对网站等 Web 应用进行自动化的应用安全扫描和测试. Rational AppScan ...
- 洛谷——P1469 找筷子
P1469 找筷子 题目描述 经过一段时间的紧张筹备,电脑小组的“RP餐厅”终于开业了,这天,经理LXC接到了一个定餐大单,可把大家乐坏了!员工们齐心协力按要求准备好了套餐正准备派送时,突然碰到一个棘 ...
- liunx安装telnet
安装环境:CentOS 6.4 一.安装telnet 1.检测telnet-server的rpm包是否安装 [root@localhost ~]# rpm -qa telnet-server 若无 ...
- iOS 设置系统音量和监听系统音量变化
很简单的调用 首先在工程引入MediaPlayer.framework #import <MediaPlayer/MediaPlayer.h> 1. 获取系统音量 // 获取系统音量 MP ...
- 安裝jpeg-6b png error错误解决方法
安裝jpeg-6b png error错误解决方法 默认安裝jpeg-6b shell> wget ftp://ftp.uu.net/graphics/jpeg/jpegsrc.v6b.tar. ...
- uprobes issue with oracle 12c
https://mahmoudhatem.wordpress.com/2017/03/21/uprobes-issue-with-oracle-12c/