自学内容网 自学内容网

力扣疑问题:最小栈

#define MAX_SIZE 10001

typedef struct {
    int* data;
    int top;
    int min;
} MinStack;

/** initialize your data structure here. */

MinStack* minStackCreate() {
    MinStack* obj = (MinStack*)malloc(sizeof(MinStack));
    obj->data = (int*)malloc(sizeof(int) * 10001);
    obj->top = -1;
    obj->min =  INT_MAX;
    return obj;
}

void minStackPush(MinStack* obj, int x) {
    if (obj->top >= MAX_SIZE) {
        return;
    }
    obj->data[++(obj->top)] = x;
    if (x < obj->min) {
        obj->min = x;
    }
}

void minStackPop(MinStack* obj) {
    if (obj->top < 0) {
        return;
    }
    obj->top--;
    if (obj->top >= 0) {
        obj->min = obj->data[obj->top];
    } else {
        obj->min = INT_MAX;
    }
    for (int i = obj->top; i >= 0; i--) {
        if (obj->data[i] < obj->min) {
            obj->min = obj->data[i];
        }
    }
}

int minStackTop(MinStack* obj) {
    return obj->data[obj->top];
}

int minStackGetMin(MinStack* obj) {
    return obj->min;
}

void minStackFree(MinStack* obj) {
    free(obj);
}

将INT_MAX换成其他值就会出问题,很奇怪,有没有大佬帮忙解决


原文地址:https://blog.csdn.net/whcwhc111111/article/details/136375948

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!