20. Valid parentheses (LeetCode)

Idea: Use the stack'slast in, first out characteristics to complete the requirements of the question 

Because C++ has a library that can be used directly, but the C language does not, we directly copy the written stack and use it.  

First, complete the construction of the framework 

Secondly, implement the part within the loop. 1. Push the left bracket onto the stack 2. Match the right bracket when popped out of the stack 

Here is the judgment ofright bracket matching, please pay attentionDon't write both sides as equal. This does not mean that all matches are successful, so write as unequal on both sides. If satisfied, return false directly. If not satisfied, continue. Loop 

Each time the loop ends, s++. After all loops stop, if there is no return false, then return true 

Seems like there's nothing wrong with it, right?​ 

Actually, the above only applies to the scenario where the number of left and right brackets is equal. We also need to consider two A special case:

1. There are more left brackets than right brackets

2. There are more right brackets than left brackets

When there are more left brackets than right brackets, the loop ends and the number of elements in the stack is not 0. Use STEmpty to judge. If it is empty, it is the same as before and returns true. , if not empty, returns false

When there are more right brackets than left brackets, inside the loop, until the stack is empty and there are still right brackets to match, then false will be returned directly at this time 

The complete code is as follows:

typedef char STDataType;
typedef struct Stack
{
	STDataType* a;
	int top;
	int capacity;
}ST;

//初始化
void STInit(ST* pst);
//销毁
void STDestroy(ST* pst);
//压栈
void STPush(ST* pst, STDataType x);
//出栈
void STPop(ST* pst);
//获取栈顶元素
STDataType STTop(ST* pst);
//检测栈是否为空
bool STEmpty(ST* pst);
//检测栈中有效元素个数
int STSize(ST* pst);

void STInit(ST* pst)
{
	assert(pst);
	
	pst->a = NULL;
	pst->top = 0;//top指向栈顶元素的下一个位置
	pst->capacity = 0;
}

void STDestroy(ST* pst)
{
	assert(pst);
	
	free(pst->a);
	pst->top = pst->capacity = 0;
}

void STPush(ST* pst, STDataType x)
{
	assert(pst);

	if (pst->top == pst->capacity)
	{
		int newCapacity = pst->capacity == 0 ? 4 : pst->capacity * 2;
		STDataType* tmp = (STDataType*)realloc(pst->a, newCapacity * sizeof(STDataType));

		if (tmp == NULL)
		{
			perror("realloc fail");
			return;
		}

		pst->a = tmp;
		pst->capacity = newCapacity;
	}

	pst->a[pst->top++] = x;
}

void STPop(ST* pst)
{
	assert(pst);
	assert(!STEmpty(pst));
	
	pst->top--;
}

STDataType STTop(ST* pst)
{
	assert(pst);
	assert(!STEmpty(pst));
	
	return pst->a[pst->top - 1];
}

bool STEmpty(ST* pst)
{
	assert(pst);

	return pst->top == 0;
}

int STSize(ST* pst)
{
	assert(pst);

	return pst->top;
}

bool isValid(char* s)
{
    ST st;
    STInit(&st);

    while (*s)
    {
        //1.左括号入栈
        //2.右括号出栈匹配
        if (*s == '('
        ||*s == '['
        ||*s == '{')
        {
            STPush(&st, *s);
        }
        else
        {
            //解决右括号多于左括号的问题
            if (STEmpty(&st))
            {
                STDestroy(&st);
                return false;
            }

            char top = STTop(&st);
            STPop(&st);

            if ((top != '(' && *s == ')')
            ||(top != '[' && *s == ']')
            ||(top != '{' && *s == '}'))
            {
                STDestroy(&st);
                return false;
            }
        }

        s++;
    }

    //解决左括号多于右括号的问题
    bool ret = STEmpty(&st);
    STDestroy(&st);
    return ret;
}

Guess you like

Origin blog.csdn.net/2301_79188764/article/details/134363213