zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    Merge Two Sorted Lists
    4Sum
    Letter Combinations of a Phone Number
    3Sum Closest
    3Sum
    Longest Common Prefix
    Roman to Integer
    Integer to Roman
    Container With Most Water
    Regular Expression Matching
  • 原文地址:https://www.cnblogs.com/sheepcore/p/12395270.html
Copyright © 2011-2022 走看看