当前位置:网站首页>2022-7-7 Leetcode 844.比较含退格的字符串
2022-7-7 Leetcode 844.比较含退格的字符串
2022-07-07 11:36:00 【weixin_51187533】


方法一:栈
class Solution {
public:
bool backspaceCompare(string s, string t) {
string tmps = "", tmpt = "";
for (auto& ch:s){
if (ch == '#'){
if (!tmps.empty())
tmps.pop_back();
}else tmps.push_back(ch);
}
for (auto& ch:t){
if (ch == '#'){
if (!tmpt.empty())
tmpt.pop_back();
}else tmpt.push_back(ch);
}
return tmps == tmpt;
}
};
方法二:双指针
class Solution {
public:
bool backspaceCompare(string S, string T) {
int i = S.length() - 1, j = T.length() - 1;
int skipS = 0, skipT = 0;
while (i >= 0 || j >= 0) {
while (i >= 0) {
if (S[i] == '#') {
skipS++, i--;
} else if (skipS > 0) {
skipS--, i--;
} else {
break;
}
}
while (j >= 0) {
if (T[j] == '#') {
skipT++, j--;
} else if (skipT > 0) {
skipT--, j--;
} else {
break;
}
}
if (i >= 0 && j >= 0) {
if (S[i] != T[j]) {
return false;
}
} else {
if (i >= 0 || j >= 0) {
return false;
}
}
i--, j--;
}
return true;
}
};
作者:demigodliu
链接:https://leetcode.cn/problems/backspace-string-compare/solution/shuang-zhi-zhen-bi-jiao-han-tui-ge-de-zi-8fn8/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
边栏推荐
- PHP - laravel cache
- Storage principle inside mongodb
- JS slow motion animation principle teaching (super detail)
- clion mingw64中文乱码
- Some principles of mongodb optimization
- JS缓动动画原理教学(超细节)
- LIS longest ascending subsequence problem (dynamic programming, greed + dichotomy)
- 【堡垒机】云堡垒机和普通堡垒机的区别是什么?
- 存储过程的介绍与基本使用
- Ogre入门尝鲜
猜你喜欢

Centso7 OpenSSL error Verify return code: 20 (unable to get local issuer certificate)

交付效率提升52倍,运营效率提升10倍,看《金融云原生技术实践案例汇编》(附下载)
![[learning notes] agc010](/img/2c/37f2537a4dadd84adacf3da5f1327a.png)
[learning notes] agc010

Cinnamon taskbar speed

提升树莓派性能的方法

Scrapy教程经典实战【新概念英语】

MySQL error 28 and solution

Ogre introduction
![Scripy tutorial classic practice [New Concept English]](/img/bc/f1ef8b6de6bfb6afcdfb0d45541c72.png)
Scripy tutorial classic practice [New Concept English]

Detr introduction
随机推荐
Mongodb replication (replica set) summary
Mongodb slice summary
[etc.] what are the security objectives and implementation methods that cloud computing security expansion requires to focus on?
华为镜像地址
shell 批量文件名(不含扩展名)小写改大写
Digital IC Design SPI
DrawerLayout禁止侧滑显示
JS determines whether an object is empty
toRaw和markRaw
LED light of single chip microcomputer learning notes
存储过程的介绍与基本使用
干货|总结那些漏洞工具的联动使用
Cinnamon taskbar speed
Scrapy教程经典实战【新概念英语】
分布式事务解决方案
Storage principle inside mongodb
MongoDB命令汇总
Ogre入门尝鲜
MongoDB 分片总结
Cinnamon Applet 入门