链接:https://leetcode-cn.com/problems/backspace-string-compare python # 比较退格字符串 classSolution: defbackspaceStringCompare1(self,S:str,T:str)->bool: """ 栈的思想, 时间O(m+n),空间借助栈O(m+n) :param S: :param T: :return: "
Java 实现 classSolution{publicbooleanbackspaceCompare(String S, String T){inti=S.length() -1, j = T.length() -1;intsSkip=0, tSkip =0;while(i >=0|| j >=0) {// 找到字符串S所对应的结果的下一个字符while(i >=0) {if(S.charAt(i) =='#') { --i; ++sSkip; }elseif(sSki...
def backspaceCompare(self, s: str, t: str) -> bool: fast_s = len(s)-1 fast_t = len(t)-1 while fast_s>=0 or fast_t>=0: cnt_s = 0 cnt_t = 0 while fast_s>=0: if s[fast_s]=='#': cnt_s += 1 fast_s-=1 elif cnt_s>0: cnt_s-=1 fast_s -= 1 else: br...
class Solution { public boolean backspaceCompare(String S, String T) { int i = S.length() - 1, j = T.length() - 1; int sSkip = 0, tSkip = 0; while (i >= 0 || j >= 0) { // 找到字符串S所对应的结果的下一个字符 while (i >= 0) { if (S.charAt(i) == '#') ...
还要注意一些python特殊的写法,yeild是返回值但是不终止程序。itertools.zip_longest就是可以同时循环,并且fit最长的那个。all就是所有值 class Solution: defbackspaceCompare(self, S:str, T:str)->bool:returnall(s == tfors, tinitertools.zip_longest(self.next_character(S),self.next_character(T))) ...
publicbooleanbackspaceCompare(String S, String T) { inti = S.length() -1, j = T.length() -1; while(true) { for(intback =0; i >=0&& (back >0|| S.charAt(i) =='#'); --i) back += S.charAt(i) =='#'?1: -1; ...
python版本:class Solution: def backspaceCompare(self, s: str, t: str) -> bool: def back(s, p): count = 0 while p>=0 and count != 0 or s[p] == '#': count = count+1 if s[p] == '#' else count-1 p -=
classSolution {public:boolbackspaceCompare(stringS,stringT) {strings ="", t ="";for(charc : S) c =='#'? s.size() >0? s.pop_back() :void() : s.push_back(c);for(charc : T) c =='#'? t.size() >0? t.pop_back() :void() : t.push_back(c);returns ==t; ...