LeetCode-20 Valid Parentheses Solution (with Java)
Posted sheepcore
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode-20 Valid Parentheses Solution (with Java)相关的知识,希望对你有一定的参考价值。
1. Description:
2. Examples:
3.Solutions:
1 /** 2 * Created by sheepcore on 2019-05-07 3 */ 4 5 class Solution { 6 public boolean isValid(String s) { 7 Stack<Character> stack = new Stack<Character>(); 8 9 for(char ch : s.toCharArray()){ 10 switch (ch){ 11 case \'(\': 12 case \'{\': 13 case \'[\': stack.push(ch); break; 14 case \')\': 15 if(!stack.isEmpty() && stack.peek() == \'(\') 16 stack.pop(); 17 else 18 return false; 19 break; 20 case \'}\': 21 if(!stack.isEmpty() && stack.peek() == \'{\') 22 stack.pop(); 23 else 24 return false; 25 break; 26 case \']\': 27 if(!stack.isEmpty() && stack.peek() == \'[\') 28 stack.pop(); 29 else 30 return false; 31 break; 32 } 33 } 34 return stack.isEmpty(); 35 } 36 }
以上是关于LeetCode-20 Valid Parentheses Solution (with Java)的主要内容,如果未能解决你的问题,请参考以下文章
[LeetCode]20. Valid Parentheses
[LeetCode]20 Valid Parentheses 有效的括号
Leetcode 20. Valid Parentheses