[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 ...
随机推荐
- Java虚拟机工作原理简介
1. Java 文件执行过程 2. 运行数据区域 Runtime Data Areas:当运行一个JVM示例时,系统将分配给它一块内存区域(这块内存区域的大小可以设置的),这一内存区域由JVM自己来管 ...
- SPOJ.1812.LCS2(后缀自动机)
题目链接 \(Description\) 求最多10个串的LCS(最长公共子序列). \(Solution\) 类比上题,对一个串建SAM,我们可以逐串地求出其在每个节点所能匹配的最大长度mx[i]. ...
- ROS知识(23)——行为树Behavio Tree原理
机器人的复杂行为的控制结构CA(Contrl Architecture)通常使用有限状态机来实现,例如ROS提供的smach.行为树是另外一种实现机器人控制的方法,ROS下代表的开源库有pi_tree ...
- stm-ledstrip : Driver and test routine for WS2811 RGB-LED
stm-ledstrip : Driver and test routine for WS2811 RGB-LED #include "ws2812.h" #include < ...
- 打印函数 lodop
Lodop属性和方法详解 例子:LODOP.PRINT_INIT("打印任务名");LODOP.SET_PRINT_COPIES(2);bdhtml=window.document ...
- OAuth2.0网页授权 提示未关注该测试号
用无高级接口权限的公众号使用别人的appid和appsecret在网页中获取用户信息时,提示未关注该测试号. 搜集各种资料才发现是因为 测试帐号只能对关注者网页授权,正式帐号可以对未关注者授权
- mysql exists 如何使用
还没时间看,exists用的少 ==>当你只需要判断后面的查询结果是否存 在时使用exists() http://edu.codepub.com/2011/0208/29218.php 今天正 ...
- delphi teechrt中TChart 一些属性设置
把图片设置成黑白 2.设置颜色
- lufylegend:动画
1.动画1 <script type="text/javascript"> var loader,anime,layer; //初始化画布 init(200, &quo ...
- CentOS MongoDB 高可用实战
原文:https://www.sunjianhua.cn/archives/centos-mongodb.html 一.MongoDB 单节点 1.1.Windows 版安装 1.1.1 获取社区版本 ...