Created
July 8, 2017 09:13
-
-
Save cshijiel/e1f582df8a55e03a93614b88123f7617 to your computer and use it in GitHub Desktop.
判定所给的栈操作序列是否合法
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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