作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/add-binary/description/

题目描述

Given two binary strings, return their sum (also a binary string).

The input strings are both non-empty and contains only characters 1 or 0.

Example 1:

Input: a = "11", b = "1"
Output: "100"

Example 2:

Input: a = "1010", b = "1011"
Output: "10101"

题目大意

使用字符串表示的二进制数,求他们的和,结果应该也是个字符串。

解题方法

BigInteger类

import java.math.BigInteger;
public class Solution {
public String addBinary(String a, String b) {
BigInteger a_ = new BigInteger(a,2);
BigInteger b_ = new BigInteger(b,2);
return a_.add(b_).toString(2);
}
}

模拟加法

使用的方法简单直接,之前也有类似的题。我先把两个字符串拼接成一样长的,然后再计算,能省去很多判断。如果最后的carry还存在的话,那么需要再加上一个1.

class Solution(object):
def addBinary(self, a, b):
"""
:type a: str
:type b: str
:rtype: str
"""
M, N = len(a), len(b)
if M < N:
a = "0" * (N - M) + a
else:
b = "0" * (M - N) + b
stack1 = list(a)
stack2 = list(b)
res = ""
carry = 0
while stack1 and stack2:
s1, s2 = stack1.pop(), stack2.pop()
cursum = int(s1) + int(s2) + carry
if cursum >= 2:
cursum %= 2
carry = 1
else:
carry = 0
res = str(cursum) + res
if carry:
res = "1" + res
return res

日期

2017 年 8 月 17 日
2018 年 11 月 24 日 —— 周六快乐

【LeetCode】67. Add Binary 解题报告(Python)的更多相关文章

  1. LeetCode: Add Binary 解题报告

    Add BinaryGiven two binary strings, return their sum (also a binary string). For example,a = "1 ...

  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. Java [Leetcode 67]Add Binary

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

  5. LeetCode 258 Add Digits 解题报告

    题目要求 Given a non-negative integer num, repeatedly add all its digits until the result has only one d ...

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

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

  7. LeetCode 67. Add Binary

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

  8. (String) leetcode 67. Add Binary

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

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

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

随机推荐

  1. 51-Intersection of Two Linked Lists

    Intersection of Two Linked Lists My Submissions QuestionEditorial Solution Total Accepted: 72580 Tot ...

  2. WINDOWS中使用svn

    官网:https://tortoisesvn.net/index.zh.html  (SVN安装包) 然后下载对应的64位安装包(语言包) 安装完后运行 可以存到D盘,新建一个文件夹存放 右键桌面会多 ...

  3. 修改linux系统下mysql数据库登陆密码(密码忘记)

    报错:Access denied for user 'root'@'localhost' (using password: NO) 解决方案: 1. 检查mysql服务是否启动,如果启动,关闭mysq ...

  4. ES6必知,箭头函数与普通函数的区别。

    1. 箭头函数没有prototype(原型),所以箭头函数本身没有this let a = () =>{}; console.log(a.prototype); // undefined 2. ...

  5. windows Notepad++ 上配置 vs 编译器 , 编译并运行

    windows 中 配置 vs编译器 在Linux下,Kris是倾向于在终端中使用gcc和g++来编译C/C++的,在Windows下相信很多人都是选择臃肿的Visual Studio,我亦不免如此. ...

  6. centOS7.4 , gcc4.8.5装cgdb

    从github上clone最新releast后进入文件夹 ./configure –prefix=/usr/local CXXFLAGS=-std=c++11 make&make instal ...

  7. C++ 类型转换(C风格的强制转换):

    转https://www.cnblogs.com/Allen-rg/p/6999360.html C++ 类型转换(C风格的强制转换): 在C++基本的数据类型中,可以分为四类:整型,浮点型,字符型, ...

  8. GO并发相关

    锁的使用 注意要成对,重点是代码中有分支或者异常返回的情况,这种情况要在异常返回前先释放锁 mysqlInstanceLock.Lock() slaveHostSql := "show sl ...

  9. java Map集合类

    ---恢复内容开始--- Map提供了一个更通用的元素存储方法,Map集合类用于存储元素对(称作"键"和"值"),其中每个键映射到一个值. 了解Map接口和方法 ...

  10. Handler与多线程

    1.Handler介绍 在Android开发中,我们常会使用单独的线程来完成某些操作,比如用一个线程来完成从网络上下的图片,然后显示在一个ImageView上,在多线程操作时,Android中必须保证 ...