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" ...
随机推荐
- 如何修改C# winform程序图标
以Visual Studio 2012 C# Winform程序为例 一.程序内部显示图标的修改方法 在窗体的属性窗口找到icon属性,设置成已经准备好的ico格式的文件,效果如下 二.程序外部显示图 ...
- DBContext基础查询
https://www.cnblogs.com/gosky/p/5752001.html 遍历所有实体 //遍历所有学生 DBSet using (var db = new Entities()) { ...
- THINKCMF5 部署到 Windows服务器
问题一 [public/index.php是项目的入口文件,请配置服务器时把 public 目录做为 web 目录]这是官方文档的一句话.如何将public 目录做为 web 目录? 解答:在IIS或 ...
- C++-POJ1067-取石子游戏
//(ak,bk)=([k*(1+sqrt(5))/2],[k*(1+sqrt(5))/2]+k)=(ak,ak+k) #include <cstdio> double sqrt5=2.2 ...
- 用python制作多份试卷防止作弊(随机排列题目顺序和答案顺序,提供参考答案)
#! /usr/bin/python# randomQuizeGenerator.py - Creates quizzes with questions and answers in # ra ...
- Flink流处理(五)- 状态与一致性模型
状态(State)与一致性模型 接下来我们转向另一个在流处理中十分重要的点:状态(state).状态在数据处理中是无处不在的.为了产生一个结果,函数一般会聚合某个时间段内(或是一定数量的)events ...
- Codeforces Round #620 (Div. 2)E(LCA求树上两点最短距离)
LCA求树上两点最短距离,如果a,b之间距离小于等于k并且奇偶性与k相同显然YES:或者可以从a先走到x再走到y再走到b,并且a,x之间距离加b,y之间距离+1小于等于k并且奇偶性与k相同也输出YES ...
- Books Exchange (hard version)
The only difference between easy and hard versions is constraints. There are nn kids, each of them i ...
- Matlab filter常用函数
Filtering and Analysis Functions Filtering Function Description fftfilt Filters a signal with a digi ...
- MySQL性能优化---优化方案
1.对查询进行优化,应尽量避免全表查询,首先考虑在where及order by涉及的列上建立索引: 2.应尽量避免where子句中对字段进行null值判断,否则将导致引擎放弃使用索引而进行全表扫描: ...