1、题目

67. Add Binary——easy

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"

2、我的解答

 # -*- coding: utf-8 -*-
# @Time : 2020/2/9 11:18
# @Author : SmartCat0929
# @Email : 1027699719@qq.com
# @Link : https://github.com/SmartCat0929
# @Site :
# @File : 67. Add Binary.py class Solution:
def addBinary(self, a: str, b: str) -> str:
a1 = "0b" + a
b1 = "0b" + b
sum1 = int(a1, 2) + int(b1, 2) #第一步 转换成十进制后的简单相加
sum2 = bin(sum1) #第二步 将相加后的和转换成二进制
a = [x for x in sum2] #第三步 bin方法转换后的二进制有0b前缀,用列表生成式将他转换成列表
b = a[2:] #第四步 采用切片,去除前两位前缀
sum3 = ''.join(b) #第五步 采用join方法将列表内部元素合并
return sum3
print(Solution().addBinary("", ""))

LeetCode练题——67. Add Binary的更多相关文章

  1. LeetCode刷题笔录Add Binary

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

  2. 【leetcode❤python】 67. Add Binary

    class Solution(object):    def addBinary(self, a, b):        """        :type a: str  ...

  3. [LeetCode] 415 Add Strings && 67 Add Binary && 43 Multiply Strings

    这些题目是高精度加法和高精度乘法相关的,复习了一下就做了,没想到难住自己的是C++里面string的用法. 原题地址: 415 Add Strings:https://leetcode.com/pro ...

  4. 67. Add Binary【LeetCode】

    67. Add Binary Given two binary strings, return their sum (also a binary string). For example,a = &q ...

  5. LeetCode算法题-Trim a Binary Search Tree(Java实现)

    这是悦乐书的第284次更新,第301篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第152题(顺位题号是669).给定二叉搜索树以及L和R的最低和最高边界,修剪树以使其所 ...

  6. LeetCode算法题-Merge Two Binary Trees(Java实现)

    这是悦乐书的第274次更新,第290篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第142题(顺位题号是617).提供两个二叉树,将其合并为新的二叉树,也可以在其中一个二 ...

  7. LeetCode算法题-Diameter of Binary Tree(Java实现)

    这是悦乐书的第257次更新,第270篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第124题(顺位题号是543).给定二叉树,您需要计算树的直径长度. 二叉树的直径是树中 ...

  8. 【LeetCode】67. Add Binary 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BigInteger类 模拟加法 日期 题目地址:h ...

  9. 【LeetCode】67. Add Binary

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

随机推荐

  1. APP开发工具如何选?

    随着技术的发展,在当前开发一款APP已经非常的简单和快速.特别是近些年,利用HTML5技术将APP的开发门槛进一步降低.各种开发工具和框架层出不穷,令人眼花缭乱.这么多的工具摆在眼前应该如何进行选择呢 ...

  2. Photoshop——APP设计规范

    随着Android和iOS语言的兴起,能够在手机上运行的APP软件已经成为了目前移动应用技术的焦点,APP的UI设计随之也越来越受到重视. 用户的需求不断增加,技术也在不断的更新,UI设计也越来越被重 ...

  3. telnet不起作用

    1.出现 'telnet' 不是内部或外部命令,也不是可运行的程序或批处理文件. 原因:因为本机的Telnet客户端默认是关闭的,所以我们要手动打开 解决方案:打开控制面板–>程序–>打开 ...

  4. Java-POJ1004-Financial Management

    题目大意:求出一年十二个月Larry平均每个月得到的利息 读入:12行每行一个浮点数 关于控制浮点数输出位数的资料:https://blog.csdn.net/hsliwei/article/deta ...

  5. Adobe 系列下载链接

    (注意!:在下方链接前加上 "pan.baidu.com/s/" 才是正确网址,用"百度网盘"下载) Photoshop 专区(图像处理软件) Adobe Ph ...

  6. AcWing 900. 整数划分

    #include <iostream> #include <algorithm> using namespace std; , mod = 1e9 + ; int n; int ...

  7. time时间模块_python

    一.常用指定格式打印时间 strftime()函数: import timeprint(time.strftime('%Y-%M-%d %H:%M:%S %A' )) #格式可任意定制,2019-14 ...

  8. linux下删除空行的几种方法

    在查看linux下的配置文件时,为了便于一目了然的查看,经常会删除空行和#头的行.而linux在删除空行的方法很多,grep.sed.awk.tr等工具都能实现.现总结如下: 1.grep grep ...

  9. MyBatis(8)——联表多对一的处理

    xml说明: <!--column不做限制,可以为任意表的字段,而property须为type 定义的pojo属性--> <resultMap id="唯一的标识" ...

  10. instrrev 和instr 区别vb

    Private Sub Form_click() Dim temp As String temp = "c:\window\system" Print Mid(temp, InSt ...