一.题目描写叙述

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的更多相关文章

  1. LeetCode 面试:Add Binary

    1 题目 Given two binary strings, return their sum (also a binary string). For example,a = "11&quo ...

  2. LeetCode 67. Add Binary (二进制相加)

    Given two binary strings, return their sum (also a binary string). For example,a = "11"b = ...

  3. [LeetCode] 67. Add Binary 二进制数相加

    Given two binary strings, return their sum (also a binary string). The input strings are both non-em ...

  4. 【leetcode】Add Binary

    题目简述: Given two binary strings, return their sum (also a binary string). For example, a = "11&q ...

  5. Java for LeetCode 067 Add Binary

    Given two binary strings, return their sum (also a binary string). For example, a = "11" b ...

  6. LeetCode 67. Add Binary

    Given two binary strings, return their sum (also a binary string). For example,a = "11"b = ...

  7. Java [Leetcode 67]Add Binary

    题目描述: Given two binary strings, return their sum (also a binary string). For example,a = "11&qu ...

  8. LeetCode(56)-Add Binary

    题目: Given two binary strings, return their sum (also a binary string). For example, a = "11&quo ...

  9. (String) leetcode 67. Add Binary

    Given two binary strings, return their sum (also a binary string). The input strings are both non-em ...

  10. [leetcode]67. Add Binary 二进制加法

    Given two binary strings, return their sum (also a binary string). The input strings are both non-em ...

随机推荐

  1. J.U.C并发框架源码阅读(十四)ScheduledThreadPoolExecutor

    基于版本jdk1.7.0_80 java.util.concurrent.ScheduledThreadPoolExecutor 代码如下 /* * ORACLE PROPRIETARY/CONFID ...

  2. HDU 6351暴力枚举 6354计算几何

    Beautiful Now Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)T ...

  3. hdu6038

    hdu6038 分析 求函数 \(f\) 的构成方案,\(f\) 确定下来后,\(f\) 和 \(b\) 的值也是一一对应的了( \(f(i)=b_{f(a_i)}\) ),观察 \(a\) 数组,代 ...

  4. poj2976(01分数规划)

    poj2976 题意 给出 a b 数组,一共 n 对数,其中最多可以去掉 k 对,问怎样使剩下比率(原始比率是 $ \frac{\sum_{i=1}^{n} a}{\sum_{i=1}^{n} b} ...

  5. BFS+最小生成树+倍增+LCA【bzoj】4242 水壶

    [bzoj4242 水壶] Description JOI君所居住的IOI市以一年四季都十分炎热著称. IOI市是一个被分成纵H*横W块区域的长方形,每个区域都是建筑物.原野.墙壁之一.建筑物的区域有 ...

  6. ELK帮助文档

    elasticsearch: API中文指南:https://es.xiaoleilu.com/010_Intro/15_API.html 官方文档:https://www.elastic.co/cn ...

  7. Spring bean初始化以及管理

    在完成bean实例化后,spring会根据配置文件的设定情况对bean 的属性进行初始化, 1.autowire方式 (可查找自动装配对象 但bean中要有相应属性的set方法)这是一个自动装配的机制 ...

  8. stack栈和Queue队列

    1.push将对象插入 System.Collections.Generic.Stack<T> 的顶部. Stack st = new Stack(); //栈是先进后出 st.Push( ...

  9. Hibernate中cascade和inverse的作用

    Inverse和cascade是Hibernate映射中最难掌握的两个属性.两者都在对象的关联操作中发挥作用.1.明确inverse和cascade的作用inverse 决定是否把对对象中集合的改动反 ...

  10. object references an unsaved transient instance - save the transient instance before flushing异常问题处理

    一.异常:org.hibernate.TransientObjectException: object references an unsaved transient instance - save ...