当前位置:网站首页>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)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
边栏推荐
- Deep understanding of array related problems in C language
- QQ的药,腾讯的票
- Centso7 OpenSSL error Verify return code: 20 (unable to get local issuer certificate)
- MATLAB中polarscatter函数使用
- 10 pictures open the door of CPU cache consistency
- 学习突围2 - 关于高效学习的方法
- xshell连接服务器把密钥登陆改为密码登陆
- Esp32 ① compilation environment
- MySQL入门尝鲜
- DID登陆-MetaMask
猜你喜欢

cmake 学习使用笔记(一)

交付效率提升52倍,运营效率提升10倍,看《金融云原生技术实践案例汇编》(附下载)

Error lnk2019: unresolved external symbol

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

Isprs2021/ remote sensing image cloud detection: a geographic information driven method and a new large-scale remote sensing cloud / snow detection data set

DETR介绍
![[learning notes] agc010](/img/2c/37f2537a4dadd84adacf3da5f1327a.png)
[learning notes] agc010

作战图鉴:12大场景详述容器安全建设要求

Cinnamon Applet 入门

QQ medicine, Tencent ticket
随机推荐
1. Deep copy 2. Call apply bind 3. For of in differences
LIS longest ascending subsequence problem (dynamic programming, greed + dichotomy)
PHP - laravel cache
作战图鉴:12大场景详述容器安全建设要求
Digital IC Design SPI
Isprs2021/ remote sensing image cloud detection: a geographic information driven method and a new large-scale remote sensing cloud / snow detection data set
PAcP learning note 1: programming with pcap
Mongodb slice summary
学习突围2 - 关于高效学习的方法
Fast development board pinctrl and GPIO subsystem experiment for itop-imx6ull - modify the device tree file
Shell batch file name (excluding extension) lowercase to uppercase
高端了8年,雅迪如今怎么样?
Clion mingw64 Chinese garbled code
OSI 七层模型
Distributed transaction solution
解决缓存击穿问题
MongoDB内部的存储原理
JS判断一个对象是否为空
迅为iTOP-IMX6ULL开发板Pinctrl和GPIO子系统实验-修改设备树文件
Flink | 多流转换