LeetCode 844. Backspace String Compare (比较含退格的字符串)
题目标签:Stack
用的最基本的办法,设两个 stack,最后比较两个stack 是否相等。
Java Solution:
Runtime: 2 ms, faster than 44.83%
Memory Usage: 38.2 MB, less than 6.06%
完成日期:02/15/2020
关键点:stack
class Solution { public boolean backspaceCompare(String S, String T) { Stack<Character> stack1 = new Stack<>(); Stack<Character> stack2 = new Stack<>(); // stack1 for(char c : S.toCharArray()) { if(c != '#') stack1.push(c); else if(!stack1.empty()) stack1.pop(); } // stack2 for(char c : T.toCharArray()) { if(c != '#') stack2.push(c); else if(!stack2.empty()) stack2.pop(); } return stack1.equals(stack2); } }
参考资料:n/a
LeetCode 题目列表 - LeetCode Questions List
题目来源:https://leetcode.com/