problem
solution
遍歷字串,遇到字母加進字串
ret,遇到左括號push stack,遇到右括號且如果stack為空,則不加入字串ret,代表目前為止右括號出現次數多餘左括號,遇到右括號且stack 不為空則加入字串ret並pop stack。最後檢查是否有左括號在字串
ret後方且沒有對應的右括號,並將他移除。
1  | class Solution {  | 
analysis
- time complexity 
O(n) - space complexity 
O(n) 
遍歷字串,遇到字母加進字串ret,遇到左括號push stack,遇到右括號且如果stack為空,則不加入字串ret,代表目前為止右括號出現次數多餘左括號,遇到右括號且stack 不為空則加入字串ret並pop stack。
最後檢查是否有左括號在字串ret後方且沒有對應的右括號,並將他移除。
1  | class Solution {  | 
O(n)O(n)