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 两个字符串,当它们分别被输入到空 ...
随机推荐
- java web项目使用IDEA打成war包
步骤: 1.点击 File -->Project Structure...如下图: 2.出现如下界面后点击 Artifacts--> 绿色加号-->Web Application:A ...
- mongo aggregate 用法记录
mongo 聚合查询查询还是很方便的,做下记录 依赖的jar是org.springframework.data.mongodb 1.9.6 低版本可能不支持. 数据结构 大概是 这是一份 ...
- java后端实习生面试题目
1.编程题:java从10000到99999找到AABB类型 public class Test1 { public static void main(String[] args) { String ...
- Maven仓库—Nexus环境搭建及使用
使用Sonatype Nexus搭建Maven私服后如何添加第三方JAR包 http://blog.csdn.net/yanjun008/article/details/42084109 Nexus介 ...
- Haskell语言学习笔记(77)Data.HashSet
安装 unordered-containers $ cabal install unordered-containers Installed unordered-containers-0.2.9.0 ...
- 将 DNSCrypt 部署到 Openwrt 路由器上+ DNSmasq 解析国内域名用本地 DNS[ZT+实践]
原文地址: 1.https://typcn.com/legacy/blog/posts/openwrt-dnscypt.html 2.http://www.openwrt.pro/post-376.h ...
- 为什么使用DLL
(1) 如果不同的程序使用相同的 DLL,只需将 DLL 在内存中装载一次,这样节省了系统内存.DLL 映射到每个进程(每运行一次应用程序)的专用地址空间中,但它们的代码使用的内存影像程序只在内存中装 ...
- shiro中的授权
- 吴裕雄 03-mysql连接
mysqli_connect(host,username,password,dbname,port,socket);参数 描述host 可选.规定主机名或 IP 地址.username 可选.规定 M ...
- 转载一篇必须超级好的JVM配置实战
不管是YGC还是Full GC,GC过程中都会对导致程序运行中中断,正确的选择不同的GC策略,调整JVM.GC的参数,可以极大的减少由于GC工作,而导致的程序运行中断方面的问题,进而适当的提高Java ...