Skip to content

Commit 042fe66

Browse files
Fix
Signed-off-by: begeekmyfriend <begeekmyfriend@gmail.com>
1 parent f429ac0 commit 042fe66

File tree

1 file changed

+5
-9
lines changed

1 file changed

+5
-9
lines changed

020_valid_parentheses/valid_parentheses.c

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,24 @@
44

55
static bool isValid(char *s)
66
{
7-
int n = 0, cap = 100;
8-
char *stack = malloc(cap);
7+
int n = 0;
8+
char stack[100];
99

1010
while (*s != '\0') {
1111
switch(*s) {
1212
case '(':
1313
case '[':
1414
case '{':
15-
if (n + 1 >= cap) {
16-
cap *= 2;
17-
stack = realloc(stack, cap);
18-
}
1915
stack[n++] = *s;
2016
break;
2117
case ')':
22-
if (stack[--n] != '(') return false;
18+
if (n == 0 || stack[--n] != '(') return false;
2319
break;
2420
case ']':
25-
if (stack[--n] != '[') return false;
21+
if (n == 0 || stack[--n] != '[') return false;
2622
break;
2723
case '}':
28-
if (stack[--n] != '{') return false;
24+
if (n == 0 || stack[--n] != '{') return false;
2925
break;
3026
default:
3127
return false;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy