码迷,mamicode.com
首页 > 编程语言 > 详细

LeetCode-20 Valid Parentheses Solution (with Java)

时间:2020-03-02 14:28:01      阅读:68      评论:0      收藏:0      [点我收藏+]

标签:character   switch   png   ISE   har   create   com   core   break   

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)

标签:character   switch   png   ISE   har   create   com   core   break   

原文地址:https://www.cnblogs.com/sheepcore/p/12395270.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!