题目来源:

  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. 怎样在一个页面使多个setInterval函数正常执行

    var firstInterval; var secondInterval; function firstAlert(){ if(firstInterval) clearInterval(firstI ...

  2. MD5和sha1加密算法

    在很多电子商务和社区应用中,我们都要存放很多的客户的资料,其中包括了很多的隐私信息和客户不愿被别人看到的信息,当然好有客户执行各种操作的密码,此时就需要对客户的信息进行加密再存储,目前有两种比较好的加 ...

  3. javascript中数组排序

    在javascript中Array类中提供了一个可以为数组排序的方法Array.sort(): 但此方法排序方式是按照unicode码进行的排序,若将整数型加入到数组元素中, 排出的结果往往达不到我们 ...

  4. JavaScript之引用类型介绍

    引用类型的值(对象)是应用类型的一个实例.在ECMAScript中,引用类型是一种数据结构,用于将数据和功能组织在一起,用于将数据和功能组织在一起.他们通常也被成为JavaScript中的类,但这种称 ...

  5. MailBee的简单使用

    保存为Eml文件方法:MailMessage.SaveMessage() 读取文件方法(不知道是不是我用的问题,没找到直接读取Eml文件的方法): MsgConvert conv = new MsgC ...

  6. Windows Server 2003 安装Sql Server 2005 问题处理

    安装途中遇到: 问题1.无法找到产品Microsoft SQL Server Native Client的安装程序包.请使用安装包sqlncli.msi的有效副本重新安装? 答:安装SQL Serve ...

  7. c# 柱状图(转载)

    // c# 显示柱状图 using System; using System.Data; using System.Configuration; using System.Web; using Sys ...

  8. Labview学习之程序Web发布

    Labview学习之程序Web发布 1. LabVIEW Web服务器     在LabVIEW开发环境中,自身带了一个已连接好的Web服务器.LabVIEW Web服务器除了与其他Web服务器一样能 ...

  9. day3_python学习笔记_chapter5_数字

    1. 整形的表示范围-2^32~2^32 - 1 : 长整形表示:aLong = 99999L 2. 复数的属性, num.real,该复数的实部, num.imag,该复数的虚部.num.conju ...

  10. EditText 双击才能获取点击事件

    在获取EditText点击事件的过程中,发现EditText setOnClickListener事件响应中,只有获取焦点的时候才会响应, 如当焦点在别的控件上时,只能先点击获取焦点,第二次点击才会响 ...