作者: 负雪明烛
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. idea中如何找到重写

    Ctrl+O 为了避免写错重写类和快速重写.

  2. STM32 BootLoader升级固件

    一.知识点 1.BootLoader就是单片机启动时候运行的一段小程序,这段程序负责单片机固件的更新,也就是单片机选择性的自己给自己下程序.可以更新,也可以不更新,更新的话,BootLoader更新完 ...

  3. liveBOS环境搭建

    环境搭建:1.准备jdk1.6及以上版本oracle11gplsqlsql脚本(oracle_init.sql,oracle_insert.sql)livebos_tomcatlivebos的授权文件 ...

  4. 学习java的第十三天

    一.今日收获(前两天家里有事,博客都忘了发了,唉) 1.通过看哔哩哔哩看黑马程序员的教学视频,学习了java中的数据类型自动转换.强制转换及注意事项三节 2.简单看了看完全学习手册 二.今日问题 1. ...

  5. 云原生时代的 APM

    作者 | 刘浩杨 来源|尔达 Erda 公众号 ​APM 的全称是 Application Performance Management(应用性能管理),早在 90 年代中期就有厂商提出性能管理的概念 ...

  6. Hbase与Phoenix整合

    目录 一.简介 二.安装 三.Phoenix Shell操作 SCHEMA操作 1.创建schema 2.使用schema 3.删除schema 表操作 1.显示所有表 2.创建表 3.表数据的增删改 ...

  7. 节省内存的循环banner(一)

    循环banner是指scrollview首尾相连,循环播放的效果,使用非常广泛.例如淘宝的广告栏等. 如果是简单的做法可以把所有要显示的图片全部放进一个数组里,创建相同个数的图片视图来显示图片.这样的 ...

  8. Output of C++ Program | Set 1

    Predict the output of below C++ programs. Question 1 1 // Assume that integers take 4 bytes. 2 #incl ...

  9. Redis集群的三种模式

    一.主从模式 通过持久化功能,Redis保证了即使在服务器重启的情况下也不会损失(或少量损失)数据,因为持久化会把内存中数据保存到硬盘上,重启会从硬盘上加载数据. 但是由于数据是存储在一台服务器上的, ...

  10. 【Linux】【Services】【Web】Nginx基础

    1. 概念 1.1. 消息通知机制:同步synchronous,异步asynchronous 同步:等待对方返回信息 异步:被调用者通过状态.通知或回调通知调用者 状态:调用者每隔一段时间就需要检查一 ...