我在我的 C 代码中收到此错误以获取有效的括号字符串

问题描述 投票:0回答:1

我是学生,我被分配用 C 做 leetcode 问题 1021。 我必须使用堆栈来解决这个问题,所以我在 C 中创建了一个。 堆栈是一个整数堆栈,但它的唯一目的是跟踪 tempString 是否已成为有效的括号字符串。

我得到的错误是:enter image description here

#define STACKSIZE 100


typedef struct{
    int top;
    int item[STACKSIZE];
} stack;

int isEmpty(stack* x)
{
    if(x->top==-1)
    {
        return 1;
    }
    return 0;
}

int isFull(stack* x)
{
    if(x->top==STACKSIZE-1)
    {
        return 1;
    }
    return 0;
}


int pop(stack *x)
{
    if(isEmpty(x))
    {
        printf("UNDERFLOW");
        exit(1);
    }
    return x->item[x->top--];
}


void push(stack* x, int y)
{
    if(isFull(x))
    {
        printf("OVERFLOW");
        exit(1);
    }
    x->item[++x->top] = y;
}


int peek(stack* x)
{
    if(isEmpty(x))
    {
        printf("There are no elements to peek");
        exit(1);
    }
    return x->item[x->top];
}

void removeChar(char* x, int y)
{
    int i=y;
//  printf("First Character: %c\n",x[0]);
    while(x[i]!='\0')
    {
        x[i] = x[i+1];
        i++;
    }
}

void clearString(char *c)
{
    c[0] = '\0';
}





char * removeOuterParentheses(char * s){
    stack x;
    char tempString[] = "";
    // char* finalString = (char*)(malloc(sizeof(char)*100)); //TO be returned
    char finalString[] = "";
    int startIndex=0;
    int endIndex=0;
    int count=0;
    while(count!=strlen(s))
    {
        if(isEmpty(&x) && strlen(tempString)!=0)
        {
            //This implies that the tempString is a primitive parenthesis string and we must add it to the final string after removing the outer parenthesis
            removeChar(tempString,0);
            removeChar(tempString,strlen(tempString)-1);
            strcat(finalString,tempString);
            clearString(tempString);


        }
        if(s[count]=='(')
        {
            push(&x,1);
            count++;
            strcat(tempString,"(");
        }
        else if(s[count]==')')
        {
            pop(&x);
            count++;
            strcat(tempString,")");
        }
    }

    return finalString;


}

请告诉我我的逻辑或我的代码或我的实现有什么错误。 我在第 46 行收到错误,即在 push() 函数中,在语句 x->item[++x->top] = y;

请请请帮助我。很难找到与C相关的答案。

LeetCode 问题 数字是 1021.

c algorithm stack
1个回答
0
投票
  1. pop()
    x->top--
    意味着
    x->(top--)
    但你的意思是
    (x->top)--
    .

  2. push()
    :类似于上面的
    ++x->top
    应该是
    ++(x->top)
    .

  3. removeOuterParentheses()
    :您需要初始化
    stack x
    。你可以只做
    stack x = { .top = -1 };
    但我写了一个
    init()
    来为你做它与其他堆栈功能一致。

  4. removeOuterParentheses()
    tempString
    finalString
    被初始化为大小 0。
    finalString
    需要大致为初始字符串的大小。

  5. removeOuterParentheses()
    return finalString;
    指向局部变量的指针,该变量在函数返回时超出范围。

  6. 无论如何你都在使用

    stack x
    ,你也可以在附加下一个数据块时存储你需要的位置。这允许您消除一些辅助功能和
    tempString
    .

  7. 更喜欢

    for()
    循环进行迭代。

  8. 添加了带有测试驱动程序的

    main()
    。测试数据是从你提到的问题中得到的https://leetcode.com/problems/remove-outermost-parentheses/.

  9. isEmpty()
    isFull()
    可以简化为只返回表达式的结果。

#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define STACKSIZE 100
typedef struct {
    int top;
    int item[STACKSIZE];
} stack;

int isEmpty(stack* x) {
    return x->top == -1;
}

int isFull(stack* x) {
    return x->top == STACKSIZE - 1;
}


void init(stack *x) {
    x->top = -1;
};

int pop(stack *x) {
    if(isEmpty(x)) {
        printf("UNDERFLOW");
        exit(1);
    }
    return x->item[(x->top)--];
}

void push(stack* x, int y) {
    if(isFull(x)) {
        printf("OVERFLOW");
        exit(1);
    }
    x->item[++(x->top)] = y;
}

int peek(stack* x) {
    if(isEmpty(x)) {
        printf("There are no elements to peek");
        exit(1);
    }
    return x->item[x->top];
}

char *removeOuterParentheses(char *s) {
    char *finalString = malloc(strlen(s) + 1);
    if(!finalString)
        return NULL;
    *finalString = '\0';
    size_t i = 0;
    stack x;
    init(&x);
    for(; s[i]; i++) {
        if(s[i]=='(')
            push(&x, i);
        else if(s[i]==')') {
            int pos = pop(&x);
            if(isEmpty(&x))
                strncat(finalString, s + pos + 1, i - pos - 1);
        }
    }
    finalString[i] = '\0';
    return finalString;
}

int main(void) {
    struct test {
        char *input;
        char *output;
    } tests[] = {
        {"(()())(())", "()()()"},
        {"(()())(())(()(()))", "()()()()(())"},
        {"()()", ""}
    };
    for(struct test *t = tests; t < tests + sizeof(tests) / sizeof(*tests); t++) {
        char *got = removeOuterParentheses(t->input);
        if(strcmp(t->output, got))
            printf("not ok\n# Expected: \"%s\"\n# Received: \"%s\"\n", t->output, got);
        else
            printf("ok\n");
        free(got);
    }
}

和示例会话:

ok
ok
ok
© www.soinside.com 2019 - 2024. All rights reserved.