Given a string containing just the characters '('
, ')'
, '{'
, '}'
, '['
and ']'
, determine if the input string is valid.
An input string is valid if:
Note that an empty string is also considered valid.
Example 1:
Example 2:
Example 3:
Example 4:
Example 5:
Related Topics: String
、Stack
這題是在驗證括號是否成對,看到這種題目第一反應就是用 Stack:
依照題目的輸入限制,進一步優化與精簡程式碼:
本文作者: 辛西亞.Cynthia
本文連結: 辛西亞的技能樹 / hackmd 版本
版權聲明: 部落格中所有文章,均採用 姓名標示-非商業性-相同方式分享 4.0 國際 (CC BY-NC-SA 4.0) 許可協議。轉載請標明作者、連結與出處!