题目: https://leetcode-cn.com/problems/backspace-string-compare/
思路:利用栈
遍历每个字符串
如果遍历到的字符不是‘#’,则将字符压入栈中,如果遍历到的字符是‘#’;则判断栈是否为空,为空就继续遍历下一个字符,不为空则将栈顶元素删掉
遍历结束后将得到两个栈
如果两个栈都为空 则返回true,否则返回false
class Solution {
public:
stackstk1, stk2;
bool backspaceCompare(string S, string T) {
int len1 = S.size();
int len2 = T.size();
for(int i = 0; i < len1; i++) {
if (S[i] != ‘#’)
{
stk1.push(S[i]);
}
else {
if (!stk1.empty())
stk1.pop();
else continue;
}
}
for (int i = 0; i < len2; i++) {
if (T[i] != ‘#’)
{
stk2.push(T[i]);
}
else {
if (!stk2.empty())
stk2.pop();
else continue;
}
}
while ((!stk1.empty()) && (!stk2.empty()))
{
if (stk1.top() != stk2.top()) {
return false;
}
else {
stk1.pop();
stk2.pop();
}
}
if (stk1.empty() && stk2.empty())
return true ;
else
return false;
}
};