来点强调,刷题是按照代码随想录的顺序进行的,链接如下https://www.programmercarl.com/
本系列是记录一些刷题心得和学习过程,就看到题目自己先上手试试,然后看程序员Carl大佬的解释,自己再敲一遍修修补补,练题的小伙伴还是跟着大佬的解释比较系统
文章目录
- 每日碎碎念
- 一、题目要求及测试点
- 844 比较含退格的字符串
- 测试点
- 提示
- 二、题解
- 自己上手
- 正经题解
- 二分法之用栈处理遍历
- 二分法之逆序双指针法
- 三、总结
每日碎碎念
苦痛生活继续
hello LeetCode,今天还是数组查找元素专项刷题…
一、题目要求及测试点
844 比较含退格的字符串
给定 s 和 t 两个字符串,当它们分别被输入到空白的文本编辑器后,如果两者相等,返回 true 。# 代表退格字符。
注意:如果对空文本输入退格字符,文本继续为空。
链接https://leetcode.cn/problems/backspace-string-compare/description/
测试点
示例 1:
输入:s = "ab#c", t = "ad#c"
输出:true
解释:s 和 t 都会变成 "ac"。
示例 2:
输入:s = "ab##", t = "c#d#"
输出:true
解释:s 和 t 都会变成 ""。
示例3:
输入:s = "a#c", t = "b"
输出:false
解释:s 会变成 "c",但 t 仍然是 "b"。
示例4:
输入:s = "y#fo##f", t = "y#f#o##f"
输出:true
提示
- 1 <= s.length, t.length <= 200
- s 和 t 只含有小写字母以及字符 ‘#’
二、题解
自己上手
代码如下:
class Solution {
public:bool backspaceCompare(string s, string t) {int slowIndex1 = 0;int fastIndex = 0;for (; fastIndex < s.size(); fastIndex++) {if (s[fastIndex] == '#' && slowIndex1 != 0)slowIndex1--;else if (s[fastIndex] != '#')s[slowIndex1++] = s[fastIndex];}if (slowIndex1 <= 0) {slowIndex1 = 0;s = "";}int slowIndex2 = 0;for (fastIndex = 0; fastIndex < t.size(); fastIndex++) {if (t[fastIndex] == '#' && slowIndex2 != 0)slowIndex2--;else if (t[fastIndex] != '#')t[slowIndex2++] = t[fastIndex];}if (slowIndex2 <= 0) {slowIndex2 = 0;t = "";}// printf("%d %d\n", slowIndex1,slowIndex2);// printf("%s %s", s.c_str(), t.c_str());if (slowIndex1 != slowIndex2)return false;else {for (int i = 0; i < slowIndex1; i++) {if (s[i] != t[i])return false;}}return true;}
};
来点无用总结:
时间复杂度O(n),空间复杂度O(1),被示例4卡了下,后发现是用的else没用else if,准确是对fastIndex是否为#做区分;
对两个字符串都进行退格操作,然后slow就是退格后字符串长,中间注意对空字符串退格无效的情况…总结着发现有段判定是无用的
class Solution {
public:bool backspaceCompare(string s, string t) {int slowIndex1 = 0;int fastIndex = 0;for (; fastIndex < s.size(); fastIndex++) {if (s[fastIndex] == '#' && slowIndex1 != 0)slowIndex1--;else if (s[fastIndex] != '#')s[slowIndex1++] = s[fastIndex];}int slowIndex2 = 0;for (fastIndex = 0; fastIndex < t.size(); fastIndex++) {if (t[fastIndex] == '#' && slowIndex2 != 0)slowIndex2--;else if (t[fastIndex] != '#')t[slowIndex2++] = t[fastIndex];}if (slowIndex1 != slowIndex2)return false;else {for (int i = 0; i < slowIndex1; i++) {if (s[i] != t[i])return false;}}return true;}
};
正经题解
两个思路,一是用栈处理遍历,二是逆序双指针法
二分法之用栈处理遍历
重构字符串,思路和我一致,只是因为这种删除退格很类似删除栈底操作…
for (char ch : x) 等价于 for (int i=0; i< x.length(); i++){ char ch = x[i] …}
如果ch是普通字符,那么我们将其压入栈中
如果ch是退格符,那么我们将栈顶弹出;
class Solution {
public:bool backspaceCompare(string s, string t) {return (judge(s) == judge(t)); }string judge(string x){ string res;//存退格后结果for (char ch : x){ if (ch != '#')res.push_back(ch); else if (!res.empty())res.pop_back(); }return res; }
};
时间复杂度:O(N+M),空间复杂度:O(N+M),其中 N 和 M 分别为字符串 s 和 t 的长度。主要为还原出的字符串的开销。
二分法之逆序双指针法
https://leetcode.cn/problems/backspace-string-compare/solutions/451606/bi-jiao-han-tui-ge-de-zi-fu-chuan-by-leetcode-solu/
时间复杂度:O(N+M),空间复杂度:O(1)
三、总结
1.C++ 中 printf输出string字符串不能直接printf(“%s”,str),可以借助str.c_str()函数对字符串str进行转换printf(“%s\n”,x.c_str()),再输出。
2.str.popback(),str.push_back,str.empty()操作熟悉下…