【LeetCode】844. Backspace String Compare 解题报告(Python)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/backspace-string-compare/description/
题目描述
Given two strings S and T, return if they are equal when both are typed into empty text editors. # means a backspace character.
Example 1:
Input: S = "ab#c", T = "ad#c"
Output: true
Explanation: Both S and T become "ac".
Example 2:
Input: S = "ab##", T = "c#d#"
Output: true
Explanation: Both S and T become "".
Example 3:
Input: S = "a##c", T = "#a#c"
Output: true
Explanation: Both S and T become "c".
Example 4:
Input: S = "a#c", T = "b"
Output: false
Explanation: S becomes "c" while T becomes "b".
Note:
- 1 <= S.length <= 200
- 1 <= T.length <= 200
- S and T only contain lowercase letters and ‘#’ characters.
Follow up:
- Can you solve it in O(N) time and O(1) space?
题目大意
在一个空白的编辑器里连续输入两段字符,其中#代表退格,要求最后两段字符是否相同。
有个Follow up,问我们能不能使用O(n)的时间复杂度和O(1)的空间复杂度。
解题方法
字符串切片
字符串题对于Python而言都不算题。就是按照题目要求做一遍就好了。
遇到#,字符串不为空,就删除最后一个字符。如果不是#号,就拼接到字符串的最后。把两个字符串都求出来,然后比较就好。
注意,我不小心踏进了一个坑,因为看到两个连续的if,就把它们合并在一起了,其实不行的:
if s == '#':
if ans_S:
ans_S = ans_S[:-1]
我给改成了:
if s == '#' and ans_S:
ans_S = ans_S[:-1]
这样看着好看了,其实是错的。因为如果字符串是空的,那么输入#号,会把这个#号拼接到字符串上去。
Follow up的要求暂时不会。
代码如下:
class Solution(object):
def backspaceCompare(self, S, T):
"""
:type S: str
:type T: str
:rtype: bool
"""
ans_S = ""
ans_T = ""
for s in S:
if s == '#':
if ans_S:
ans_S = ans_S[:-1]
else:
ans_S += s
for t in T:
if t == '#':
if ans_T:
ans_T = ans_T[:-1]
else:
ans_T += t
return ans_S == ans_T
栈
使用一个栈的话,可以完美处理这个问题,遇到#退栈就好了,唯一需要注意的时候如果栈是空的时候,不能退栈。
class Solution(object):
def backspaceCompare(self, S, T):
"""
:type S: str
:type T: str
:rtype: bool
"""
stackS, stackT = [], []
for s in S:
if s != "#":
stackS.append(s)
elif stackS:
stackS.pop()
for t in T:
if t != "#":
stackT.append(t)
elif stackT:
stackT.pop()
return stackS == stackT
日期
2018 年 6 月 10 日 —— 等了两天的腾讯比赛复赛B的数据集,结果人家在复赛刚开始就给了。。
【LeetCode】844. Backspace String Compare 解题报告(Python)的更多相关文章
- [LeetCode] 844. Backspace String Compare 退格字符串比较
Given two strings S and T, return if they are equal when both are typed into empty text editors. # m ...
- 【Leetcode_easy】844. Backspace String Compare
problem 844. Backspace String Compare solution1: class Solution { public: bool backspaceCompare(stri ...
- [LeetCode&Python] Problem 844. Backspace String Compare
Given two strings S and T, return if they are equal when both are typed into empty text editors. # m ...
- 【LeetCode】443. String Compression 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 使用额外空间 不使用额外空间 日期 题目地址:htt ...
- LeetCode 942 DI String Match 解题报告
题目要求 Given a string S that only contains "I" (increase) or "D" (decrease), let N ...
- 844. Backspace String Compare判断删除后的结果是否相等
[抄题]: Given two strings S and T, return if they are equal when both are typed into empty text editor ...
- 844. Backspace String Compare
class Solution { public: bool backspaceCompare(string S, string T) { int szs=S.size(); int szt=T.siz ...
- 【LeetCode】62. Unique Paths 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/unique-pa ...
- [LeetCode] 844. Backspace String Compare_Easy tag: Stack **Two pointers
Given two strings S and T, return if they are equal when both are typed into empty text editors. # m ...
随机推荐
- 中兴交换机基础配置(备份、dhcp中继、monitor)
1. 备份配置 格式: copy tftp/sftp/ftp [vrf mng] root: 本地文件 远端文件 1. 通过tftp进行备份,vrf mng表示指定使用管理口链路连接 copy tft ...
- accelerate
accelerate accelerare, accumulare和accurate共享一个含义为to的词根,后半截分别是:fast, pile up, care (关心则精确). 近/反义词: ex ...
- academy
academy at/in school都行,academy一般用at. The word comes from the Academy in ancient Greece, which derive ...
- day28 进程(Process)
day28 进程(Process) 1.进程的概念 进程(Process)是计算机中的程序关于某数据集合上的一次运行活动,是系统进行资源分配和调度的基本单位,是操作系统结构的基础. # 进程是系统进行 ...
- 集合类——Collection、List、Set接口
集合类 Java类集 我们知道数组最大的缺陷就是:长度固定.从jdk1.2开始为了解决数组长度固定的问题,就提供了动态对象数组实现框架--Java类集框架.Java集合类框架其实就是Java针对于数据 ...
- Linux基础命令---smbpasswd管理samba密码
smbpasswd smbpasswd指令可以用来修改samba用户的的密码,该指令不仅可以修改本地samba服务器的用户密码,还可以修改远程samba服务器的用户密码. 此命令的适用范围:RedHa ...
- 软件测试人员必备的linux命令
1 目录与文件操作1.1 ls(初级)使用权限:所有人功能 : 显示指定工作目录下之内容(列出目前工作目录所含之档案及子目录). 参数 : -a 显示所有档案及目录 (ls内定将档案名或目录名称开头为 ...
- Can a C++ class have an object of self type?
A class declaration can contain static object of self type,it can also have pointer to self type,but ...
- springboot+vue集成mavon-editor,开发在线文档知识库
先睹为快,来看下效果: 技术选型 SpringBoot.Spring Security.Oauth2.Vue-element-admin 集成mavon-editor编辑器 安装 mavon-edit ...
- 【Linux】【专项突破】Linux重定向与管道
[专项突破]Linux重定向与管道 This article is written by Xrilang(Chinese Name:萌狼蓝天) If you want find me ,You can ...