[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 ...
随机推荐
- mac文件权限
如何设置文件/或文件夹权限为777 进入终端,切换到指定目录,输入以下命令,后面添加你的文件名/目录名$sudo chmod -R 777 (文件名/目录名) 或 $chmod 777 ./test. ...
- JS isArray记录
var isArray=Function.isArray||function(0){ return typeof o === "object" && Object. ...
- struts2的初步认识!
struts2的jar包会完成一些工作,让你的数据和显示很好的联系在一起. 开始的时候,主要通过三个点来完成Struts2的工作 1,JAVA类 2,struts.x ...
- 腾讯TGideas语义化标签(转)
--------引子--------------- 家里有个熊孩子,经常会有一些意想不到的事情发生:回家的时候,他会笑呵呵冲过来,大声喊着“臭爸爸”:你让他把鞋穿上,他会提起鞋子往楼下扔...在小孩的 ...
- CS0016: 未能写入输出文件“c:\Windows\Microsoft.NET\Framework64\v2.0.50727\Temporary ASP.NET Files\root\921bbfc4\ca7cf42\App_Code.fu98jwep.dll”--“拒绝访问。 ”
在本地开发环境没问题,但是发布到服务器出现:未能写入输出文件“c:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Fil ...
- 展开BOM并使用最终用量的算法(转载)
本文系转载子ITPUB,如果有侵犯您权益的地方,烦请及时的告知与我,我即刻将停止侵权行为: 网址:http://www.itpub.net/thread-1020586-1-1.html http:/ ...
- oracle与sql server时间差的取法
Oracle: oracle 两个时间相减默认的是天数 oracle 两个时间相减默认的是天数*24 为相差的小时数 oracle 两个时间相减默认的是天数*24*60 为相差的分钟数 oracle ...
- MySQL varchar和char类型
varchar和char是两种最主要的字符串类型.不幸的是,很难精确地解释这些值是怎么储存在磁盘和内存中的,因为这根存储引擎的具体实现有关.下面的描述假设使用的存储引擎是InnoDB或者MyISAM. ...
- Fedora安装theano
Fedora下安装theano Fedora下安装theano Theano的安装依赖很多包,有必须的,有可选的.此外,python版本必须大于2.6,请在shell直接键入python,如果小于2. ...
- Django一对多,多对多操作
简要说明 Django里面的数据库操作O2O&M2M,一般归属于models管理 使用场景 一对一:在某表中创建一行数据时,有一个单选的下拉框(下拉框中的内容被用过一次就消失了).//两个表的 ...