-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_validparentheses.c
63 lines (44 loc) · 1.38 KB
/
_validparentheses.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// https://leetcode.com/problems/valid-parentheses/description/
#include <stdlib.h>
typedef struct stack {
size_t SP;
char* data;
size_t cap;
} stack;
// NEW(stack*, size_t), allocate a stack of given size.
#define NEW(s, n) ((s = (stack*)malloc(sizeof(stack))),\
(s -> cap = n), \
(s -> SP = 0), \
(s -> data = (char*)calloc(n, sizeof(char))))
// CLEAR(stack*), reset the stack pointer.
#define CLEAR(s) (s -> SP = 0)
// ISEMPTY(stack*), check if the stack pointer is 0.
#define ISEMPTY(s) (((s -> SP) == 0))
// PUSH(stack*, size_t), put a given value on the top of the stack
#define PUSH(s, v) (s -> data[(s -> SP)++] = (char)v)
// POP(s), return the value on top of the stack, or 0 if the stack is empty.
#define POP(s) (ISEMPTY(s) ? 0 : s -> data[--(s -> SP)])
// ISFULL(s), check if the stack is full
#define ISFULL(s) (((s -> SP) == (s -> cap)))
char par_map[] = {
['('] = ')',
['['] = ']',
['{'] = '}',
};
bool isValid(char* s) {
stack* STACK;
NEW(STACK, strlen(s));
while(*s) {
switch(*s) {
case '(':
case '[':
case '{':
PUSH(STACK, *s); break;
default:
if(par_map[POP(STACK)] != *s)
return false;
}
s++;
}
return ISEMPTY(STACK);
}