zoukankan      html  css  js  c++  java
  • 括号匹配

    import java.util.Scanner;
    import java.util.Stack;
    
    public class Test15 {
    
        public static void main(String[] args){
            Scanner sc = new Scanner(System.in);
            while(sc.hasNext()){  
                String str = sc.nextLine();
                Stack<Character> s = new Stack<Character>();
                if(str.length() % 2 != 0){  
                    System.out.println(str+" -> "+"FALSE");  
                }else{ 
                    boolean flag = true;
                    for (int i = 0; i < str.length(); i++) {
                        char ch = str.charAt(i);
                        if(ch=='('||ch=='['||ch=='{'){
                            switch(ch){
                            case '(': s.push(')');break;
                            case '[': s.push(']');break;
                            case '{': s.push('}');break;
                            }
                        }
                        else if(ch==')'||ch==']'||ch=='}'){
                            if(s.isEmpty()||ch!=s.peek()){
                                flag = false;
                                System.out.println(str+" -> "+"FALSE");
                                break;
                            }else{
                                s.pop();
                            }
                        }
                    }
                    if(s.isEmpty())
                        System.out.println(str+" -> "+"TRUE");
                    else if(flag)
                        System.out.println(str+" -> "+"FALSE");
                }
                
          }
        }
            
    }
    
    /*
     ()
     [()]
     ()()
     ({}([]))
     )
     ({
     {)(}
     
     
     */
    Jumping from failure to failure with undiminished enthusiasm is the big secret to success.
  • 相关阅读:
    2015上阅读计划
    《梦断代码》读书笔记 第2篇
    四则运算3
    求数组中最大子数组的和(一维)
    四则运算2—单元测试
    四则运算2
    《梦断代码》读书笔记 第1篇
    四组运算2(思路)
    四则运算1
    读书笔记
  • 原文地址:https://www.cnblogs.com/chongerlishan/p/5948705.html
Copyright © 2011-2022 走看看