【问题标题】:Backspace String Compare given two strings s and t, return true if they are equalBackspace String 比较给定两个字符串 s 和 t,如果相等则返回 true
【发布时间】:2021-12-25 14:40:11
【问题描述】:

我在使用堆栈时遇到此测试用例的运行时错误

 "bxj##tw", "bxj###tw"
Line 171: Char 16: runtime error: reference binding to misaligned address 0xbebebebebebec0ba for type 'int', which requires 4 byte alignment (stl_deque.h)
0xbebebebebebec0ba: note: pointer points here
<memory cannot be printed>
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_deque.h:180:16
class Solution {

public:
    bool backspaceCompare(string s, string t) {

        stack<int>st1;
        stack<int>st2;
        
        for(int i=0; i<s.size(); i++){

            if(st1.empty() && s[i]!='#'){

                st1.push(s[i]);
            }
            else{
                if(!st1.empty() && s[i]=='#'){

                    st1.pop();
                }
                else if(s[i]=='#' && (st1.empty())){

                    continue;
                }
                else{
                    st1.push(s[i]);
                }
            }
        }
        for(int i=0; i < t.size(); i++){

            if(st2.empty() && t[i]!='#'){

                st2.push(t[i]);
            }
            else{
                if(!st2.empty() && t[i]=='#'){

                    st2.pop();
                }
                else if(t[i]=='#' && st2.empty()){

                    continue;
                }
                else{

                    st2.push(t[i]);
                }
            }
        }
        if(st1.empty() && st2.empty()){

            return "";
        }
        while(!st1.empty()){

            if(st1.top()!= st2.top()){

                return false;
            }

            else{

                st1.pop();

                st2.pop();
            }
        }

        return true;
    }
};

【问题讨论】:

  • 您的while 循环不能确保st2 具有至少与st1 一样多的元素,因此您最终可能会得到比st2 更多的元素的pop()。尝试改用while(!st1.empty() &amp;&amp; !st2.empty()),然后在while 循环之后移动if(st1.empty() &amp;&amp; st2.empty()) 块。
  • 附带说明,return ""; 对于声明为返回bool 的函数没有意义。效果和return true;一样,就这么写吧。
  • “退格字符串比较”的定义是什么?

标签: c++ string stack


【解决方案1】:

我不会使用stack&lt;char&gt;,因为自然表示是string,并且您没有使用stack 在前面扩展或收缩的任何功能(除了结尾你可以说return a == b)。 string 也有 push_backpop_back 方法。

对于较小的输入(例如保证解决这个挑战问题的输入),我建议构建两个“编辑器”并与 == 进行比较:

class Solution {
public:
    bool backspaceCompare(string s, string t) {
        return backspace(s) == backspace(t);
    }

private:
    string backspace(const string &s) {
        string editor = "";
        string::const_iterator commandItr = s.cbegin();
        while(commandItr != s.cend())
            if(*commandItr == '#' && !editor.empty()) {
                editor.pop_back();
                ++commandItr;
            } else if(*commandItr != '#')
                editor.push_back(*commandItr++);
            else
                ++commandItr;
        
        return editor;
    }
};

但是,他们确实向编码员提出了使用 O(1) 内存的挑战。这是一个例子:

class Solution {
public:
    bool backspaceCompare(string s, string t) {
        int left = s.size() - 1;
        int right = t.size() - 1;
        while(true) {
            left = backspace(s, left);
            right = backspace(t, right);
            
            if (left == -1 && right == -1)
                return true;
            if (left == -1 && right != -1 || right == -1 && left != -1)
                return false;
            if(s[left--] != t[right--])
                return false;
        }
    }
private:
    // Returns first index from back that indexes to a non-deleted character
    int backspace(string const &s, int startingIndex) {       
        if(startingIndex == -1)
            return -1;
        
        if(s[startingIndex] != '#')
            return startingIndex;
        
        unsigned backspaceCount = 0;
        while(true) {
            while(startingIndex != -1 && s[startingIndex] == '#') {
                ++backspaceCount;
                --startingIndex;
            }

            while (startingIndex != -1 && backspaceCount && s[startingIndex] != '#') {
                --startingIndex;
                --backspaceCount;
            }
            
            if (startingIndex == -1)
                return -1;
            else if(s[startingIndex] != '#' && !backspaceCount)
                return startingIndex;
        }
    }
};

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-28
    • 1970-01-01
    相关资源
    最近更新 更多