题解 | #牛群的秘密通信#
牛群的秘密通信
https://www.nowcoder.com/practice/f0047999594d4cd39f85d7347c6941af
package main
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param s string字符串
* @return bool布尔型
*/
func is_valid_cow_communication( s string ) bool {
// write code here
if len(s)==0 {
return true
}
if (len(s)/2)==1{
return false
}
str :=[]byte(s)
stack :=&stack{}
Map :=map[byte]byte{
')':'(',
']':'[',
'}':'{',
}
for _,value :=range str{
switch value{
case '{','(','[':
stack.push(value)
case '}',')',']':
byte :=stack.top()
if byte==' '{
return false
}
if byte!=Map[value]{
return false
}
stack.pop()
}
}
if stack.top()==' '{
return true
}
return false
}
type stack struct{
strStack []byte
}
func (s *stack) push(str byte){
s.strStack=append(s.strStack, str)
}
func (s *stack) pop(){
if len(s.strStack)==0{
return
}
s.strStack=s.strStack[:len(s.strStack)-1]
return
}
func (s *stack) top() byte{
if len(s.strStack)==0{
return ' '
}
return s.strStack[len(s.strStack)-1]
}
