zoukankan      html  css  js  c++  java
  • leetcode------Plus One

    标题: Valid Parentheses
    通过率: 27.7%
    难度: 简单

    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.

    本题是传统的符号匹配题用一个栈来模拟就行了。遇见(,{,[全部进栈,遇到),},]进行弹栈比较,如果不是() [] {}则返回错误,直接看代码:

     1 public class Solution {
     2     public boolean isValid(String s) {
     3         int len=s.length();
     4         Stack<Character> stack = new Stack<Character>();  
     5         for(int i=0;i<len;i++){
     6             char tmp=s.charAt(i);
     7             if(tmp=='('||tmp=='{'||tmp=='['){
     8                 stack.push(tmp);
     9             }
    10             if(tmp==')'||tmp=='}'||tmp==']'){
    11                 if(stack.empty())return false;
    12                 char ctr=stack.pop();
    13                 if(tmp==')'&&ctr=='(') continue;
    14                  if(tmp==']'&&ctr=='[') continue;
    15                  if(tmp=='}'&&ctr=='{') continue;
    16                  else return false;
    17             }
    18         }
    19         if(stack.empty())return true;
    20         else{
    21             return false;
    22         }
    23     }
    24 }
  • 相关阅读:
    pandas 的pd.cut()数据分箱
    pandas 的groupby()
    pandas 的DataFrame.apply()
    天池二手车_特征工程
    numpy简单的笔记
    python 面向对象编程的@property
    mybatis 复杂sql语句
    mybatis Lombok
    mybatis 获取 sqlSession
    mybatis @Param 注解
  • 原文地址:https://www.cnblogs.com/pkuYang/p/4231595.html
Copyright © 2011-2022 走看看