题目来源:

  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. 使用rowid和rownum获取记录时要注意的问题

    我们知道.rowid和rownum在Oracle中都是能够被当做伪劣使用的,主要用来定位表中特定的记录,但它们是有差别的,rowid是和行记录的物理地址相应的.而rownum则不是,是通过返回的记录集 ...

  2. CRM需要注意的一些事,修改字段类型

    crm字段类型如果变了,比如文本类型变为查找类型,要新建命名跟原来不一样,千万不能删除以前的字段再建原来的一样的,那样如果导到正式系统会造成无法导入,执行sql失败, 切记切记.可以字段名后加2,或者 ...

  3. C#控件大小随窗体大小等比例变化

    相信很多博友在开发初次接触学习C# winForm时,当窗体大小变化时,窗体内的控件并没有随着窗体的变化而变化,最近因为一个项目工程的原因,也需要解决这个问题.通过查阅和学习,这个问题得到了解决,或许 ...

  4. C++_基础_C与C++的区别2

    内容: (1)C++中的函数 (2)动态内存 (3)引用 (4)类型转换 (5)C++社区对C程序员的建议 1.C++中的函数1.1 函数的重载(1)重载的概念 在同一个作用域中,函数名相同,函数的参 ...

  5. JSTL入门

    在页面最上方引入 -------------------- if语句 8}"> b的值大于8 --------------------- foreach语句 i的值是:${i}

  6. ExpandableListView 箭头样式

    ExpandableListVivew是ListView的子类,它在普通ListView的基础上进行了扩展,它把应用中的列表项分为几组,每组里 又可包含多个列表项.ExpandableListVive ...

  7. 通过class类来实例化其他类的对象(使用有参构造函数)

    import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; class Pers ...

  8. opengl 正方体+模拟视角旋转

    // first_3D.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <GL/glut.h> #includ ...

  9. 如何实现win7和VirtualBox中Ubuntu系统共享文件夹

    设备: 1.win7 旗舰版    2.VirtualBox虚拟机    3.Ubuntu12.04 以前在VM虚拟机中可以直接进行复制就可以将win7系统的文件复制到虚拟机中,然后现在安装了Virt ...

  10. css3_note

    css3基础 css3选择器 属性选择器 属性选择器基本上IE7+都支持,可以放心的使用,参见caniuse [attr] [attr=val] [attr*=val] [attr^=val] [at ...