[LeetCode]题解(python):067-Add Binary
题目来源:
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的更多相关文章
- LeetCode练题——67. Add Binary
1.题目 67. Add Binary——easy Given two binary strings, return their sum (also a binary string). The inp ...
- Java for LeetCode 067 Add Binary
Given two binary strings, return their sum (also a binary string). For example, a = "11" b ...
- LeetCode刷题笔录Add Binary
Given two binary strings, return their sum (also a binary string). For example, a = "11" b ...
- 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 ...
- [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 ...
- [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 ...
- 067 Add Binary 二进制求和
给定两个二进制字符串,返回他们的和(用二进制表示).案例:a = "11"b = "1"返回 "100" .详见:https://leetc ...
- LeetCode(67) Add Binary
题目 Given two binary strings, return their sum (also a binary string). For example, a = "11" ...
- LeetCode题解之Diameter of Binary Tree
1.题目描述 2.分析 深度优先. 3.代码 int ans; int diameterOfBinaryTree(TreeNode* root) { ans = ; depth(root); ; } ...
- # Leetcode 67:Add Binary(二进制求和)
Leetcode 67:Add Binary(二进制求和) (python.java) Given two binary strings, return their sum (also a binar ...
随机推荐
- winds引导配置数据文件包含的os项目无效
我装了winds7与linux双系统,用easyBcd程序时,删除了一个winds7,之后winds7就进不去了, 进入winds7时显示winds未能启动,原因可能是最近更改了硬件或软件.解决此问题 ...
- Android Json生成及解析实例
JSON的定义: 一种轻量级的数据交换格式,具有良好的可读和便于快速编写的特性.业内主流技术为其提供了完整的解决方案(有点类似于正则表达式 ,获得了当今大部分语言的支持),从而可以在不同平台间进行数据 ...
- [置顶] PHP如何扩展和如何在linux底层对php扩展?
虽然大部分php工程师都不需要知道php的C代码核心是如何运作的,有些人可能知道有个dl()函数.或者使用过一些第三方的类库,这些正是本文的重点之一. 希望对那些想把php带向更 ...
- SAN实现
Linux 上主要有三个 iSCSI Target(基于internet scsi协议的target) 实现: Linux SCSI Target – STGT / tgt Linux-IO Targ ...
- ASPxGridView-为每行添加序号
添加一个新的非绑定列,使用CustomColumnDisplayText事件来分配序号给该列 <dx:GridViewDataTextColumn Caption="序号" ...
- localhost简介、localhost与 127.0.0.1 及 本机IP 的区别
localhost是什么意思? 相信有人会说是本地ip,曾有人说,用127.0.0.1比localhost好,可以减少一次解析. 看来这个入门问题还有人不清楚,其实这两者是有区别的. localhos ...
- iOS手机号正则表达式并实现344格式 (正则的另一种实现方式)
[Demo下载地址]https://git.oschina.net/remainedmute/PhoneNumDemo.git 相关博客http://www.jianshu.com/p/00da4d8 ...
- html object元素
知道object是播放音频,但是想了解具体点,百度一下,感觉模模糊糊的,感觉看不大明白,最后找到一个解释比较详细,先从应用,到解释具体属性, 具体网址是: http://www.w3school.co ...
- JQuery初识
一.什么是JQuery JQuery官方网站上是这样解释的:JQuery是一个快速简洁的JavaScript库,它可以简化HTML文档的元素遍历.事件处理.动画及Ajax交互,快速开发We ...
- python练习之list
请用索引取出下面list的指定元素: # -*- coding: utf-8 -*- L = [ ['Apple', 'Google', 'Microsoft'], ['Java', 'Python' ...