Determine a mathematical expression in the parentheses matches

2010-08-19  来源:本站原创  分类:Java  人气:152 

Use stack, making it fairly simple for this operation.

package com.java.util;

import java.util.Stack;

public class MyStack {
        public static boolean check(String input){
                Stack<Character> stack = new Stack<Character>();
                for(int i=0;i<input.length();i++){
                        char ch = input.charAt(i);
                        switch(ch){
                                case '{':       case '[':       case '(':
                                        stack.push(ch);
                                        break;
                                case '}':       case ']':       case ')':
                                        if(!stack.isEmpty()){//[]](){}
                                                char chx = stack.pop();
                                                if(ch == '}' && chx != '{'
                                                        || (ch == ']' && chx != '[')
                                                        || (ch == ')' && chx != '(')){
                                                        return false;
                                                }
                                        }else{//[](){})
                                                return false;
                                        }
                                        break;
                                default:
                                        break;
                        }
                }
                if(!stack.isEmpty()){//[](){}(
                        return false;
                }

                return true;
        }
        public static void main(String[] args) {
                String input = "[wer]()asdf{adsf}";
                System.out.println(MyStack.check(input));
        }

}
相关文章
  • Determine a mathematical expression in the parentheses matches 2010-08-19

    Use stack, making it fairly simple for this operation. package com.java.util; import java.util.Stack; public class MyStack { public static boolean check(String input){ Stack<Character> stack = new Stack<Character>(); for(int i=0;i<input.len

  • Step-by-step learning Ruby (7): mathematical expression 2009-09-23

    We know that in the bottom of the computer is completely based on the data, we have prepared procedures for operation data. Expression is understood to be the number of computers, operator of the joint variables. For example, add, subtract, multiply,

  • js regular expression syntax 2010-09-24

    Regular expression syntax A regular expression is the general character (such as characters a to z) and special characters (called metacharacters) composed of text mode. The model describes the main characters in the search to be matched when one or

  • Regular Expression Reference 2010-11-08

    / / Check whether all the numbers function isDigit (s) { var patrn = / ^ [0-9] {1,20} $ /; if (! patrn.exec (s)) return false return true } / / Check login name: Enter only 5-20 with a letter, can be a string with digital ,"_","." Java

  • JS regular expression (reproduced) under good learning 2011-04-29

    / / Check whether all the numbers function isDigit (s) { var patrn = / ^ [0-9] {1,20} $ /; if (! patrn.exec (s)) return false return true } / / Check login name: Enter only 5-20 with a letter, can be a string with digital ,"_","." func

  • Regular expression from the foundation to learn the 2010-03-21

    [Edit this paragraph] 1, what is the regular expression a regular expression is a pattern to match with a string of a formula for a class. Many more people because they look so strange and complex not to use - unfortunately, this article will not be

  • (Transfer) java regular expression Xiangjie 2010-04-12

    Java regular expression Xiangjie If you have used Perl or any other built-in support for regular expression language, you must know to use a regular expression pattern matching and text, and how easy it is. If you are not familiar with this term, the

  • Java regular expression Xiangjie 2010-07-10

    Java regular expression Xiangjie On 2005-10-08 10:01: Cactus Studio Source: KissJava.com Editor: Wang Yuhan, if you have used Perl or any other built-in support for regular expression language, you must know to use regular expressions to process text

  • [Reprint] regular expression 2010-07-27

    Reprint address http://www.javaeye.com/topic/719950 Regular Expressions / / Check if all the numbers function isDigit (s) ( var patrn = / ^ [0-9] (1,20) $ /; if (! patrn.exec (s)) return false return true ) / / Check login name: Enter only 5-20 month

  • js in the regular expression Xiangjie 2010-07-28

    Recognizing regular expressions not used if the original regular expression, then the terms and concepts may be unfamiliar. However, they do not you think it strange. Please try to recall how to find the hard disk file. You will certainly use? And *

  • Regular Expression Object 2010-08-16

    This object contains the regular expression patterns and show how to apply the model sign. Syntax 1 re = / pattern / [flags] Syntax 2 re = new RegExp ("pattern", ["flags"]) Parameter re Required. Will be assigned to a regular expressio

  • Java regular expression Xiangjie (Reprinted) 2010-08-23

    First, the basics of regular expressions We start with a simple start. Suppose you want to search for a containing characters "cat" string search with regular expression is "cat". If the search is not case sensitive, the word "cat

  • [Change] Java regular expression summary 2010-09-24

    Study: Wind in the bamboo Time: 2010-09-21 Source: http://www.cnblogs.com/fzzl/archive/2010/09/21/1832794.html Google search with "Java regular expression" is not very easy to find especially good on the java regular expression summary of the ar

  • Detailed Java regular expression 2010-10-09

    If you've ever used Perl or any other built-in support for regular expression language, you must know to use a regular expression pattern matching to process text and how easy it is. If you are not familiar with this term, then the "regular expressio

  • javascript regular expression [Z] 2010-10-16

    JS regular expressions / / Check whether all the numbers function isDigit (s) { var patrn = / ^ [0-9] {1,20} $ /; if (! patrn.exec (s)) return false return true } / / Check login name: Enter only 5-20 with a letter, can be a string with digital ,"_&q

  • JS regular expression (to) 2010-10-23

    JS Regular expression // Check whether all the numbers function isDigit(s) { var patrn=/^[0-9]{1,20}$/; if (!patrn.exec(s)) return false return true } // Check login : Enter only 5-20 with a letter . A string can be with digital ."_".".&quo

  • VS2005 Replace regular expression search 2010-04-28

    Regular expression search and replace text mode is concise and flexible representation. Visual Studio, use the regular expression is a Visual C + + 6.0 for use with a simplified syntax a superset of the expression. In the "Find", "find in f

  • Perl regular expression (Regular Expression, regex) 2010-05-05

    If you want to ask me the advantages of Perl language, I would say three and a half. The first three are regular expressions, hash, reference, that half a Map, the General Assembly because they did not use, do not say it's second office draw, so half

  • oracle10g regular expression 2010-06-09

    The basic examples of regular expressions Before using this new feature, you need to understand the meaning of metacharacters. Full stop (.) Matching a regular expression of any character (except newline). For example, the regular expression matching

  • Regular expression operations 2010-10-24

    This switched: http://docs.python.org/library/re.html # re-syntax This module provides regular expression matching operations similar to those found in Perl. Both patterns and strings to be searched can be Unicode strings as well as 8-bit strings. Re