844比较含退格的字符串 Posted on 2020-04-22 Words count in article: 192 | Reading time ≈ 1 844比较含退格的字符串下面是题目 下面是题目给出的代码12345class Solution {public: bool backspaceCompare(string S, string T) { }}; 解答 还是要先判断特殊字符,在判断特殊字符的时候同时要判断栈是不是空的,因为如果是#且栈是空的,那也没有意义了 1234567891011121314151617181920212223242526class Solution {public: static void addchar(stack<char>&s,string S) { //因为string也是容器,我们也可以用for( : )来把s中每个元素赋值出来 for(char i : S) { if (i=='#'&&!s.empty()) { s.pop(); } else if(i<='z'&&i>='a') { s.push(i); } } } bool backspaceCompare(string S, string T) { stack<char>s; stack<char>t; addchar(s,S); addchar(t,T); return s==t? true:false; }}; -------------本文结束,感谢您的阅读------------- Post author: Jason Post link: https://jasonxqh.github.io/2020/04/22/844%E6%AF%94%E8%BE%83%E5%90%AB%E9%80%80%E6%A0%BC%E7%9A%84%E5%AD%97%E7%AC%A6%E4%B8%B2/ Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 3.0 unless stating additionally.