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 ...
随机推荐
- Linked List Random Node -- LeetCode
Given a singly linked list, return a random node's value from the linked list. Each node must have t ...
- 【NOI2016】优秀的拆分
题目描述 如果一个字符串可以被拆分为 $AABB$ 的形式,其中 $A$ 和 $B$ 是任意非空字符串,则我们称该字符串的这种拆分是优秀的. 例如,对于字符串 aabaabaa,如果令 $A = \m ...
- 洛谷五月月赛 T1
做一下差分之后,把每个位置的差分数看成这个位置有多少个石子,于是每次操作就是选一个有石子的位置并把这个位置的石子移到后面的位置(如果这个位置已经是最后了那么直接扔掉). 所以就是带权石子问题了,最后判 ...
- HNOI2016 游记
题外 忽然想起去年的HNOI2015总结里好像引了一句诗: 此情可待成追忆,只是当时已惘然. Day0 唔,感觉不知道想些什么,只是觉得其实还没有做好准备,想学的东西学的仓促,想复习的东西,也只能看一 ...
- iOS AudioSession详解 Category选择 听筒扬声器切换
在你读这篇文章之前,如果你不嫌读英文太累,推荐阅读下苹果iOS Human Interface Guidelines中Sound这一章. 选择一个Category AVAudioSessionCa ...
- tcp 三次握手和四次断连深入分析:连接状态和socket API的关系----BAT 李运华
http://blog.csdn.net/yunhua_lee/article/details/40513677 http://blog.csdn.net/yah99_wolf/article/cat ...
- android开发之自定义圆形ImagView
在日常使用中我们经常会使用到圆形的图片,但是android系统中并没有默认的圆形控件,所以我们需要自己来写一个自定义的ImagView来显示一张圆形的图片,下面先看效果 详细的方法是我们自定义一个类, ...
- [置顶]
kubernetes资源类型--RC和RS
Replication Controller(RC) RC是K8S中的另一个核心概念,应用托管在K8S后,K8S需要保证应用能够持续运行,这是RC的工作内容. 主要功能 确保pod数量:RC用来管理正 ...
- apache 配置防盗
防盗链目的:防止其他网站盗用自己的网站而增加额外的流量损失 SetEnvIfNoCase Referer "^http://.*\.yourdomin\.com" local_re ...
- (转)指针的引用(*&)与指针的指针(**)
本文转载而来,转载出处:http://www.cppblog.com/doing5552/archive/2010/09/28/127994.html 在下列函数声明中,为什么要同时使用*和& ...