【LeetCode每天一题】Add Binary(二进制加法)
Given two binary strings, return their sum (also a binary string).The input strings are both non-empty and contains only characters 1 or 0.
Example 1:
Input: a = "11", b = "1"
Output: "100"
Example 2:
Input: a = "1010", b = "1011"
Output: "10101" 思路
这道题和字符串加法思路是一样的,两个字符串都从尾向前遍历,使用溢出标志量进行记录是否存在溢出。直到遍历完毕。时间复杂度为O(n+m), n,m为a,b字符串的长度,空间复杂度为O(N), N为n,m中最大值加1。
解题思路
class Solution(object):
def addBinary(self, a, b):
"""
:type a: str
:type b: str
:rtype: str
"""
res, flow= '', 0 # 设置结果变量和溢出变量
i, j = len(a) - 1, len(b) - 1 # 两个字符串长度
while i >= 0 or j >= 0 or flow: # 循环变量
curval = (i >= 0 and a[i] == '') + (j >= 0 and b[j] == '') # 每一位相加的结果
flow, rem = divmod(curval + flow, 2) # 存储结果
res = `rem` + res
i -= 1
j -= 1
return res
【LeetCode每天一题】Add Binary(二进制加法)的更多相关文章
- leetCode 67.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]67. Add Binary 二进制加法
Given two binary strings, return their sum (also a binary string). The input strings are both non-em ...
- # Leetcode 67:Add Binary(二进制求和)
Leetcode 67:Add Binary(二进制求和) (python.java) Given two binary strings, return their sum (also a binar ...
- Leetcode 第 2 题(Add Two Numbers)
Leetcode 第 2 题(Add Two Numbers) 题目例如以下: Question You are given two linked lists representing two non ...
- LeetCode算法题-Add Binary(Java实现)
这是悦乐书的第157次更新,第159篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第16题(顺位题号是67).给定两个二进制字符串,返回它们的总和(也是二进制字符串).输 ...
- Add Strings大整数加法十进制求和 & Add Binary二进制求和
[抄题]: 以字符串的形式给出两个非负整数 num1 和 num2,返回 num1和 num2 的和. 比如一个50位+一个100位. 给定 num1 = "123",num2 = ...
- Leetcode 67 Add Binary 大数加法+字符串处理
题意:两个二进制数相加,大数加法的变形 大数加法流程: 1.倒置两个大数,这一步能使所有大数对齐 2.逐位相加,同时进位 3.倒置两个大数的和作为输出 class Solution { public: ...
- 067 Add Binary 二进制求和
给定两个二进制字符串,返回他们的和(用二进制表示).案例:a = "11"b = "1"返回 "100" .详见:https://leetc ...
随机推荐
- 【winform】userControl刷新父窗体的datagridview
1.ContextMenuStrip 获取右键控件名称 this.contextMenuScriptScore.SourceControl.Name; //当前控件名 2.radiobutton 分组 ...
- 继承了AppCompatActivity的全屏设置
v7下全屏设置:getSupportActionBar().hide();getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN ...
- java 安装以及配置
1.下载 JDK 下载地址:https://www.oracle.com/technetwork/java/javase/downloads/index-jsp-138363.html 2.环境变量配 ...
- Aspnet Mvc 前后端分离项目手记(一) 关于跨域问题(还有前言)
前言,最近的项目使用前后端分离的模式,记录其中一些知识点.经过这个项目,也对前后端分离有了更多理解,尤其是在技术之外的方面. 越来越多的项目采用前后端分离的原因,有两点: 1,技术方面的原因 ...
- ASP.NET Core 3.0预览版体验
目前.NET Core 3.0的版本为.NET Core 3.0 Preview 3,对应ASP.NET Core 3.0 Preview 3. ASP.NET Core 3.0 之后将不再支持.NE ...
- python flask_Sqlalchemy管理数据库
懒癌复发直接粘贴代码,算是做一个简单备份吧. #coding:utf8 from flask import Flask from flask_sqlalchemy import SQLAlchemy ...
- Redis自学笔记:4.4进阶-消息通知
4.4消息通知 4.4.1任务队列 传递任务的队列.与任务队列进行交互的实体有两类,一类是生产者,一类是消费者. 生产者将需要处理的任务放入任务队列中,二消费者不断从任务队列中读入任务 信息并执行. ...
- node环境配置
1.进入node的官网https://nodejs.org/en/download/ 2.选择自己需要的安装包 3.下载之后,直接安装http://www.runoob.com/nodejs/node ...
- MariaDB基本操作--(创建用户)(转)
一. 创建用户 命令: CREATE USER 'username'@'host' IDENTIFIED BY 'password'; 说明: username:你将创建的用户名 host:指定该用户 ...
- DataTable数据存储问题
需求: 我想要实现这样一个效果:用户可以在表中通过右键新建行.删除行(这两个后面再写),编辑数据后进行保存.这里保存需要做一个区分,就是新增的和修改的.他们的区别就是新增的主键为空,而修改的因为原本就 ...