【leetcode】包含min函数的栈

 

#define MAX_INT 2147483647

int min (int a, int b) {
    return a < b? a: b;
}

typedef struct {
    int min[20001];
    int stack[20001];
    int top;
} MinStack;

/** initialize your data structure here. */

MinStack* minStackCreate() {
    MinStack *obj = (MinStack *) malloc (sizeof (MinStack));
    obj->top = 0;
    obj->stack[0] = obj->min[0] = MAX_INT;
    return obj;
}

void minStackPush(MinStack* obj, int x) {
    obj->stack[++obj->top] = x;
    obj->min[obj->top] = min (obj->min[obj->top - 1], x);
}

void minStackPop(MinStack* obj) {
    --obj->top;
}

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

int minStackMin(MinStack* obj) {
    return obj->min[obj->top];
}

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

 

posted @ 2020-08-22 10:51  温暖了寂寞  阅读(106)  评论(0编辑  收藏  举报