[leetcode:python]67.Add Binary
来源:互联网 发布:决战武林进阶数据地煞 编辑:程序博客网 时间:2024/04/29 15:46
题目:
Given two binary strings, return their sum (also a binary string).
For example,
a = “11”
b = “1”
Return “100”.
方法一:性能72ms
class Solution(object): def addBinary(self, a, b): """ :type a: str :type b: str :rtype: str """ r = 0 for i in range(len(a)): r += pow(2, i)*int(a[len(a)-i-1]) for i in range(len(b)): r += pow(2, i)*int(b[len(b)-i-1]) s = '{0:b}'.format(r) return s
方法二:性能38ms
class Solution(object): def addBinary(self, a, b): """ :type a: str :type b: str :rtype: str """ return bin(int(a, 2) + int(b,2))[2:]
这里int(a, 2)
表示a是二进制数,将它转换成十进制数。
1 0
- 67. Add Binary Leetcode Python
- [leetcode:python]67.Add Binary
- 【Leetcode】【python】Add Binary
- 【leetcode】67. Add Binary(Python & C++)
- 【LeetCode with Python】 Add Binary
- leetcode Add Binary python 题解
- [LeetCode]67.Add Binary
- LeetCode --- 67. Add Binary
- [Leetcode] 67. Add Binary
- [leetcode] 67.Add Binary
- 【leetcode】67. Add Binary
- [leetcode] 67.Add Binary
- [leetcode] 67. Add Binary
- 【Leetcode】67. Add Binary
- 67. Add Binary LeetCode
- 【LeetCode】67. Add Binary
- leetcode--67. Add Binary
- leetcode 67. Add Binary
- POJ 1098 Robots 笔记
- nginx负载分发,cookie滞粘策略配置,sticky第三方模块追加
- 认识HTML5的WebSocket
- springmvc-5,HiddenHttpMethodFilter过滤器的作用
- 关于enum in android的一篇好帖子
- [leetcode:python]67.Add Binary
- 智能指针之scopedptr
- 内核态和用户态
- hexo+github快速搭建个人博客
- js变量、数组、对象
- STM32CubeMX生成基于Eclipse的GCC工程(二)(移植FreeRTOS操作系统)
- Android开发中对于适时进行网络状态的获取
- Andriod 自定义控件之创建可以复用的组合控件
- C#160课的主要内容