not use stack for java solution


  • 0
    S
    public class Solution {
        public boolean isValid(String s) {
    		ArrayList<Character> lst=new ArrayList<Character>();
    		if(s.length()%2==1){
    			return false;
    		}
    		for(int i=0;i<s.length();i++){
    			if(s.charAt(i)=='['){
    				lst.add(']');
    			}
    			else if(s.charAt(i)=='('){
    				lst.add(')');
    			}
    			else if(s.charAt(i)=='{'){
    				lst.add('}');
    			}else{
    				if(lst.size()==0 || s.charAt(i)!=lst.get(lst.size()-1)){
    					return false;
    				}else{
    					lst.remove(lst.size()-1);
    				}
    			}
    		}
    		if(lst.size()!=0){
    		    return false;
    		}
    		return true;
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.