1
Cのカッコのn-parisのすべての有効な組み合わせをプリントアウトしたいと思います。メインでは3を与えます。これは3桁の有効な括弧のすべての組み合わせを出力したいのですカッコと3つの右括弧。しかし、私はセグメンテーションフォールトを得て、gdbは_printValidParentheses(str, leftCount--, rightCount, count++);
行に出力します。私は誰がなぜ失敗を知っているのだろうと思っていますか?ありがとう。括弧をつけてセグメンテーションフォールトを得る
void printString(char * str) {
while (*str) {
printf("%c", *str++);
}
printf("\n");
}
void _printValidParentheses(char str[], int leftCount, int rightCount, int count) {
if (leftCount < 0 || rightCount < 0) {
return;
}
if (leftCount == 0 && rightCount == 0) {
printString(str);
return;
} else {
if (leftCount > 0) {
str[count] = '(';
_printValidParentheses(str, leftCount--, rightCount, count++);
}
if (rightCount > leftCount) {
str[count] = ')';
_printValidParentheses(str, leftCount, rightCount--, count++);
}
}
}
void printValidParentheses(int n) {
char *str = malloc(sizeof(char) * n * 2);
_printValidParentheses(str, n, n, 0);
}
int main() {
printValidParentheses(3);
return 1;
}
うわー、これを指摘してくれてありがとう。だから私は '_printValidParentheses(str、leftCount-1、rightCount、count + 1);に変更し、完全に動作します。ありがとう。 –