原题地址: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的更多相关文章

  1. LeetCode: Add Binary 解题报告

    Add BinaryGiven two binary strings, return their sum (also a binary string). For example,a = "1 ...

  2. [LeetCode] Add Binary 二进制数相加

    Given two binary strings, return their sum (also a binary string). For example,a = "11"b = ...

  3. Leetcode Add Binary

    Given two binary strings, return their sum (also a binary string). For example,a = "11"b = ...

  4. LeetCode——Add Binary

    Given two binary strings, return their sum (also a binary string). For example, a = "11" b ...

  5. LeetCode Add Binary |My Solution

    Given two binary strings, return their sum (also a binary string). For example, a = "11" b ...

  6. [Leetcode] add binary 二进制加法

    Given two binary strings, return their sum (also a binary string). For example,a ="11"b =& ...

  7. LeetCode Add Binary 两个二进制数相加

    class Solution { public: string addBinary(string a, string b) { if(a==""&&b==" ...

  8. LeetCode 面试:Add Binary

    1 题目 Given two binary strings, return their sum (also a binary string). For example,a = "11&quo ...

  9. # Leetcode 67:Add Binary(二进制求和)

    Leetcode 67:Add Binary(二进制求和) (python.java) Given two binary strings, return their sum (also a binar ...

随机推荐

  1. Android-LayoutAnimation

    Android-LayoutAnimation 学习自 <Android开发艺术探索> LayoutAnimation漫谈 LayoutAnimation 也是View动画的一种,作用是为 ...

  2. 网页图表Highcharts实践教程之图表区

    网页图表Highcharts实践教程之图表区 网页图表Highcharts图表区 图表区是图表的基本区域.所有的数据和图形都是绘制在图表区中.从图形绘制范围来分,图表区域分为外层图表区和绘图区.本章将 ...

  3. usb3.0 monitor is already started

    用360 开机加速里找到这个程序,把它从开机启动中删除掉就好.

  4. 洛谷P1220关路灯[区间DP 提前计算代价]

    题目描述 某一村庄在一条路线上安装了n盏路灯,每盏灯的功率有大有小(即同一段时间内消耗的电量有多有少).老张就住在这条路中间某一路灯旁,他有一项工作就是每天早上天亮时一盏一盏地关掉这些路灯. 为了给村 ...

  5. 「PKUWC2018」猎人杀

    「PKUWC2018」猎人杀 解题思路 首先有一个很妙的结论是问题可以转化为已经死掉的猎人继续算在概率里面,每一轮一直开枪直到射死一个之前没死的猎人为止. 证明,设所有猎人的概率之和为 \(W\) , ...

  6. nodejs备忘总结(一) -- node和express安装与配置,新建简单项目(附安装配置过程中遇到问题的解决方法)

    安装node 本文以安装node_v8.9.0为例(win10环境),下载node-v8.9.0-x64.msi插件 下载后,安装,安装目录默认为C:\Program Files\nodejs 配置环 ...

  7. Eclipse配置开发Go的插件——Goclipse

    引言: 上篇 <Golang快速入门(不用急,但要快)> 我们大致过了一遍Go语言的基本语法,但在开始正式的项目创建前,有必要选择一个比较顺手的 IDE (编辑器),由于之前一直都是做Ja ...

  8. 推荐13个.Net开源的网络爬虫

    1:.Net开源的跨平台爬虫框架 DotnetSpider Star:430 DotnetSpider这是国人开源的一个跨平台.高性能.轻量级的爬虫软件,采用 C# 开发.目前是.Net开源爬虫最为优 ...

  9. golang-bcc-bpf-function-tracing

    http://www.brendangregg.com/blog/2017-01-31/golang-bcc-bpf-function-tracing.html

  10. 为什么使用this构造器

    当一个类有多个构造函数的时候,常使用this构造器: public class SomeClass { public SomeClass() { //TODO:初始化一些字段 } public Som ...