c++ - Backspace String 比较给定两个字符串 s 和 t,如果相等则返回 true
问题描述
我使用堆栈的这个测试用例出现运行时错误
"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;
}
};
解决方案
我不会使用 a stack<char>
,因为自然表示是 a string
,并且您没有使用任何stack
能够在前面扩展或收缩的功能(除了您可以说的末尾return a == b
)。string
有push_back
和pop_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;
}
}
};
推荐阅读
- spring-boot - 使用 Spring WebFlux 的 Flutter Stream
- ag-grid - ag-grid 主详细信息展开/折叠功能
- amazon-web-services - 生产中的 AWS Lambda event.body 解析错误
- oracle - SQLPlus DBMS 输出与开发人员中的不同
- javascript - 正则表达式从单词创建链接,但如果单词包含三个点则不会
- python - 在列的行中选择有限的值(第 2 部分)
- angular - 如何在 Angular 中使用嵌套 *ngFor
- python - 具体的 Python cumsum
- java - 命令行中的 Java 执行因 JMX 错误而失败
- typescript - 使用 wasm 将模块导入并编译到 webpack 中