【leetcode】Add Binary
题目简述:
Given two binary strings, return their sum (also a binary string).
For example,
a = "11"
b = "1"
Return "100".
解题思路:
class Solution:
# @param a, a string
# @param b, a string
# @return a string
def addBinary(self, a, b):
la = len(a) - 1
lb = len(b) - 1
f = 0
res = []
while la >= 0 or lb >= 0:
A = 0
B = 0
if la >= 0:
A = int(a[la])
la -= 1
if lb >= 0:
B = int(b[lb])
lb -= 1
if A + B +f >= 2:
res.append(str(A + B +f - 2))
f = 1
else:
res.append(str(A + B +f ))
f = 0
if f ==1:
res.append('1')
res.reverse()
return "".join(res)
【leetcode】Add Binary的更多相关文章
- 【LeetCode】199. Binary Tree Right Side View 解题报告(Python)
[LeetCode]199. Binary Tree Right Side View 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/probl ...
- 【LeetCode67】 Add Binary
题目描述: 解题思路: 此题的思路简单,下面的代码用StringBuilder更加简单,注意最后的结果要反转过来.[LeetCode415]Add Strings的解法和本题一模一样. java代码: ...
- 【LeetCode】145. Binary Tree Postorder Traversal
Difficulty: Hard More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/binary-tree-pos ...
- 【LeetCode】二叉查找树 binary search tree(共14题)
链接:https://leetcode.com/tag/binary-search-tree/ [220]Contains Duplicate III (2019年4月20日) (好题) Given ...
- 【LeetCode】Validate Binary Search Tree ——合法二叉树
[题目] Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defin ...
- 【LeetCode】Balanced Binary Tree 解题报告
[题目] Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bi ...
- 【leetcode】 Unique Binary Search Trees (middle)☆
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- 【题解】【链表】【Leetcode】Add Two Numbers
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- 【Leetcode】【Easy】Add Binary
Given two binary strings, return their sum (also a binary string). For example,a = "11"b = ...
随机推荐
- 后台动态添加的button,如何触发button_click事件?
后台动态添加的button,需要在Page_Load或者Page_Init重新动态生成才能执行button_click public Panel GetContrlType() { Panel pan ...
- word转化jpg
用画图复制粘贴word内容,然后保存为jpg 即可~亲测可行,比截图清洗太多.
- variadic function 的使用
最近在看<the c programming language> K&R 7.3章 Variable-length Argument Lists 变长参数列表, 笔记一下用法 1 ...
- 如何设置tomcat定时自动重启
,今天笔者就跟大家分享一下如何设置tomcat定时自动重启. 第一:把Tomcat6安装成windows 2003服务. 1.首先检查tomcat\bin目录下是否有service.bat文件,如图: ...
- mysql 语法
-create database database_name; show databases;show tables; | -creat ...
- 渐进式框架、自底向上增量开发的vue
官网对vue.js的介绍是这样的: 一套构建用户界面的 渐进式框架.与其他重量级框架不同的是,Vue 采用自底向上增量开发的设计. 那么到底什么是渐进式框架.自底向上增量开发又是什么呢? 其实我觉得这 ...
- liniux mint android-ndk风波
我的安装过程sudo chmod a+x android-ndk-r10d-linux-x86_64.bin/dowonload $ ./android-ndk-r10d-linux-x86_64.b ...
- 手写一个allocator
似乎就像是一个计算机原理的实践.. 首先介绍一下大多数操作系统的内存架构..对于某个程序它会认为自己是独占了整个系统的所有内存在运行的这样才能方便移植,因此人们搞出了虚拟内存和物理内存的区别,于是人们 ...
- day21
1.文件上传 a. Html Form表单提交 b. Ajax提交 原生XMLHttpRequest XmlHttpReqeust() 类 ...
- 在一个aspx或ashx页面里进行多次ajax调用
在用ajax开发asp.net程序里.利用ashx页面与前台页面进行数据交互.但是每个ajax交互都需要一个ashx页面.结果是项目里一大堆ashx页面.使项目难以管理.现在我们就想办法让一个ashx ...