Skip to content

Instantly share code, notes, and snippets.

@cshijiel
Created July 8, 2017 09:13
Show Gist options
  • Save cshijiel/e1f582df8a55e03a93614b88123f7617 to your computer and use it in GitHub Desktop.
Save cshijiel/e1f582df8a55e03a93614b88123f7617 to your computer and use it in GitHub Desktop.
判定所给的栈操作序列是否合法
#include <stdio.h>
#define TRUE 1
#define FALSE 0
int checkStackOption(char str[], int length) {
int leftSize = 0;
for (int i = 0; i < length; ++i) {
if (str[i] == 'I') {
leftSize++;
} else if (str[i] == 'O') {
leftSize--;
} else {
return FALSE;
}
if (leftSize < 0) {
return FALSE;
}
}
if (leftSize == 0) {
return TRUE;
} else {
return FALSE;
}
}
int main() {
printf("Hello, World!\n");
// char str[] = "IOIIOIOO";
// char str[] = "IOOIOIIO";
// char str[] = "IIIOIOIO";
char str[] = "IIIOOIOO";
int length = sizeof(str) / sizeof(str[0]) - 1;
int isLegal = checkStackOption(str, length);
printf("%d", isLegal);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment