【leetcode】20. 有效的括号

有效的括号 题目链接
在这里插入图片描述

// 栈结构
typedef char valuetype;
typedef struct {
   
	valuetype* arr;
	int top;
	int capacity;
} Stack;

void Init(Stack* stack);

void Push(Stack* stack, valuetype value);
void Pop(Stack* stack);

valuetype Top(Stack* stack);
int Size(Stack* stack);
bool Empty(Stack* stack);

void Destroy(Stack* stack);


bool isValid(char* s) {
   
	Stack stack;
	Init(&stack);

	while (*s) {
   
		if (*s == '(' || *s == '[' || *s == '{') {
   
			Push(&stack, *s); // 1.左括号入栈;
		}
		else {
    // 2.比较。
			if (Empty(&stack)) {
    // 2.1右括号比左括号多
				Destroy(&stack);
				return false;
			}
			else {
    // 2.2 比较左括号与右括号是否匹配
				char ch = Top(&stack);
				Pop(&stack);
				if ((ch == '(' && *s != ')') ||
					(ch == '[' && *s != ']') ||
					(ch == '{' && *s != '}')) {
   
					Destroy(&stack);
					return false;
				}
			}
		}
		s++;
	}
	// 如果栈里还剩有括号,说明左右括号没有对应
	bool res = Empty(&stack);
	Destroy(&stack);
	return res;
}


void Init(Stack* stack) {
   
	assert(stack);
	stack->arr = NULL;
	stack->capacity = stack->top = 0;
}

void Push(Stack* stack, valuetype value) {
   
	assert(stack);
	if (stack->top == stack->capacity) {
   
		stack->capacity = stack->capacity == 0 ? 10 : (int)(stack->capacity * 1.5);
		stack->arr = (valuetype*)realloc(stack->arr, sizeof(valuetype) * stack->capacity);
		if (stack->arr == NULL) {
   
			perror("realloc failed in the function Push(Stack*, valuetype).");
			return;
		}
	}
	stack->arr[stack->top++] = value;
}

void Pop(Stack* stack) {
   
	assert(stack && stack->top > 0);
	stack->top--;
}

valuetype Top(Stack* stack) {
   
	assert(stack && stack->top > 0);
	return stack->arr[stack->top - 1];
}

int Size(Stack* stack) {
   
	assert(stack);
	return stack->top;
}

bool Empty(Stack* stack) {
   
	assert(stack);
	return stack->top == 0;
}

void Destroy(Stack* stack) {
   
	assert(stack);
	free(stack->arr);
	stack->arr = NULL;
	stack->capacity = stack->top = 0;
}

相关推荐

  1. LeetCode 20. 有效括号

    2024-02-02 09:54:03       36 阅读
  2. Leetcode 20. 有效括号

    2024-02-02 09:54:03       18 阅读
  3. LeetCode 20.有效括号

    2024-02-02 09:54:03       12 阅读
  4. Leetcode 20有效括号

    2024-02-02 09:54:03       13 阅读
  5. LeetCode20题 - 有效括号

    2024-02-02 09:54:03       40 阅读
  6. LeetCode_20_简单_有效括号

    2024-02-02 09:54:03       29 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-02-02 09:54:03       14 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-02-02 09:54:03       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-02-02 09:54:03       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-02-02 09:54:03       18 阅读

热门阅读

  1. 使用docker部署Kafka(MAC Apple M2 Pro)

    2024-02-02 09:54:03       29 阅读
  2. npm出现 Error: EISDIR: illegal operation on a directory, read

    2024-02-02 09:54:03       30 阅读
  3. mac上,配置bundletool,将aab转为apk

    2024-02-02 09:54:03       33 阅读
  4. Codeforces Round 481 (Div. 3)

    2024-02-02 09:54:03       37 阅读
  5. k8s集群master和node添加

    2024-02-02 09:54:03       26 阅读
  6. SQL中Limit的用法详解

    2024-02-02 09:54:03       28 阅读