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 }
  • 相关阅读:
    纯JavaScripst的全选、全不选、反选 【转】
    Java 文件和byte数组转换
    nc命令使用详解
    mtr 命令详解
    Nginx主动检测方案---Tengine
    Apache相关安全设置
    tomcat APR的配置
    Vsftpd 配置详解
    FTP主动模式和被动模式的区别
    iptables配置详解
  • 原文地址:https://www.cnblogs.com/pkuYang/p/4231595.html
Copyright © 2011-2022 走看看