[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] 虚拟机(JVM)底层结构详解[转]
本文来自:曹胜欢博客专栏.转载请注明出处:http://blog.csdn.net/csh624366188 在以前的博客里面,我们介绍了在java领域中大部分的知识点,从最基础的java最基本语法到 ...
- 研究table-cell和overflow
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- CentOS 7下使用chkconfig添加的服务无法使用/etc/profile里面的环境变量
经过分析/etc/profile为入口的,基本是登录后执行的变量,而使用chkconfig添加的服务多变以守护经常运行,没有登录. CentOS 7下使用chkconfig添加的服务无法使用/etc/ ...
- oracle dba
http://www.oracleblog.org/category/study-note/ https://jonathanlewis.wordpress.com/ http://www.julia ...
- Visio中如何绘制黑白图像
- Revit API创建一个拷贝房间内对象布局命令
本课程演示创建一个拷贝房间内对象布局命令,完整演示步骤和代码.这个命令把选中房间内的对象复制到其它选中的一个或多个房间中,而且保持与源房间一致的相对位置.通过本讲座使听众知道创建一个二次开发程序很简单 ...
- RxJS 简介:可观察对象、观察者与操作符
RxJS 简介:可观察对象.观察者与操作符 对于响应式编程来说,RxJS 是一个不可思议的工具.今天我们将深入探讨什么是 Observable(可观察对象)和 observer(观察者),然后了解如何 ...
- bind,apply,call的区别
在Javascript中,bind, apply, call方法都可以显式绑定上下文this,这三者有何不同呢? bind只绑定this不马上执行 var person = { firstname: ...
- 在Brackets中使用Emmet
当在Brackets中安装上Emmet插件后,就可以使用Emmet的语法来加速前端编写. 有关html ● 子关系> div>ul>li ● 相邻+ div+p+bq ● 上一级^ ...
- AutoMapper在MVC中的运用04-string映射各种类型、一个属性映射多个属性等
本篇AutoMapper使用场景: ※ 类型转换,源string类型分别转换成int, DateTime,Type ※ 源和目标都包含复杂类型属性 ※ 把源中的一个属性映射到目标中的多个属性 类型转换 ...