Runtime error with C language.


  • 0
    B

    My code:

    typedef struct {
        int pos_val;
        int pos_min;
        int* val;
        int* min;
    } MinStack;
    
    void minStackCreate(MinStack *stack, int maxSize) {
        if (stack == NULL || maxSize <= 0) return;
        stack->val = (int *)malloc(maxSize * sizeof(int));
        stack->min = (int *)malloc(maxSize * sizeof(int));
        stack->pos_val = -1;
        stack->pos_min = -1;
    }
    
    void minStackPush(MinStack *stack, int element) {
        if (stack == NULL) return;
        stack->val[++stack->pos_val] = element;
        if (stack->pos_min == -1 || element <= stack->min[stack->pos_min]) stack->min[++stack->pos_min] = element;
    }
    
    void minStackPop(MinStack *stack) {
        if (stack == NULL) return;
        if (stack->pos_min != -1) {
            if (stack->val[stack->pos_val] == stack->min[stack->pos_min]) --stack->pos_min;
            --stack->pos_val;
        }
    }
    
    int minStackTop(MinStack *stack) {
        return stack->val[stack->pos_val];
    }
    
    int minStackGetMin(MinStack *stack) {
        return stack->min[stack->pos_min];
    }
    
    void minStackDestroy(MinStack *stack) {
        free(stack->val);
        free(stack->min);
    }
    

    Could anyone help me?


  • 0
    X

    i also have this problem,is that a bug exit?


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.