[LeetCode] 67. Add Binary_Easy tag: String
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" 这个题我们就把a,b补全, 使得他们length相等, 这样, 只需要判断最后一个edge case, 看看是不是需要多加一位.
class Solution:
def addBinary(self, a, b):
if len(b) < len(a):
a, b = b, a
m, n, ans, pre = len(a), len(b), "", 0
a = ''*(n-m) + a
for i in range(n)[::-1]: # note 从n-1往前走
pre, rem = divmod(int(a[i]) + int(b[i] + pre, 2))
ans += str(rem)
return ans[::-1] if not pre else '' + ans[::-1]
[LeetCode] 67. Add Binary_Easy tag: String的更多相关文章
- [LeetCode] 415. Add Strings_Easy tag: String
Given two non-negative integers num1 and num2 represented as string, return the sum of num1 and num2 ...
- LeetCode 616. Add Bold Tag in String
原题链接在这里:https://leetcode.com/problems/add-bold-tag-in-string/description/ 题目: Given a string s and a ...
- (String) leetcode 67. Add Binary
Given two binary strings, return their sum (also a binary string). The input strings are both non-em ...
- LeetCode 67. Add Binary (二进制相加)
Given two binary strings, return their sum (also a binary string). For example,a = "11"b = ...
- [LeetCode] 67. Add Binary 二进制数相加
Given two binary strings, return their sum (also a binary string). The input strings are both non-em ...
- Leetcode 67 Add Binary 大数加法+字符串处理
题意:两个二进制数相加,大数加法的变形 大数加法流程: 1.倒置两个大数,这一步能使所有大数对齐 2.逐位相加,同时进位 3.倒置两个大数的和作为输出 class Solution { public: ...
- LeetCode 67. Add Binary
Given two binary strings, return their sum (also a binary string). For example,a = "11"b = ...
- Java [Leetcode 67]Add Binary
题目描述: Given two binary strings, return their sum (also a binary string). For example,a = "11&qu ...
- [leetcode]67. Add Binary 二进制加法
Given two binary strings, return their sum (also a binary string). The input strings are both non-em ...
随机推荐
- echarts - 特殊需求实现代码汇总之【线图】篇
时间过得好快,刚刚还是7月底,一转眼自己调整(浪费)了大半个月的时间.. 接下来要先总结一下自己之前的知识点,然后清掉自己的待办任务,重新轻装上阵! 继7月24的echarts-柱图配置汇总后,ech ...
- Android 本地搭建Tomcat服务器供真机测试
准备工具:tomcat 环境:win7 + JDK1.8 + tomcat 9.0.13(64bit) 准备工具:tomcat 1.tomcat官网下载 https://tomcat. ...
- Oracle 学习之 Select 1
1. select 1 from table 增加临时列,每行的列值是写在select后的数,这条sql语句中是1,若select后为2,则是每行为2的列 2:select cou ...
- hihoCoder挑战赛28 题目1 : 异或排序
题目1 : 异或排序 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给定一个长度为 n 的非负整数序列 a[1..n] 你需要求有多少个非负整数 S 满足以下两个条件: ...
- 4606: [Apio2008]DNA
4606: [Apio2008]DNA Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 63 Solved: 36[Submit][Status][D ...
- nagios监控报错 It appears as though you do not have permission to view...
今天在安装完nagios后,通过nagios网页界面点击主机.服务.问题页面时.均报错,报错的内容都差不多.如点击服务,报错: It appears as though you do not have ...
- [LintCode] Invert Binary Tree 翻转二叉树
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. ...
- js之数据类型及类型转换
一.数据类型 js中的数据类型: 5种基础类型:Undefined,Null,Boolean,Number,String 1种复合类型:Object(对象包括数组,函数等) 1 ...
- Yii2 使用json 和设置component 中'format' => yii\web\Response::FORMAT_JSON 的区别
在Yii2中如果设置了 'response' => [ 'format' => yii\web\Response::FORMAT_JSON, 'charset' => 'UTF- ...
- PAT甲1004 Counting Leaves【dfs】
1004 Counting Leaves (30 分) A family hierarchy is usually presented by a pedigree tree. Your job is ...