惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Recent Commits to openclaw:main
Recent Commits to openclaw:main
Cisco Talos Blog
Cisco Talos Blog
S
SegmentFault 最新的问题
大猫的无限游戏
大猫的无限游戏
Project Zero
Project Zero
博客园 - 叶小钗
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
L
Lohrmann on Cybersecurity
C
CXSECURITY Database RSS Feed - CXSecurity.com
V
Vulnerabilities – Threatpost
NISL@THU
NISL@THU
罗磊的独立博客
Hugging Face - Blog
Hugging Face - Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
P
Palo Alto Networks Blog
博客园_首页
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Last Week in AI
Last Week in AI
量子位
Security Latest
Security Latest
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
小众软件
小众软件
博客园 - 【当耐特】
T
Threat Research - Cisco Blogs
The Cloudflare Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
腾讯CDC
宝玉的分享
宝玉的分享
V2EX - 技术
V2EX - 技术
P
Privacy International News Feed
Security Archives - TechRepublic
Security Archives - TechRepublic
Webroot Blog
Webroot Blog
Apple Machine Learning Research
Apple Machine Learning Research
Jina AI
Jina AI
美团技术团队
Hacker News: Ask HN
Hacker News: Ask HN
人人都是产品经理
人人都是产品经理
S
Securelist
S
Security Affairs
WordPress大学
WordPress大学
J
Java Code Geeks
月光博客
月光博客
N
News and Events Feed by Topic
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
阮一峰的网络日志
阮一峰的网络日志
C
CERT Recently Published Vulnerability Notes
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
T
Tailwind CSS Blog

博客园 - Zero Lee

调用栈(call stack) 关于STL allocator Calculate maximum sum of any subarray set Calcuate power n of x recursively Convert one binary search tree to double-linked list 类模板的模板友元函数定义 一道百度的面试题解答 非printf形式的十六进制和二进制打印(雅虎面试题) 一道腾讯面试题 (转)C++中extern “C”含义深层探索 selection algorithm to select nth small elements based on partition 删除与某个字符相邻且相同的字符 产生全排列的方法解析 一组数的全排列和组合程序实现 求一个正整数的平方根程序实现 [转]多线程队列的算法优化 [转载] STL allocator的介绍和一个基于malloc/free的allocator的简单实现 如何将一片内存链接成链表 One simple counted object pointer
设计包含min函数的栈
Zero Lee · 2012-06-17 · via 博客园 - Zero Lee

定义栈的数据结构,要求添加一个min的函数,能够得到栈的最下元素。
要求函数min, push, pop的时间复杂度都是O(1)

 1 class stackmin {
 2     int data[N];
 3     int minidx[N];
 4     int top;
 5 public:
 6     stackmin()
 7         : top(-1)
 8     {
 9         ::memset(minidx, -1, sizeof(int)*N);
10         ::memset(data, 0, sizeof(int)*N);
11     }
12 
13     bool push(int d)
14     {
15         if (top < N-1)
16             data[++top] = d;
17         else
18             return false;
19         if (top==0)
20             minidx[top] = 0;
21         else if (data[minidx[top-1]] > d)
22             minidx[top] = top;
23         else
24             minidx[top] = minidx[top-1];
25         return true;
26     }
27     bool pop(int& d)
28     {
29         if (top<0)
30             return false;
31         else
32 
33             d = data[top];
34         minidx[top] = -1;
35         top--;
36         return true;
37     }
38     bool min(int& m)
39     {
40         if (top>=0)
41             m = data[minidx[top]];
42         else
43             return false;
44         return true;
45     }
46     void print()
47     {
48         for (int i = 0; i <= top; i++)
49             std::cout << data[i] << " ";
50         std::cout << "\n";
51     }
52 };
53 

借助另一个内部的数组,用来存储每次push元素之后的栈最小值的索引,便可很方便的在O(1)的时间内完成以上三种操作。一种空间换时间的方法。