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 }
  • 相关阅读:
    jQuery扩展extend一
    json对象的操作,json工具
    typeof操作符的返回值
    jacksonall的使用,解析json
    jQuery的扩展
    An Introduction to Computer Thinking
    lazy初始化和线程安全的单例模式
    Compiler Principle
    Lineare Algebra
    Mathematik
  • 原文地址:https://www.cnblogs.com/pkuYang/p/4231595.html
Copyright © 2011-2022 走看看