844. Backspace String Compare判断删除后的结果是否相等
[抄题]:
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".
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
[思维问题]:以为要用stack:可是应该考虑这个占的空间比较大
[英文数据结构或算法,为什么不用别的数据结构或算法]:
统计numOfBlankspaces的个数,然后看i j是否同时倒数到1
[一句话思路]:
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
统计numOfBlankspaces的个数,然后看i j是否同时倒数到1
[复杂度]:Time complexity: O(n) Space complexity: O(n)
[算法思想:迭代/递归/分治/贪心]:
[关键模板化代码]:
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
[是否头一次写此类driver funcion的代码] :
[潜台词] :
class Solution {
public boolean backspaceCompare(String S, String T) {
for (int i = S.length() - 1, j = T.length() - 1; ; i--, j--) {
for (int numOfBlankspaces = 0; i >= 0 && (numOfBlankspaces > 0 || S.charAt(i) == '#'); i--) {
numOfBlankspaces = S.charAt(i) == '#' ? numOfBlankspaces + 1 : numOfBlankspaces - 1;
}
for (int numOfBlankspaces = 0; j >= 0 && (numOfBlankspaces > 0 || T.charAt(j) == '#'); j--) {
numOfBlankspaces = T.charAt(j) == '#' ? numOfBlankspaces + 1 : numOfBlankspaces - 1;
}
if (i == -1 || j == -1 || S.charAt(i) != T.charAt(j)) return (i == -1) && (j == -1);
}
}
}
844. Backspace String Compare判断删除后的结果是否相等的更多相关文章
- 【Leetcode_easy】844. Backspace String Compare
problem 844. Backspace String Compare solution1: class Solution { public: bool backspaceCompare(stri ...
- [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 ...
- 844. Backspace String Compare
class Solution { public: bool backspaceCompare(string S, string T) { int szs=S.size(); int szt=T.siz ...
- 【LeetCode】844. Backspace String Compare 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字符串切片 栈 日期 题目地址:https://le ...
- [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] Backspace String Compare 退格字符串比较
Given two strings S and T, return if they are equal when both are typed into empty text editors. # m ...
- [Swift]LeetCode844. 比较含退格的字符串 | Backspace String Compare
Given two strings S and T, return if they are equal when both are typed into empty text editors. # m ...
- LeetCode算法题-Backspace String Compare(Java实现)
这是悦乐书的第327次更新,第350篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第197题(顺位题号是844).给定两个字符串S和T,如果两个字符串都输入到空文本编辑器 ...
- C#LeetCode刷题之#844-比较含退格的字符串(Backspace String Compare)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4030 访问. 给定 S 和 T 两个字符串,当它们分别被输入到空 ...
随机推荐
- linux查询硬件信息
硬件信息查询 sudo dmidecode -t baseboard
- 2018SDIBT_国庆个人第三场
A - A CodeForces - 1042A There are nn benches in the Berland Central park. It is known that aiai peo ...
- delphi 新版内存表 FDMemTable
c++builder XE 官方demo最全60多个 http://community.embarcadero.com/blogs?view=entry&id=8761 FireDAC.Com ...
- Delphi的程序单元、结构、基础知识(转)
Object Passal的程序结构很特殊,与其它语言如C++,Object Windows等结构都不同.一个Delphi程序由多个称为单元的源代码模块组成.使用单元可以把一个大型程序分成多个逻辑相关 ...
- Delphi Class of 类引用
Delphi Class of 类引用也就是类的类型,也可说是指向类的指针 Type TControlCls = Class of TControl;function CreateComponent( ...
- week06 08 postman 测试jsonrpc
用postman来测试rpc需要添加特别的字段 ’ { "jsonrpc":"2.0", "id":"123", &qu ...
- MVC part4
SpringMVC 注解 @Controller 负责注册一个bean 到spring 上下文中,bean 的ID 默认为类名称开头字母小写,你也可以自己指定, 如下 方法一: @Controller ...
- windows7 IIS7报错:如果要使用托管的处理程序,请安装 ASP.NET
IIS7报错:如果要使用托管的处理程序,请安装 ASP.NET windows7,部署在本地的IIS7里以后,结果不能访问承载SL的.aspx页面,而如果用.html承载则可以访问. 亲测可用修复办法 ...
- svn:Cannot negotiate authentication mechanism
解决方案:在eclipse->window->preference->team->svn中将svn接口设定为svnkit.
- org注释包
1.注释包 物种 OrgDB 按蚊(Anopheles) org.Ag.eg.db 拟南芥(Arabidopsis) org.At.tair.db 牛(Brovine) org.Bt.eg.db 蠕虫 ...