LeetCode20. Valid parentheses - pure C

"Looking and looking forlorn and lonely, desolate and miserable"
"Three cups and two glasses of light wine, how can he be in a hurry at night?"

This problem is a bracket matching problem, which is a typical application of stacks.

How to create one 顺序栈has been implemented in the previous blog post: Portal .

Topic description

Given a string s containing only '(', ')', '{', '}', '[', ']', determine whether the string is valid.

A valid string must satisfy:

  1. Opening parentheses must be closed with closing parentheses of the same type.
  2. Left parentheses must be closed in the correct order.

insert image description here

topic link

LeetCode20. Valid parentheses. Simple

topic analysis

The bracket matching problem, a typical application to the stack of data structures.
At my current level of beginner data structure, although LeetCode shows that it is a simple problem, it is not easy for me, and the knowledge reserve is not enough. It is very cumbersome to solve it with pure C.
My idea is to first create a stack with a sequential structure, and then use the basic functions of the stack such as: push the stack (StackPush), pop the stack (StackPop), get the elements on the top of the stack (StackTop) and other functions to complete this problem.

Ideas:
1. Traverse the entire string "'(',')','{','}','[',']'".
2. In the process of traversing, if you encounter an opening bracket '(', or '{', or '[', perform a stack push operation (StackPush).
3. If you encounter a closing bracket ')', or '}', Or ']', the operation of accessing the value of the top element of the stack (StackTop) is performed, and if the left parenthesis at the top of the stack matches one of the closing parentheses, the stack popping operation (StackPop) is performed.
4. Until the string is out of bounds the loop ends.

Code

Note: In the process of implementation, you need to pay attention to extreme cases.
Normal situations can be imagined by normal people. And extreme cases are not necessarily.
And the example of the title is not given.
1. What if there is only one left parenthesis ‘(’?
//Hint: When there is only one left parenthesis, the stack is not empty.
2. What if there is only one closing parenthesis ‘)’?
//Hint: The stack is empty when there is only one closing parenthesis.

/***********
*结构体创建
***********/
typedef char STDataType;

typedef struct Stack
{
    
    
	STDataType* a;
	int top;
	int capacity;
}ST;
/***********
*函数的声明
***********/

//初始化结构体
void StackInit(ST* ps);
//销毁结构体
void StackDestroy(ST* ps);
//压栈
void StackPush(ST* ps, STDataType x);
//出栈
void StackPop(ST* ps);
//得到栈顶的值
STDataType StackTop(ST* ps);
//判断栈是否为空
bool StackEmpty(ST* ps);
//得到栈的长度
int StackSize(ST* ps);

/***********
*函数的定义
***********/

//初始化结构体
void StackInit(ST* ps)
{
    
    
	assert(ps);
	ps->a = NULL;
	ps->capacity = 0;
	ps->top = 0;
}
//销毁结构体
void StackDestroy(ST* ps)
{
    
    
	assert(ps);
	free(ps->a);
	ps->a = NULL;
	ps->capacity = 0;
	ps->top = 0;


}
//压栈
void StackPush(ST* ps, STDataType x)
{
    
    

	assert(ps);
	if (ps->top == ps->capacity)
	{
    
    
		int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		STDataType* new = (STDataType*)realloc(ps->a, sizeof(STDataType) * newcapacity);
		if (new == NULL)
		{
    
    
			printf("realloc fail\n");
			exit(-1);
		}
		ps->a = new;
		ps->capacity = newcapacity;
	}
	ps->a[ps->top] = x;
	ps->top++;
}
//出栈
void StackPop(ST* ps)
{
    
    
	assert(ps);
	assert(ps->top > 0);
	ps->top--;
}
//访问栈顶元素
STDataType StackTop(ST* ps)
{
    
    
	assert(ps);
	assert(ps->top > 0);
	return ps->a[ps->top - 1];
}
//判断栈是否为空
bool StackEmpty(ST* ps)
{
    
    
	assert(ps);
	return ps->top == 0;
}
//得到栈的长度
int StackSize(ST* ps)
{
    
    
	assert(ps);
	return ps->top;
}

/***********
*题目函数接口
***********/
bool isValid(char* s)
{
    
    
	ST st;
	//创建栈,否则后续压栈等操作无法实现
	StackInit(&st);
	while (*s)
	{
    
    

		if (*s == '(' || *s == '{' || *s == '[')
		{
    
    
			StackPush(&st, *s);
			s++;
		}
		else
		{
    
    
		//只有一个右括号时,栈为空。直接return false
			if (StackEmpty(&st))
			{
    
    
				return false;
			}
			char top = StackTop(&st);
			if (*s == ')' && top == '(' || *s == '}' && top == '{' || *s == ']' && top == '[')
			{
    
    
				StackPop(&st);
				s++;
			}
			else
			{
    
    
				return false;
			}
		}
	}
	//处理只有一个左括号
	
	if (!StackEmpty(&st))
	{
    
    
		return false;
	}
	return true;
	//销毁栈,拒绝内存泄漏
	StackDestroy(&st);
}

Guess you like

Origin blog.csdn.net/qq2466200050/article/details/123784510