当前位置:网站首页>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)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
边栏推荐
- Read PG in data warehouse in one article_ stat
- ESP32构解工程添加组件
- DID登陆-MetaMask
- Esp32 construction engineering add components
- 存储过程的介绍与基本使用
- Final review notes of single chip microcomputer principle
- MongoDB 遇见 spark(进行整合)
- How to make join run faster?
- centso7 openssl 报错Verify return code: 20 (unable to get local issuer certificate)
- MySQL error 28 and solution
猜你喜欢
Error lnk2019: unresolved external symbol
如何让join跑得更快?
[dark horse morning post] Huawei refutes rumors about "military master" Chen Chunhua; Hengchi 5 has a pre-sale price of 179000 yuan; Jay Chou's new album MV has played more than 100 million in 3 hours
Ogre introduction
LIS longest ascending subsequence problem (dynamic programming, greed + dichotomy)
Scripy tutorial classic practice [New Concept English]
About how appium closes apps (resolved)
Milkdown 控件图标
PAcP learning note 1: programming with pcap
分布式事务解决方案
随机推荐
Scripy tutorial classic practice [New Concept English]
Scrapy教程经典实战【新概念英语】
10 pictures open the door of CPU cache consistency
Introduce six open source protocols in detail (instructions for programmers)
Milkdown control icon
Vscade editor esp32 header file wavy line does not jump completely solved
Split screen bug notes
1. Deep copy 2. Call apply bind 3. For of in differences
OSI seven layer model
cmake 学习使用笔记(一)
Realize the IP address home display function and number home query
PAcP learning note 3: pcap method description
ROS机器人更换新雷达需要重新配置哪些参数
最佳实践 | 用腾讯云AI意愿核身为电话合规保驾护航
PHP - laravel cache
shell 批量文件名(不含扩展名)小写改大写
LIS 最长上升子序列问题(动态规划、贪心+二分)
1、深拷贝 2、call apply bind 3、for of for in 区别
Isprs2021/ remote sensing image cloud detection: a geographic information driven method and a new large-scale remote sensing cloud / snow detection data set
Toraw and markraw