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

推荐订阅源

Martin Fowler
Martin Fowler
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
T
Troy Hunt's Blog
Latest news
Latest news
Vercel News
Vercel News
S
SegmentFault 最新的问题
V
Vulnerabilities – Threatpost
博客园 - Franky
P
Privacy International News Feed
A
Arctic Wolf
T
The Blog of Author Tim Ferriss
S
Schneier on Security
T
The Exploit Database - CXSecurity.com
P
Palo Alto Networks Blog
T
Tor Project blog
Jina AI
Jina AI
GbyAI
GbyAI
The Hacker News
The Hacker News
博客园 - 叶小钗
Google DeepMind News
Google DeepMind News
T
Threatpost
C
CERT Recently Published Vulnerability Notes
P
Proofpoint News Feed
Scott Helme
Scott Helme
WordPress大学
WordPress大学
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Microsoft Azure Blog
Microsoft Azure Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
博客园 - 司徒正美
A
About on SuperTechFans
Recorded Future
Recorded Future
爱范儿
爱范儿
L
LangChain Blog
V
V2EX
C
Cybersecurity and Infrastructure Security Agency CISA
The Cloudflare Blog
阮一峰的网络日志
阮一峰的网络日志
K
Kaspersky official blog
Webroot Blog
Webroot Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
D
DataBreaches.Net
宝玉的分享
宝玉的分享
Google Online Security Blog
Google Online Security Blog
C
Cisco Blogs
L
Lohrmann on Cybersecurity
Help Net Security
Help Net Security
AWS News Blog
AWS News Blog
M
MIT News - Artificial intelligence
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
G
GRAHAM CLULEY

博客园 - 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)的时间内完成以上三种操作。一种空间换时间的方法。