zoukankan      html  css  js  c++  java
  • LeetCode--有效的括号

    题目:

      给定一个只包括'(',')','[',']','{','}'的字符串,判断字符串是否有效;

      有效字符串需满足:

        1、左括号必须用相同类型的右括号闭合

        2、左括号必须以正确的顺序闭合

        3、空字符串可认为是有效字符串

      

      看到括号匹配就想到可能要用到栈,所以提交的代码是:

     1 import java.util.*;
     2 
     3 public class Solution {
     4     public boolean isValid(String s) {
     5         if(s == null || s.length() == 0) return true;
     6         
     7         Map<Character, Character> map = new HashMap<Character, Character>() {
     8             {
     9                 put(')', '(');
    10                 put('}', '{');
    11                 put(']', '[');
    12             }
    13         };
    14         
    15         Stack<Character> stack = new Stack<>();
    16         for(int i = 0; i < s.length(); i++)
    17         {
    18             if(s.charAt(i) == '(' || s.charAt(i) == '[' || s.charAt(i) == '{')
    19             {
    20                 stack.push(s.charAt(i));
    21             }
    22             else
    23             {
    24                 if(stack.empty() || stack.pop() != map.get(s.charAt(i)))
    25                 {
    26                     return false;
    27                 }
    28             }
    29         }
    30         return stack.empty();
    31     }
    32 }

      官方题解也是用到栈,思想是一致的,只是实现细节上有点不同,当括号的类型只有几种时,可以使用上面代码第18行使用的枚举法来逐一判断,但如果候选类型很多的话,使用枚举法就不实际了, 更好的方法是使用map的containsKey或containsValue方法来判断,如下代码所示:

     1 import java.util.*;
     2 
     3 public class Solution {
     4     
     5     public boolean isValid(String s) {
     6         if(s == null || s.length() == 0) return true;
     7         
     8         Map<Character, Character> map = new HashMap<Character, Character>() {
     9             {
    10                 put(')', '(');
    11                 put('}', '{');
    12                 put(']', '[');
    13             }
    14         };
    15         
    16         Stack<Character> stack = new Stack<>();
    17         for(int i = 0; i < s.length(); i++)
    18         {
    19             char c = s.charAt(i);
    20             {     
    21                 if(stack.empty() || stack.pop() != map.get(c))
    22                 {
    23                     return false;
    24                 }
    25             }
    26             else
    27             {
    28                 stack.push(c);
    29             }
    30         }
    31         return stack.empty();
    32     }
    33 }
  • 相关阅读:
    Understanding about Baire Category Theorem
    Isometric embedding of metric space
    Convergence theorems for measurable functions
    Mindmap for "Principles of boundary element methods"
    Various formulations of Maxwell equations
    Existence and uniqueness theorems for variational problems
    Kernels and image sets for an operator and its dual
    [loj6498]农民
    [luogu3781]切树游戏
    [atAGC051B]Three Coins
  • 原文地址:https://www.cnblogs.com/OoycyoO/p/11725724.html
Copyright © 2011-2022 走看看