【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 = ...
随机推荐
- Swift中类与结构的初始化
前言:通过学习与研究swift3.0的官方文档关于初始化这部分可以总结为一句话:类与结构是确保一个类型的实例可以使用之前,其所有存储属性都得到了正确的赋值. 一,结构的初始化1 struct Firs ...
- HTML基础标签入门
HTML基础标签 昨天学习了一些HTML的基本标签以及基本属性: HTML是一种超文本标记语言,其中PHP是世界上最好的语言(增加学习的动力荣誉感). HTML文档里包含三部分: <html&g ...
- PHP安装kafka插件
在工作中我们经常遇到需要给php安装插件,今天把php安装kafka的插件的步骤整理下,仅供大家参考 1:需要先安装librdkafka git clone https://github.com/ed ...
- inline-block元素vertical-align的问题分析
先来解释下这两个标签 inline-block: 字面意思:行内块元素,相当于强制转换,把一个标签设置为行内的块元素,既有块元素的部分特性(支持width\height\maigin-top\marg ...
- redis技巧--自动完成功能实现
自动完成功能一般都伴随搜索框出现,就是用户在输入时帮助其自动补全. 比如对成语进行补全,现有如下成语:一心一意,一心二用,一帆风顺. 两种实现方式: 实现方式一: 为每个成语的每个前缀都使用一个集合类 ...
- [转]C语言SOCKET编程指南
1.介绍 Socket编程让你沮丧吗?从man pages中很难得到有用的信息吗?你想跟上时代去编Internet相关的程序,但是为你在调用 connect() 前的bind() 的结构而不知所措?等 ...
- 【IOS】将字体大小不同的文字底部对齐
从WP转IOS了,还是放不下...... 在项目中,要实现如图多个不同大小的文字 底部对齐的效果 像下面这样: (想要的效果) 以为用三个UIFont不同的UILabel 之后让他们底部对齐 ...
- C# Async, Await and using statements
Async, Await 是基于 .NEt 4.5架构的, 用于处理异步,防止死锁的方法的开始和结束, 提高程序的响应能力.比如: Application area Support ...
- 搭建Apache Web服务器
1.下载Apache服务器的安装包 地址:http://httpd.apache.org/download.cgi 从http://archive.apache.org/dist/httpd/bina ...
- C#高级编程笔记 2016年10月26日 MVC入门 Controller
1.MVC的定义: Models: Classes that represent the data of the application and that use validation logi ...