1 Star 0 Fork 0

唐梓迅/leetcode题解

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
剑指offer30 1.48 KB
一键复制 编辑 原始数据 按行查看 历史
#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);
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/Tang-CMer/leetcode-problem-solving.git
git@gitee.com:Tang-CMer/leetcode-problem-solving.git
Tang-CMer
leetcode-problem-solving
leetcode题解
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385