https://leetcode.cn/problems/valid-parentheses/solutions/2771702/20-you-xiao-de-gua-hao-by-joker-ek4-zsmo
C可能的写法,不用STL
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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
| #include <stdio.h> #include <stdlib.h>
#define MaxSize 10000
typedef char ElemType; typedef struct { ElemType data[MaxSize]; int top; } SqStack; void InitStack(SqStack &S); bool StackEmpty(SqStack S); bool Push(SqStack &S, ElemType x); bool Pop(SqStack &S, ElemType &x); bool GetTop(SqStack S, ElemType &x); bool bracketCheck(char str[], int length);
int main() { char str[] = "[]"; int ret = bracketCheck(str, 2); printf("%d", ret); return 0; }
void InitStack(SqStack &S) { S.top = -1; } bool StackEmpty(SqStack S) { return S.top == -1; } bool Push(SqStack &S, ElemType x) { if (S.top == MaxSize - 1) return false; S.data[++S.top] = x; return true; } bool Pop(SqStack &S, ElemType &x) { if (StackEmpty(S)) return false; x = S.data[S.top--]; return true; } bool GetTop(SqStack S, ElemType &x) { if (StackEmpty(S)) return false; x = S.data[S.top]; return true; } bool bracketCheck(char str[], int length) { SqStack S; InitStack(S); for (int i = 0; i < length; i++) { if (str[i] == '(' || str[i] == '[' || str[i] == '{') Push(S, str[i]); else { if (StackEmpty(S)) return false; char topElem; Pop(S, topElem); if (str[i] == ')' && topElem != '(') return false; if (str[i] == ']' && topElem != '[') return false; if (str[i] == '}' && topElem != '{') return false; } } return StackEmpty(S); }
|
https://leetcode.cn/problems/remove-all-adjacent-duplicates-in-string/solutions/2777120/1047-shan-chu-zi-fu-chuan-zhong-de-suo-y-3yk7
https://leetcode.cn/problems/evaluate-reverse-polish-notation/solutions/2777448/150-ni-bo-lan-biao-da-shi-qiu-zhi-by-jok-bew5