题目简述:

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):
la = len(a) - 1
lb = len(b) - 1
f = 0
res = []
while la >= 0 or lb >= 0:
A = 0
B = 0
if la >= 0:
A = int(a[la])
la -= 1
if lb >= 0:
B = int(b[lb])
lb -= 1
if A + B +f >= 2:
res.append(str(A + B +f - 2))
f = 1
else:
res.append(str(A + B +f ))
f = 0
if f ==1:
res.append('1')
res.reverse()
return "".join(res)

【leetcode】Add Binary的更多相关文章

  1. 【LeetCode】199. Binary Tree Right Side View 解题报告(Python)

    [LeetCode]199. Binary Tree Right Side View 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/probl ...

  2. 【LeetCode67】 Add Binary

    题目描述: 解题思路: 此题的思路简单,下面的代码用StringBuilder更加简单,注意最后的结果要反转过来.[LeetCode415]Add Strings的解法和本题一模一样. java代码: ...

  3. 【LeetCode】145. Binary Tree Postorder Traversal

    Difficulty: Hard  More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/binary-tree-pos ...

  4. 【LeetCode】二叉查找树 binary search tree(共14题)

    链接:https://leetcode.com/tag/binary-search-tree/ [220]Contains Duplicate III (2019年4月20日) (好题) Given ...

  5. 【LeetCode】Validate Binary Search Tree ——合法二叉树

    [题目] Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defin ...

  6. 【LeetCode】Balanced Binary Tree 解题报告

    [题目] Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bi ...

  7. 【leetcode】 Unique Binary Search Trees (middle)☆

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  8. 【题解】【链表】【Leetcode】Add Two Numbers

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

  9. 【Leetcode】【Easy】Add Binary

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

随机推荐

  1. mybatis- spring 批量实现数据导入数据库

    终于实现了ibatis的批量插入,此方法插入3000条数据,比单条插入可以节省一半的时间XML代码: <insert id="insertBatch" parameterTy ...

  2. 《你不常用的c#之XX》

    你不常用的c#之一>:略谈unsafe http://blog.csdn.net/robingaoxb/article/details/6199508 <你不常用的c#之二>:略谈G ...

  3. getElementsByClassName简单实现

    function getElementsByClassName(node, className) { var aClassReg = className.split(' ').map(function ...

  4. ZOJ 3686 A Simple Tree Problem

    A Simple Tree Problem Time Limit: 3 Seconds      Memory Limit: 65536 KB Given a rooted tree, each no ...

  5. 取代 Windows Search

    windows自带的搜索工具太难用了,总是在你急需的时候提示还没有建立索引,眼皮底下的文件都找不到. 1. everything 适合快速搜索文件名 优点是速度快,非常快,几乎是瞬间就建立好了索引. ...

  6. ubuntu系统theano和keras的安装

    说明:系统是unbuntu14.04LTS,32位的操作系统,以前安装了python3.4,现在想要安装theano和keras.步骤如下: 1,安装pip sudo apt-get install ...

  7. 理解Attribute

    注:本文转载自http://kb.cnblogs.com/page/87531/ Attribute与Property 的翻译区别 Attribute 一般译作“特性”,Property 仍然译为“属 ...

  8. [转]SpringMVC Controller介绍及常用注解

    一.简介 在SpringMVC 中,控制器Controller 负责处理由DispatcherServlet 分发的请求,它把用户请求的数据经过业务处理层处理之后封装成一个Model ,然后再把该Mo ...

  9. shell脚本: 备份mysql远程数据库并清除一个月之前的数据

    hxingxing-backup.sh: date="$(date +"%Y-%m-%d")"mysqldump -u root -h localhost -p ...

  10. hdu1045 DFS

    #include<stdio.h> #include<string.h> int n; int maxx; ][]; ]={,-,,}; ]={,,,-}; ][][];//炮 ...