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" ...
随机推荐
- Python - python3.7新增的contextvars vs Thread local(threading.local)
总结 和threading.local()类似.Python3.7新增. thread.local(): 不同线程,同一个变量保存不同的值. contextvars: 不同上下文,同一个变量保存不同的 ...
- 线性混合+ROI
相关代码: #include <opencv2/opencv.hpp> #include <iostream> using namespace cv; using namesp ...
- vue引用fastClick后,ios输入框聚焦不灵敏问题
fastClick.prototype.focus = function (targetElement) { targetElement.focus() }
- html表单提交给PHP然后浏览器显示出了PHP的源代码
今天学习到PHP处理网页表单提交的数据时,碰到一个巨头疼的问题,先贴上案例代码: html表单部分: <html> <head> <meta charset=" ...
- 重新装Mysql后原来数据的恢复办法:即一个版本的mysql的data文件夹下的所有的数据,怎么去加载到另一个安装的mysql中
重新装Mysql后原来数据的恢复办法本博客:http://blog.csdn.net/xiaowu_zhu/article/details/71188955 不管是重新装系统或者数据库时,总会遇到怎么 ...
- tomcat8.5优化配置
参考文章: https://www.cnblogs.com/steven-snow/p/9262025.html 1.Tomcat内存使用调整 windows系统在bin/catalina.bat文件 ...
- 普及C组第二题(8.2)
1340. [南海2009初中]jumpcow(牛跳) (Standard IO) 题目: John的奶牛们计划要跳到月亮上去.它们请魔法师配制了 P (1 <= P <=150,000) ...
- ubuntu16.04修改复制粘贴快捷键的方法
打开终端-选择配置文件首选项 打开,选择快捷键,自行修改
- Bugku-CTF之PHP_encrypt_1(ISCCCTF) [fR4aHWwuFCYYVydFRxMqHhhCKBseH1dbFygrRxIWJ1UYFhotFjA=]
Day34 PHP_encrypt_1(ISCCCTF) fR4aHWwuFCYYVydFRxMqHhhCKBseH1dbFygrRxIWJ1UYFhotFjA= 下载下来.zip文件
- 【代码总结】Spring MVC数据校验
1.实验介绍 --------------------------------------------------------------------------------------------- ...