题目:

      Given a string containing just the characters'(',')', '{', '}', '[' and']', determine if the input string is valid.

      The brackets must close in the correct order,"()" and"()[]{}" are all valid but "(]" and"([)]" are not.

      给定一个字符串,只包含字符'(','),' {','} ','['和'],判断输入字符串是否有效。括号必须以正确的顺序关闭”和“()(){ }”都是有效的但“()”和“()”。


思路:

  这是一道验证配对情况的题,是对栈的应用,有效括号就是有一个右括号必定就有一个左括号在前面,将左括号push进栈中,遇到右括号时再pop消除,这种时候不必担心连续不同种类左括号,有效的括号对最终还是会有紧邻的括号对,比如说{[]}。栈在peek或者pop操作之前要验证非空,否则会抛出StackEmptyException。

LeetCode编程练习 - Valid Parentheses学习心得





相关文章:

  • 2021-11-16
  • 2021-06-04
  • 2021-10-03
  • 2021-04-06
  • 2021-12-11
  • 2021-06-26
  • 2021-04-24
  • 2021-06-02
猜你喜欢
  • 2021-11-24
  • 2021-08-12
  • 2021-09-26
  • 2021-05-13
  • 2021-06-05
  • 2021-06-25
  • 2021-08-04
相关资源
相似解决方案