[leetcode]Add Binary @ Python
原题地址:https://oj.leetcode.com/problems/add-binary/
题意:
Given two binary strings, return their sum (also a binary string).
For example,
a = "11"
b = "1"
Return "100"
.
解题思路:提供两种实现方式吧。
代码一:
class Solution:
# @param a, a string
# @param b, a string
# @return a string
def addBinary(self, a, b):
aIndex = len(a)-1; bIndex = len(b)-1
flag = 0
s = ''
while aIndex>=0 and bIndex>=0:
num = int(a[aIndex])+int(b[bIndex])+flag
flag = num/2; num %= 2
s = str(num) + s
aIndex -= 1; bIndex -= 1
while aIndex>=0:
num = int(a[aIndex])+flag
flag = num/2; num %= 2
s = str(num) + s
aIndex -= 1
while bIndex>=0:
num = int(b[bIndex])+flag
flag = num/2; num %= 2
s = str(num) + s
bIndex -= 1
if flag == 1:
s = '' + s
return s
代码二:
class Solution:
# @param a, a string
# @param b, a string
# @return a string
def addBinary(self, a, b):
length = max(len(a),len(b)) + 1
sum = ['' for i in range(length)]
if len(a) <= len(b):
a = '' * ( len(b) - len(a) ) + a
if len(a) > len(b):
b = '' * ( len(a) - len(b) ) + b
flag = 0
i = len(a) - 1
while i >= 0:
if int(a[i]) + int(b[i]) + flag == 3:
sum[i+1] = ''
flag = 1
elif int(a[i]) + int(b[i]) + flag == 2:
sum[i+1] = ''
flag = 1
elif int(a[i]) + int(b[i]) + flag == 1:
sum[i+1] = ''
flag = 0
else:
sum[i+1] = ''
flag = 0
i = i - 1
if flag == 1:
sum[0] = ''
if flag == 0:
sum = sum[1:length]
sum = ''.join(sum)
return sum
[leetcode]Add Binary @ Python的更多相关文章
- LeetCode: Add Binary 解题报告
Add BinaryGiven two binary strings, return their sum (also a binary string). For example,a = "1 ...
- [LeetCode] Add Binary 二进制数相加
Given two binary strings, return their sum (also a binary string). For example,a = "11"b = ...
- Leetcode Add Binary
Given two binary strings, return their sum (also a binary string). For example,a = "11"b = ...
- LeetCode——Add Binary
Given two binary strings, return their sum (also a binary string). For example, a = "11" b ...
- LeetCode Add Binary |My Solution
Given two binary strings, return their sum (also a binary string). For example, a = "11" b ...
- [Leetcode] add binary 二进制加法
Given two binary strings, return their sum (also a binary string). For example,a ="11"b =& ...
- LeetCode Add Binary 两个二进制数相加
class Solution { public: string addBinary(string a, string b) { if(a==""&&b==" ...
- LeetCode 面试:Add Binary
1 题目 Given two binary strings, return their sum (also a binary string). For example,a = "11&quo ...
- # Leetcode 67:Add Binary(二进制求和)
Leetcode 67:Add Binary(二进制求和) (python.java) Given two binary strings, return their sum (also a binar ...
随机推荐
- NOIP练习赛题目2
小K的农场 难度级别:C: 运行时间限制:1000ms: 运行空间限制:51200KB: 代码长度限制:2000000B 试题描述 小K在MC里面建立很多很多的农场,总共n个,以至于他自己都忘记了每个 ...
- css的浮动
浮动的框可以向左或向右移动,直到它的外边缘碰到包含框或另一个浮动框的边框为止. 由于浮动框不在文档的普通流中,所以文档的普通流中的块框表现得就像浮动框不存在一样. 一 css的浮动 CSS提供了元素对 ...
- PG的集群技术:Pgpool-II与Postgres-XC Postgres-XL Postgres-XZ Postges-x2
https://segmentfault.com/a/1190000007012082 https://www.postgres-xl.org/ https://www.biaodianfu.com/ ...
- ASP.NET 2.0
http://www.cnblogs.com/linezero/p/nightlynetcore2.html
- stap 命令
SystemTap accepts script as command line option or external file, for example: * Command-line script ...
- 在Delphi中操作快捷方式
快捷方式减少了系统的重复文件,是快速启动程序或打开文件或文件夹的方法,快捷方式对经常使用的程序.文件和文件夹非常有用.在Windows系统中,充斥着大量的快捷方式,那么如何操作这些快捷方式就是一个很头 ...
- MSSQL、MySQL 数据库删除大批量千万级百万级数据的优化
原文:https://blog.csdn.net/songyanjun2011/article/details/7308414 SQL Server上面删除1.6亿条记录,不能用Truncate(因为 ...
- NSDictionary 详解
1.使用dictionaryWithObjectsAndKeys方法存储数据时,中间任何一个对象都不能为nil,否则它后面都对象都无法存入aFiledic.因为dictionaryWithObject ...
- ArcEngine设置有牵引线的标注
来自:https://blog.csdn.net/u011609113/article/details/51372827/ 在ArcGIs中很容易就能设置带有牵引线的标注. 在ArcEngine中 ...
- 【k8s】搭建步骤
搭建步骤 基础概念:https://www.cnblogs.com/sxdcgaq8080/p/10640879.html ====================================== ...