跟着Carl大佬学leetcode之844 比较含退格的字符串

来点强调,刷题是按照代码随想录的顺序进行的,链接如下https://www.programmercarl.com/本系列是记录一些刷题心得和学习过程,就看到题目自己先上手试试,然后看程序员Carl大佬的解释,自己再敲一遍修修补补,练题的小伙伴还是跟着大佬的解释比较系统


每日碎碎念

苦痛生活继续
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. 1 <= s.length, t.length <= 200
  2. 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()操作熟悉下…

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

进击的lab681

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值