(微软100题)2.设计包含min 函数的栈。
微软 函数 设计 包含 100 min
2023-09-14 09:06:32 时间
#include <iostream> using namespace std; /*2.设计包含min 函数的栈。 定义栈的数据结构,要求添加一个min 函数,能够得到栈的最小元素。 要求函数min、push 以及pop 的时间复杂度都是O(1)。 ANSWER: Stack is a LIFO data structure. When some element is popped from the stack, the status will recover to the original status as before that element was pushed. So we can recover the minimum element, too. */ struct MinStackElement { int data; int min; }; struct MinStack { MinStackElement * data; int size; int top; }; MinStack MinStackInit(int maxSize) { MinStack stack; stack.size = maxSize; stack.data = (MinStackElement*) malloc(sizeof(MinStackElement)*maxSize); stack.top = 0; return stack; } void MinStackFree(MinStack stack) { free(stack.data); } void MinStackPush(MinStack stack, int d) { if (stack.top == stack.size) cout<<"out of stack space."; MinStackElement* p = &stack.data[stack.top]; p->data = d; p->min = (stack.top==0?d : stack.data[stack.top-1].min); if (p->min > d) p->min = d; stack.top ++; } int MinStackPop(MinStack stack) { if (stack.top == 0) cout<<"stack is empty!"; return stack.data[--stack.top].data; } int MinStackMin(MinStack stack) { if (stack.top == 0) cout<<"stack is empty!"; return stack.data[stack.top-1].min; } void main() { }
相关文章
- 惠普与微软开启双重认证,全面互联 “云”程万里
- XP用户噩耗来袭:微软将于2014年停止其技术支持
- 微软成功收购NVIDIA|微软的野望
- 微软一站式示例代码库
- 微软 Excel 365 里如何设置下拉菜单和自动高亮成指定颜色
- jMeter创建微软Azure Lambda Function报错
- 一起学微软Power BI系列-使用技巧(6) 连接Sqlite数据库
- 微软看上的Rust 语言,安全性真的很可靠吗
- 微软Win11 Dev/Beta预览版22581.1(ni_release)发布
- 微软Win10 KB5011543(19044.1618)补丁发布
- Win11微软输入法打不出汉字怎么办?
- Windows7下的免费虚拟机(微软官方虚拟机)
- 【GPT4】微软 GPT-4 测试报告(6)与人类的交互能力
- [Microsoft] 通过Microsoft Spotlight 中国站云技能挑战获取微软免费考试券
- [ Azure - Cloud Shell ] 微软 Azure Cloud Shell 介绍