zoukankan      html  css  js  c++  java
  • 20.Valid Parentheses

    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.

    solution:

    楼主曾经很天真的:

    public class Valid_Parentheses {
       public static boolean vaildparenthess(String s) {
           boolean flag=true;
           if(s.charAt(0)-')'==0 ||s.charAt(0)-']'==0||s.charAt(0)-'}'==0){
               return false;
           }else{
               for (int i = 1; i < s.length(); i++) {
                if(s.charAt(i)-'('==0){
                    if(s.charAt(i+1)-')'!=0)
                        flag=false;
                }
                if(s.charAt(i)-'['==0){
                    if(s.charAt(i+1)-']'!=0)
                        flag=false;
                }
                if(s.charAt(i)-'{'==0){
                    if(s.charAt(i+1)-'}'!=0)
                        flag=false;
                }
                if(s.charAt(i)-')'==0){
                    if(s.charAt(i-1)-'('!=0)
                        flag=false;
                }
                if(s.charAt(i)-']'==0){
                    if(s.charAt(i-1)-'['!=0)
                        flag=false;
                }
                if(s.charAt(i)-'}'==0){
                    if(s.charAt(i+1)-'{'!=0)
                        flag=false;
                }
            }
           return flag;
        }
        
    }
        public static void main(String[] args) {
            // TODO Auto-generated method stub
         String s="[]";
         System.out.println(vaildparenthess(s));
        }
    
    }

    但是,楼主这个愚蠢的人类!!!(≧∇≦)

    明明用stack可以很方便的解决!!first in last out!!

    package leetcode2;
    
    import java.util.Stack;
    
    public class valid_parenthese {
        public static boolean vaildparenthess(String s) {
           boolean flag=true;
           Stack<Character> st=new Stack<Character>();
           if(s==null){
               return false;
           }
           for(int i=0;i<s.length();i++){
               if(s.charAt(i)=='('||s.charAt(i)=='{'||s.charAt(i)=='['){
                   st.push(s.charAt(i));
               }else if(s.charAt(i)==')'){
                   if(st.pop()!='(')
                       flag=false;
               }else if(s.charAt(i)==']'){
                   if(st.pop()!='[')
                       flag=false;
               }else if(s.charAt(i)=='}'){
                   if(st.pop()!='{')
                       flag=false;
               }
              
           }
           if(!st.isEmpty()){
               flag=false;
           }
           return flag;
        }
        public static void main(String[] args) {
            // TODO Auto-generated method stub
        String string="{()()}";
        System.out.println(vaildparenthess(string));
        }
    
    }
  • 相关阅读:
    重构之重新组织函数(ExTract Method)
    设计模式之桥接模式
    设计模式原则之里氏替换原则
    设计模式原则之依赖倒置原则
    设计模式原则之接口隔离原则
    设计模式原则之单一职责原则
    编译php5.6
    wireshark总结
    Blu-Ray BRRip 和 BDRip 的区别
    openwrt虚拟机的network unreachable
  • 原文地址:https://www.cnblogs.com/joannacode/p/4389655.html
Copyright © 2011-2022 走看看