当前位置:网站首页>844. compare strings with backspace
844. compare strings with backspace
2022-06-11 08:56:00 【Drag is me】
leetcode Force button to brush questions and punch in
subject :844. Compare strings with backspace
describe : Given s and t Two strings , When they are entered into a blank text editor , If the two are equal , return true .# For backspace characters .
Be careful : If you enter backspace characters for empty text , Text continues to be empty .
Their thinking
1、 meet # Just pop up at the top of the stack ;
2、 Originally I wanted to use the stack , As a result, I read the official answer , Oh my god , It turns out that the string itself can be pushed out of the stack , good heavens , Learned .
3、 The string itself is pushed onto the stack push_back(), Out of the stack is pop_back();
Source code ##
class Solution {
public:
string build(string str) {
string ret = "";
for (int i = 0; i < str.size(); ++i) {
if (str[i] != '#') {
ret.push_back(str[i]);
} else if (!ret.empty()) {
ret.pop_back();
}
}
return ret;
}
bool backspaceCompare(string s, string t) {
return build(s) == build(t);
}
};
边栏推荐
猜你喜欢

C语言打印菱形

leetcode - 230. The k-th smallest element in a binary search tree

Can not connect to local MySQL server through socket ‘/tmp/mysql. sock (2)‘

复制的代码格式混乱怎么办?

Codetop - sort odd ascending even descending linked list

The interviewer asked four questions and summed up four experiences

Sword finger offer 21 Adjust array order so that odd numbers precede even numbers

领导让我重写测试代码,我也要照办嘛?

c语言打印心形

Matlab学习9-图像处理之非线性锐化滤波
随机推荐
硅树脂油漆申请美国标准UL 790 Class A 合适吗?
Flutter开发日志——路由管理
光伏板怎么申请ASTM E108阻燃测试?
GCC AVR (ATMEL studio+ AVR studio) how to define the structure array in the program memory (flash) space and read it
Which Apple devices support this system update? See if your old apple device supports the latest system
Question d'entrevue 02.02. Renvoie l'avant - dernier noeud K
端口占用问题,10000端口
206. reverse linked list
Can not connect to local MySQL server through socket ‘/tmp/mysql. sock (2)‘
Codeworks round 723 (Div. 2)
win10家庭版如何连接远程桌面
86. 分隔链表
PVC 塑料片BS 476-6 火焰传播性能测定
Installation (detailed illustration) and use of SVN
1721. exchange nodes in the linked list
The interviewer asked four questions and summed up four experiences
MATLAB R2022a 安装教程
面試題 02.02. 返回倒數第 k 個節點
19. 删除链表的倒数第 N 个结点
How to apply for BS 476-7 sample for display? Is it the same as the display