[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 ...
随机推荐
- ExecuteScalar
ExecuteScalar运行查询,并返回查询所返回的结果集中第一行的第一列或空引用(假设结果集为空).忽略其它列或行. 使用 ExecuteScalar 方法从数据库中检索单个值. 由于不用创建行集 ...
- 一个可无限伸缩且无ABA问题的无锁队列
关于无锁队列,详细的介绍请参考陈硕先生的<无锁队列的实现>一文.然进一步,如何实现一个不限node数目即能够无限伸缩的无锁队列,即是本文的要旨. 无锁队列有两种实现形式,分别是数组与链表. ...
- SET ANSI_NULLS (Transact-SQL)
指定在 SQL Server 2014 中与 Null 值一起使用等于 (=) 和不等于 (<>) 比较运算符时采用符合 ISO 标准的行为. 当 SET ANSI_NULLS 为 ON ...
- linux杂记(五)正确关机方法(shutdown,reboot,init,halt)
前言:由于在linux底下,每个程序(或者说是服务)都是在背景下运行的,因此,在你看不到的屏幕背后其实可能有相当多人同时在你的主机上面工作,如果 你直接按下电源开关来关机,则可能导致其他人的数据就此中 ...
- firebug如何使用
1.怎么安装firebug: a.打开火狐浏览器--------b.点击火狐浏览器的右上角这个小图标-------c.点击<获取附件组件>,在右上角的搜索框()内,输入firebug,点击 ...
- 制作OB图的时候,OB玩家进入后就退出的问题
开始怀疑是 OB玩家没有建筑 所以强行退出了,有朋友说那是因为有无效的触发造成的 我没有测试过 最后解决是给OB玩家在地图中加上建筑 更新 最后测试,把OB玩家放到一个组里 开局KILL掉这个组的建筑 ...
- poj 1949 Chores 最长路
题目链接 求出最长路..... #include <iostream> #include <vector> #include <cstdio> #include & ...
- 【转】Centos 设置IP地址的几种方式
对于很多刚刚接触linux的朋友来说,如何设置linux系统的IP地址,作为第一步,下面小编以centos系统为例,给大家演示如何给centos设置IP地址,如何修改linux 系统IP地址? 查看I ...
- C语言2
函数是C语言的基本单位,类是java,c#,c++的基本单位 int abs(int x); double fabs(double x); 按变量的存储方式:静态变量.自动变量.寄存器变量 指针就 ...
- 面试题之 query转为obj
要注意处理编码后的字串 对于a=123要得到number形的值 function parseQueryString(url) { var obj = {}; var query = url.sear ...