LeetCode练题——67. Add Binary
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的更多相关文章
- LeetCode刷题笔录Add Binary
Given two binary strings, return their sum (also a binary string). For example, a = "11" b ...
- 【leetcode❤python】 67. Add Binary
class Solution(object): def addBinary(self, a, b): """ :type a: str ...
- [LeetCode] 415 Add Strings && 67 Add Binary && 43 Multiply Strings
这些题目是高精度加法和高精度乘法相关的,复习了一下就做了,没想到难住自己的是C++里面string的用法. 原题地址: 415 Add Strings:https://leetcode.com/pro ...
- 67. Add Binary【LeetCode】
67. Add Binary Given two binary strings, return their sum (also a binary string). For example,a = &q ...
- LeetCode算法题-Trim a Binary Search Tree(Java实现)
这是悦乐书的第284次更新,第301篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第152题(顺位题号是669).给定二叉搜索树以及L和R的最低和最高边界,修剪树以使其所 ...
- LeetCode算法题-Merge Two Binary Trees(Java实现)
这是悦乐书的第274次更新,第290篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第142题(顺位题号是617).提供两个二叉树,将其合并为新的二叉树,也可以在其中一个二 ...
- LeetCode算法题-Diameter of Binary Tree(Java实现)
这是悦乐书的第257次更新,第270篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第124题(顺位题号是543).给定二叉树,您需要计算树的直径长度. 二叉树的直径是树中 ...
- 【LeetCode】67. Add Binary 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BigInteger类 模拟加法 日期 题目地址:h ...
- 【LeetCode】67. Add Binary
题目: Given two binary strings, return their sum (also a binary string). For example,a = "11" ...
随机推荐
- IDEA常用操作链接
idea中svn的提交.更新等操作 https://jingyan.baidu.com/article/375c8e19e3c47a25f3a22955.html Idea 部署SVN详细步骤以及上传 ...
- 用apscheduler写python定时脚本
apscheduler 官方文档:http://apscheduler.readthedocs.io/en/latest/ 写一个后台定时任务,一般2个选择,一个是apscheduler,一个cele ...
- PHP 可选参数
function chooseable($a,$b,$d,$c="我是可选参数c"){ //注意:可选参数一定要是在必选参数后面(有默认值就是可选参数):PHP中参数一定要变量符号 ...
- idea tomcat启动无效
今天换了台电脑,用idea 部署tomcat启动死活启动不了,报 Application Server was not connected before run configuration stop, ...
- IIR filter design from analog filter
Analog filter和digital filter的联系: z变换与Laplace从数学上的关系为: 但这种关系在实际应用上不好实现,因此通常使用biliner transform(https: ...
- ACM的探索之Everything is Generated In Equal Probability! 后序补充丫!
Problem Desciption: 百度翻译后的汉化: 参见博客:https://www.cnblogs.com/zxcoder/p/11253099.html https://blog.csdn ...
- java基础(十一)之抽象类和抽象函数
1.抽象函数的语法特征2.抽象类的语法特征3.抽象类的作用 抽象函数 只有函数的定义,没有函数体的函数被称为抽象函数: abstract void func(); 抽象类 使用abstract定义的类 ...
- 百炼OJ - 1001 - Exponentiation
题目链接 哇一遍AC的感觉也太爽了吧:) #include <stdio.h> #include <string.h> int times=0; char *myCalc(ch ...
- Docker - Deepin中docker不能启动容器,-d也无效
问题重现 1. 搭建mysql docker run -p 3306:3306 --name docker-mysql-5.7 -v $PWD/conf:/etc/mysql/conf.d -v $P ...
- AcWing 841. 字符串哈希
//快速判断两次字符串是不是相等 #include<bits/stdc++.h> using namespace std ; typedef unsigned long long ULL; ...