LeetCode Vaild Parentheses Stack (golang)

problem
在这里插入图片描述
basics

Stack
A special data structure in a linear table in which data can only be inserted or deleted from a fixed end and the other end is blocked
The main characteristic of Stack is First In Last Out
Stack overflows when full, and underflows when empty
Overflow: When the stack is already full of data elements, the stack storage will error if it continues to deposit data into the stack.
Uderflow: When the stack is empty, the stack will continue to fetch data

在这里插入图片描述
Two kinds of algorithm
PUSH and POP
RC


```bash
Status InitStack(SqStack &S){
    S.base = (SElemType *)malloc(STACK_INIT_SIZE * sizeof(SElemType))
    if(!S.base) exit(OVERFLOW)
    S.top = S.base;
    S.stacksize = STACK_INIT_SIZE;
    return OK;
}


Status Push(SqStack &S, SElemType &e){
    if(S.top - S.base >= S.stacksize){  //栈满,追加存储空间
        S.base = (SElemType *)realloc(S.base, (S.stacksize + STACKINCREMENT) * sizeof(SElemType));
        if(!S.base) exit(OVERFLOW); //存储分配失败
        S.top = S.base + S.stacksize;
        S.stacksize += STACKINCREMENT;
    }
    * S.top++ = e;
    return OK;
}




Status Pop(SqStack &S, SElemType &e){
    if(S.top == S.base) return ERROR;
    e = * --S.top;
    return OK;
}

Analysis Process
1.Initialize stack S
2.Process each bracket of the expression once
3.If an open bracket is encountered, we simply push it onto the stack
4.If we encounter a close bracket, then we check the top of the stack if the top of the stack is an open bracket of the same type, then we pop it off the stack and continue processing ,otherwise, the expression is invalid
5.If we still have elements in the stack at the end, that means the expression is invalid

Code

func isValid(s string) bool {
	if s == "" {
		return true
	}                      // Empty strings are valid by default
	if len(s)%2 == 1 {
		return false
	}                     // An odd number of parentheses will not match
	keyMap := map[string]string{
		"}": "{",
		"]": "[",
		")": "(",
	}                     //Establish correspondence
	var stack []string
	for i := 0; i < len(s); i++ {
		if len(stack) > 0 {
			tmp, ok := keyMap[string(s[i])]       // The stack is not empty
		 		if ok {
				top := stack[len(stack)-1]      // The current element is the right half bracket
	// If the mapping to the element currently traversed is the same as the bracket at the top of the stack
				if top == tmp {
					// pop
					stack = stack[:len(table)-1]
					// skip this loop
					continue
				}
			} 
		}
		// Empty stack or no match, add stack
		stack = append(stack, string(s[i]))
	}
	// If the last stack is empty, it all matches, and returns true
	return len(stack) == 0
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值