代码拉取完成,页面将自动刷新
#define Max_size 10000
//x_stack存储正常入栈元素,min_stack用来存储入栈时栈中最小的元素
//x_top和min_top分别为栈顶的下标
//min_value用来存储最小值
typedef struct {
int *x_stack;
int *min_stack;
int x_top;
int min_top;
int min_value;
} MinStack;
/** initialize your data structure here. */
MinStack* minStackCreate() {
MinStack *obj = (MinStack*)malloc(sizeof(MinStack));
obj->x_stack = (int*)malloc(sizeof(int) * Max_size);
obj->min_stack = (int*)malloc(sizeof(int) * Max_size);
obj->min_top = obj->x_top = -1;
obj->min_value = 0;
return obj;
}
void minStackPush(MinStack* obj, int x) {
if(obj->x_top < Max_size)
{
if(obj->x_top == -1)
obj->min_value = x;
obj->x_stack[++(obj->x_top)] = x;
if(x < obj->min_value)
obj->min_value = x; //当前元素比min_value小时改变min_value
obj->min_stack[++(obj->min_top)] = obj->min_value;
}
}
void minStackPop(MinStack* obj) {
--(obj->x_top);
--(obj->min_top);
if(obj->min_top != -1)
obj->min_value = obj->min_stack[obj->min_top];//出栈后将min_value变成min_stack的栈顶元素
}
int minStackTop(MinStack* obj) {
return obj->x_stack[obj->x_top];
}
int minStackMin(MinStack* obj) {
return obj->min_stack[obj->min_top];
}
void minStackFree(MinStack* obj) {
free(obj->x_stack);
free(obj->min_stack);
free(obj);
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。