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 ...
随机推荐
- JDK7集合框架源码阅读(七) ArrayDeque
基于版本jdk1.7.0_80 java.util.ArrayDeque 代码如下 /* * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to li ...
- Python的功能模块[0] -> wmi -> 获取 Windows 内部信息
wmi模块 / wmi Module WMI (Windows Management Instrumentation) 模块可用于获取 Windows 内部信息.该模块需要 win32com 的支持, ...
- MyEclipse 10.0安装图解
- 将一个txt里的A和B谈话内容获取出来并分别保存到A和B的txt文件中
import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream;import java.io.Fi ...
- POJ 2482 Stars in Your Window 离散化+扫描法 线段树应用
遇见poj上最浪漫的题目..题目里图片以上几百词为一篇模板级英文情书.这情感和细腻的文笔深深地打动了我..不会写情书的童鞋速度进来学习.传送门 题意:坐标系内有n个星星,每个星星都有一个亮度c (1& ...
- EditText中输入信息的限制的方法
应用场景 在Android应用中有时需要EditText中只允许输入约定的一些字符,禁止输入其他字符.这里列举了一些可能的应用场景. 1. 场景一 在通讯录保存好友信息界面中填写好友的电话号码时,应当 ...
- [置顶]
kubernetes资源类型--PetSets/StatefulSet
PetSet首次在K8S1.4版本中,在1.5更名为StatefulSet.除了改了名字之外,这一API对象并没有太大变化. 注意:以下内容的验证环境为CentOS7.K8S版本1.5.2,并部署Sk ...
- [置顶]
kubernetes资源类型--DaemonSet
概念 DaemonSet能够让所有(或者特定)的节点运行同一个pod. 当节点加入到K8S集群中,pod会被(DaemonSet)调度到该节点上运行,当节点从K8S集群中被移除,被DaemonSet调 ...
- HTML5 资源导航
1,百度百科 http://baike.baidu.com/view/951383.htm 2,w3school http://www.w3school.com.cn/html5/ 3, HTML5 ...
- 容器窗口 <QTabWidget>
////////////////////////////////////////// #include "test9_2a.h" #include "M_win.h&qu ...