Skip to content

Commit 9e7efbf

Browse files
add problem 20 and test function
1 parent a87dbb2 commit 9e7efbf

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

C/1-50/20-Valid-Parentheses.c

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
3+
*
4+
* The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not.
5+
* Created by supercoderx on 2017/8/9.
6+
*/
7+
8+
#include <stdio.h>
9+
#include "../main.h"
10+
11+
bool isValid(char* s) {
12+
char* tmp=s;
13+
int len = 0, index = -1;
14+
15+
while (*tmp!='\0'){
16+
len++;
17+
tmp++;
18+
}
19+
20+
char indicator[len] ;
21+
for(int i = 0; i < len;i++){
22+
switch (s[i]){
23+
case '{':
24+
indicator[++index]='{';
25+
break;
26+
case '}':
27+
if(index==-1 || indicator[index]!='{'){
28+
return false;
29+
}else{
30+
index--;
31+
}
32+
break;
33+
case '[':
34+
indicator[++index]='[';
35+
break;
36+
case ']':
37+
if(index == -1 || indicator[index]!='['){
38+
return false;
39+
} else{
40+
index--;
41+
}
42+
break;
43+
case '(':
44+
indicator[++index] = '(';
45+
break;
46+
case ')':
47+
if(index == -1 || indicator[index]!='('){
48+
return false;
49+
} else{
50+
index--;
51+
}
52+
break;
53+
default:
54+
break;
55+
}
56+
}
57+
return index == -1?true:false;
58+
}
59+
60+
void testIsValid(){
61+
char s[] = "[(](";
62+
printf("%d",isValid(s));
63+
}

0 commit comments

Comments
 (0)