题目来源:

  https://leetcode.com/problems/add-binary/


题意分析:

  这题是要将二进制相加,比如“11”,“1”,那么就返回“100”。


题目思路:

  模拟加法的过程,直接模拟,大于等于2就进位。


代码(Python):

  

 class Solution(object):
def addBinary(self, a, b):
"""
:type a: str
:type b: str
:rtype: str
"""
size1 = len(a);size2 = len(b)
if size1 == 0:
return b
if size2 == 0:
return a
carry,ans = 0,""
while size1 > 0 and size2 > 0:
tmp = int(a[size1 -1]) + int(b[size2 - 1]) + carry
carry = tmp // 2;tmp %= 2
ans += str(tmp)
size1 -= 1;size2 -= 1
if size1 == 0:
while size2 > 0:
tmp = int(b[size2 - 1]) + carry
carry = tmp // 2;tmp %= 2
ans += str(tmp)
size2 -= 1
if size2 == 0:
while size1 > 0:
tmp = int(a[size1 - 1]) + carry
carry = tmp // 2;tmp %= 2
ans += str(tmp)
size1 -= 1
if carry == 1:
ans += str(carry)
ans = ans[::-1]
return ans

转载请注明出处:http://www.cnblogs.com/chruny/p/5028769.html

[LeetCode]题解(python):067-Add Binary的更多相关文章

  1. LeetCode练题——67. Add Binary

    1.题目 67. Add Binary——easy Given two binary strings, return their sum (also a binary string). The inp ...

  2. Java for LeetCode 067 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题解:(114) Flatten Binary Tree to Linked List

    题目说明 Given a binary tree, flatten it to a linked list in-place. For example, Given 1 / \ 2 5 / \ \ 3 ...

  5. [LeetCode 题解]: Maximum Depth of Binary Tree

    Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...

  6. [LeetCode 题解]: Minimum Depth of Binary Tree

    Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...

  7. 067 Add Binary 二进制求和

    给定两个二进制字符串,返回他们的和(用二进制表示).案例:a = "11"b = "1"返回 "100" .详见:https://leetc ...

  8. LeetCode(67) Add Binary

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

  9. LeetCode题解之Diameter of Binary Tree

    1.题目描述 2.分析 深度优先. 3.代码 int ans; int diameterOfBinaryTree(TreeNode* root) { ans = ; depth(root); ; } ...

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

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

随机推荐

  1. ExecuteScalar

    ExecuteScalar运行查询,并返回查询所返回的结果集中第一行的第一列或空引用(假设结果集为空).忽略其它列或行. 使用 ExecuteScalar 方法从数据库中检索单个值. 由于不用创建行集 ...

  2. 一个可无限伸缩且无ABA问题的无锁队列

    关于无锁队列,详细的介绍请参考陈硕先生的<无锁队列的实现>一文.然进一步,如何实现一个不限node数目即能够无限伸缩的无锁队列,即是本文的要旨. 无锁队列有两种实现形式,分别是数组与链表. ...

  3. SET ANSI_NULLS (Transact-SQL)

    指定在 SQL Server 2014 中与 Null 值一起使用等于 (=) 和不等于 (<>) 比较运算符时采用符合 ISO 标准的行为. 当 SET ANSI_NULLS 为 ON ...

  4. linux杂记(五)正确关机方法(shutdown,reboot,init,halt)

    前言:由于在linux底下,每个程序(或者说是服务)都是在背景下运行的,因此,在你看不到的屏幕背后其实可能有相当多人同时在你的主机上面工作,如果 你直接按下电源开关来关机,则可能导致其他人的数据就此中 ...

  5. firebug如何使用

    1.怎么安装firebug: a.打开火狐浏览器--------b.点击火狐浏览器的右上角这个小图标-------c.点击<获取附件组件>,在右上角的搜索框()内,输入firebug,点击 ...

  6. 制作OB图的时候,OB玩家进入后就退出的问题

    开始怀疑是 OB玩家没有建筑 所以强行退出了,有朋友说那是因为有无效的触发造成的 我没有测试过 最后解决是给OB玩家在地图中加上建筑 更新 最后测试,把OB玩家放到一个组里 开局KILL掉这个组的建筑 ...

  7. poj 1949 Chores 最长路

    题目链接 求出最长路..... #include <iostream> #include <vector> #include <cstdio> #include & ...

  8. 【转】Centos 设置IP地址的几种方式

    对于很多刚刚接触linux的朋友来说,如何设置linux系统的IP地址,作为第一步,下面小编以centos系统为例,给大家演示如何给centos设置IP地址,如何修改linux 系统IP地址? 查看I ...

  9. C语言2

    函数是C语言的基本单位,类是java,c#,c++的基本单位 int abs(int x); double fabs(double x);   按变量的存储方式:静态变量.自动变量.寄存器变量 指针就 ...

  10. 面试题之 query转为obj

    要注意处理编码后的字串  对于a=123要得到number形的值 function parseQueryString(url) { var obj = {}; var query = url.sear ...