当前位置:网站首页>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)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
边栏推荐
- Flink | 多流转换
- centso7 openssl 报错Verify return code: 20 (unable to get local issuer certificate)
- cmake 学习使用笔记(一)
- JS中为什么基础数据类型可以调用方法
- Isprs2021/ remote sensing image cloud detection: a geographic information driven method and a new large-scale remote sensing cloud / snow detection data set
- Problems that cannot be accessed in MySQL LAN
- User management summary of mongodb
- 【面试高频题】难度 2.5/5,简单结合 DFS 的 Trie 模板级运用题
- OSI 七层模型
- PAcP learning note 1: programming with pcap
猜你喜欢

PAcP learning note 1: programming with pcap

Ways to improve the performance of raspberry pie

JS slow motion animation principle teaching (super detail)

flask session伪造之hctf admin

交付效率提升52倍,运营效率提升10倍,看《金融云原生技术实践案例汇编》(附下载)
![Scripy tutorial classic practice [New Concept English]](/img/bc/f1ef8b6de6bfb6afcdfb0d45541c72.png)
Scripy tutorial classic practice [New Concept English]

Ogre入门尝鲜

Write it down once Net a new energy system thread surge analysis

Introduce six open source protocols in detail (instructions for programmers)

Getting started with cinnamon applet
随机推荐
单片机学习笔记之点亮led 灯
线程池拒绝策略最佳实践
QQ的药,腾讯的票
靠卖概念上市,认养一头牛能走多远?
[learning notes] agc010
提升树莓派性能的方法
Why can basic data types call methods in JS
单片机原理期末复习笔记
Milkdown 控件图标
MongoDB 遇见 spark(进行整合)
xshell连接服务器把密钥登陆改为密码登陆
My "troublesome" subordinates after 00: not bad for money, against leaders, and resist overtime
工具箱之 IKVM.NET 项目新进展
一文读懂数仓中的pg_stat
Getting started with MySQL
Vscade editor esp32 header file wavy line does not jump completely solved
服务器到服务器 (S2S) 事件 (Adjust)
自定义线程池拒绝策略
centso7 openssl 报错Verify return code: 20 (unable to get local issuer certificate)
Final review notes of single chip microcomputer principle