[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 ...
随机推荐
- AngularJs(一) MVC 模式的应用
Model的应用 MVC的模式大家都是十分熟悉了,那么Angular是怎么规划的呢.数据被放在json文件中,通过Ajax获取数据. [{ "action": "Buy ...
- Server.MapPath(string sFilePath) 报未将对象引用到实例异常
System.Web.HttpContext.Current.Server.MapPath(string sfilePath)将虚拟路径转换成物理路径.这个必须在aspx或者MVC中Action调用才 ...
- Excel文件转换为html静态网页
Excel文件转换为html静态网页:右键另存为:
- Csharp多态的实现(接口)
1.什么是接口 接口可以看做是一个标准, 所有继承的子类需要按照接口中声明的方法来 接口用关键字 interface 修饰,接口的名字一般是I.........able ,表示我有什么能力 接口一般是 ...
- tomcat无法正常启动的一个原因
简要报错信息: java.lang.IllegalArgumentException: Document base E:\apache-tomcat-7.0.65\webapps\springmvc0 ...
- Windows 桌面边栏小工具开发入门
准备为网站做一个桌面通知功能的工具,现在网上一般是html5+js的比较多.虽然html5+js现在是web的开发主流,但是我们应用一般是windows系统.并且应使用中,需要打开谷歌或其 ...
- 联想V480关闭UEFI安装Win7
联想V480关闭UEFI安装Win7 http://www.dadclab.com/archives/3283.jiecao 故事背景 兔兔牛入了一枚Lenovo V480,预装Win8,想换成 ...
- 关于scanf("%c",&ch)直接跳过的问题
有时候scanf("%c",&ch)本应该阻塞等待用户输入一个char型数据的,但为什么会跳过呢? 例:在该程序段中, int year; printf(" ...
- printf不同格式表示法
格式代码 A ABC ABCDEFGH %S A ABC ABCDEFGH %5S ####A ##ABC ABCDEFGH %.5S A ABC ABCDE %5.5S ####A ##ABC AB ...
- Linux 查看支持的语言,日期,时间,计算器
1.查看系统目前支持的语言 echo %LANG 2.查看日历 cal 3.查看日期时间 date 4.计算器 bc