题解 | #牛群的秘密通信#
牛群的秘密通信
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<>();
for(int i = 0;i < s.length();i++){
switch (s.charAt(i)){
case ')':
if(st.isEmpty() || st.peek() != '('){
st.add(')');
}else{
st.pop();
}
break;
case '}':
if(st.isEmpty() || st.peek() != '{'){
st.add('}');
}else{
st.pop();
}
break;
case ']':
if(st.isEmpty() || st.peek() != '['){
st.add(']');
}else{
st.pop();
}
break;
default:
st.add(s.charAt(i));
}
}
return st.isEmpty();
}
}
快手成长空间 767人发布