题解 | #牛群的秘密通信#
牛群的秘密通信
https://www.nowcoder.com/practice/f0047999594d4cd39f85d7347c6941af
import java.util.*;
public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param s string字符串 
     * @return bool布尔型
     */
    public boolean is_valid_cow_communication (String s) {
        // write code here
        Stack<Character>st=new Stack<Character>();
        for(int i =0;i<s.length();i++){
            if(s.charAt(i)=='('){
                st.push(')');
            }
            else if(s.charAt(i)=='{'){
                st.push('}');
            }
            else if(s.charAt(i)=='['){
                st.push(']');
            }
            else if(st.isEmpty()||st.pop()!=s.charAt(i)){
                return false;
            }
            }
        return st.isEmpty();
        }
    }
查看19道真题和解析
