My solution:using Stack<Integer> instead of Stack<Character>


  • 0
    S

    public class Solution {
    public boolean isValid(String s) {
    Stack<Integer> st = new Stack<Integer>();
    for(int i=0;i<s.length();i++){
    int val = s.charAt(i)-40;
    if(val==0){st.push(1);}
    else if(val==51){st.push(53);}
    else if(val==83){st.push(85);}
    else if(st.isEmpty()||val!=st.peek()){return false;}
    else{st.pop();}
    }
    return st.isEmpty();
    }
    }


Log in to reply
 

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