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

推荐订阅源

GbyAI
GbyAI
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
C
Check Point Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
J
Java Code Geeks
博客园 - 【当耐特】
H
Hacker News: Front Page
S
Secure Thoughts
博客园_首页
Engineering at Meta
Engineering at Meta
N
News | PayPal Newsroom
美团技术团队
SecWiki News
SecWiki News
U
Unit 42
The Hacker News
The Hacker News
有赞技术团队
有赞技术团队
T
The Exploit Database - CXSecurity.com
M
MIT News - Artificial intelligence
T
Threat Research - Cisco Blogs
V
Vulnerabilities – Threatpost
TaoSecurity Blog
TaoSecurity Blog
The Last Watchdog
The Last Watchdog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
F
Fortinet All Blogs
T
Tor Project blog
T
Tailwind CSS Blog
Scott Helme
Scott Helme
Recorded Future
Recorded Future
Know Your Adversary
Know Your Adversary
The Register - Security
The Register - Security
Google DeepMind News
Google DeepMind News
Microsoft Security Blog
Microsoft Security Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Google DeepMind News
Google DeepMind News
S
Security @ Cisco Blogs
S
SegmentFault 最新的问题
Apple Machine Learning Research
Apple Machine Learning Research
月光博客
月光博客
阮一峰的网络日志
阮一峰的网络日志
H
Heimdal Security Blog
MongoDB | Blog
MongoDB | Blog
S
Securelist
C
CXSECURITY Database RSS Feed - CXSecurity.com
雷峰网
雷峰网
博客园 - 聂微东
S
Schneier on Security
T
Tenable Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
B
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)的时间内完成以上三种操作。一种空间换时间的方法。