有效的括号 (Valid Parentheses)

 

思路:

// @Title: 有效的括号 (Valid Parentheses)
// @Author: qisiii
// @Date: 2024-09-14 11:58:53
// @Runtime: 1 ms
// @Memory: 40.5 MB
// @comment: 
// @flag: 
class Solution {
    public boolean isValid(String s) {
        if(s.length()%2!=0){
            return false;
        }
        Stack<Character> stack=new Stack();
        char[] arr=s.toCharArray();
        for(char c:arr){
            if(c=='('||c=='['||c=='{'){
                stack.push(c);
            }else{
                if(stack.isEmpty()){
                    return false;
                }
                if((c==')'&&stack.peek()!='(')
                ||(c==']'&&stack.peek()!='[')
                ||(c=='}'&&stack.peek()!='{')
                ){
                    break;
                }
                stack.pop();
            }
        }
        return stack.isEmpty();
    }
}

+++ title = “有效的括号 (Valid Parentheses)” draft = false +++

思路:

// @Title: 有效的括号 (Valid Parentheses)
// @Author: qisiii
// @Date: 2024-04-12 23:52:35
// @Runtime: 2 ms
// @Memory: 40.5 MB
// @comment: 
// @flag: 
class Solution {
    public boolean isValid(String s) {
        if(s.length()<=0){
            return true;
        }
        if(s.length()%2!=0){
            return false;
        }
        char[] chars = s.toCharArray();
        Stack<Character> stack = new Stack();
        for (Character c : chars) {
            if (!stack.isEmpty()) {
                Character top = stack.peek();
                if (top == '(' && c == ')' ||
                        top == '[' && c == ']' ||
                        top == '{' && c == '}') {
                    stack.pop();continue;
                }
            }
            stack.push(c);
        }
        return stack.isEmpty();
    }
}

思路:

// @Title: 有效的括号 (Valid Parentheses)
// @Author: qisiii
// @Date: 2024-04-12 23:52:11
// @Runtime: 2 ms
// @Memory: 40.5 MB
// @comment: 
// @flag: 
class Solution {
    public boolean isValid(String s) {
        if(s.length()%2!=0){
            return false;
        }
        char[] chars = s.toCharArray();
        Stack<Character> stack = new Stack();
        for (Character c : chars) {
            if (!stack.isEmpty()) {
                Character top = stack.peek();
                if (top == '(' && c == ')' ||
                        top == '[' && c == ']' ||
                        top == '{' && c == '}') {
                    stack.pop();continue;
                }
            }
            stack.push(c);
        }
        return stack.isEmpty();
    }
}

思路:

// @Title: 有效的括号 (Valid Parentheses)
// @Author: qisiii
// @Date: 2024-04-12 23:51:44
// @Runtime: 2 ms
// @Memory: 40.6 MB
// @comment: 
// @flag: 
class Solution {
    public boolean isValid(String s) {
        char[] chars = s.toCharArray();
        Stack<Character> stack = new Stack();
        for (Character c : chars) {
            if (!stack.isEmpty()) {
                Character top = stack.peek();
                if (top == '(' && c == ')' ||
                        top == '[' && c == ']' ||
                        top == '{' && c == '}') {
                    stack.pop();continue;
                }
            }
            stack.push(c);
        }
        return stack.isEmpty();
    }
}

+++ title = “有效的括号 (Valid Parentheses)” draft = false +++

思路:

// @Title: 有效的括号 (Valid Parentheses)
// @Author: qisiii
// @Date: 2024-09-14 11:58:15
// @Runtime: 2 ms
// @Memory: 40.6 MB
// @comment: 
// @flag: 
class Solution {
    public boolean isValid(String s) {
        Stack<Character> stack=new Stack();
        char[] arr=s.toCharArray();
        for(char c:arr){
            if(c=='('||c=='['||c=='{'){
                stack.push(c);
            }else{
                if(stack.isEmpty()){
                    return false;
                }
                if((c==')'&&stack.peek()!='(')
                ||(c==']'&&stack.peek()!='[')
                ||(c=='}'&&stack.peek()!='{')
                ){
                    break;
                }
                stack.pop();
            }
        }
        return stack.isEmpty();
    }
}
Licensed under CC BY-NC-SA 4.0
最后更新于 2024-10-18