python stack 844. Backspace String Compare
阿新 • • 發佈:2018-12-10
leetcode 844 字串匹配,遇到‘#’回退
思想:使用棧,遍歷字串,若不為‘#’,壓棧,如果棧不空,出棧,如果空,繼續遍歷
class Solution(object): def backspaceCompare(self, S, T): """ :type S: str :type T: str :rtype: bool """ stack1=[] stack2=[] for s1 in S: if s1!='#': stack1.append(s1) elif stack1: stack1.pop() else: continue for s2 in T: if s2!='#': stack2.append(s2) elif stack2: stack2.pop() else: continue if stack1==stack2: return True else: return False