题解 | #有效括号序列#
有效括号序列
https://www.nowcoder.com/practice/37548e94a270412c8b9fb85643c8ccc2
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param s string字符串 * @return bool布尔型 */ #include <string.h> char piPei(char param){ if(param == ')'){ return '('; } if(param == ']'){ return '['; } if(param == '}'){ return '{'; } return 0; } bool isValid(char* s ) { // write code here int l = strlen(s); if(l % 2 != 0){ return 0; } char *stack = (char*)malloc(l*sizeof(char)); int top = 0; for(int i = 0; i < strlen(s); i++){ if(s[i] == '(' || s[i] == '[' || s[i] == '{'){ stack[top++] = s[i]; } if(s[i] == ')' || s[i] == ']' || s[i] == '}'){ if(top<1) return 0; if(stack[--top] == piPei(s[i])){ continue; }else{ return 0; } } } if(top != 0){ return 0; }else{ return 1; } }