Leetcode updated to 155, this easy question acceptance but not high, I am curious to open it.
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
- Push (x)-push element x onto stack.
- Pop ()--Removes the element on top of the stack.
- Top ()--Get the top element.
- Getmin ()--Retrieve The minimum element in the stack.
Really easy, そして:
classMinstack {structNode {intvalue; Node*Next; Node (intV, node * n=0): Value (v), Next (n) {}}; Node*h; Node*min; Public: Minstack (): H (0), Min (0){} voidPushintx) {h=Newnode (x, h); if(!min | | min->value >=x) Min=Newnode (x, Min); } voidpop () {if(h) {node*p; if(H->value = = min->value) {P=min; Min= min->Next; Delete p; } P=h; H= h->Next; Delete p; } } intTop () {if(h)returnH->value; Throw; } intgetmin () {if(min)returnMin->value; Throw; }};
It is said that the general purpose of the STL container is very worried, multi-threaded gods and horses have taken into account, static links will never call the function added in, so I am accustomed to manually implement the container. XX What's going On: Memory Limited error! n the next pointer incredibly not resistant and too proud!
Then use the array instead of the chained structure. Try it with a vector.
classMinstack {vector<int>h; Vector<int>min; Public: voidPushintx) {h.push_back (x); if(Min.empty () | | min.back () >=x) min.push_back (x); } voidpop () {if(H.back () = =min.back ()) Min.pop_back (); H.pop_back (); } intTop () {returnH.back (); } intgetmin () {returnMin.back (); }};
XX, again mle! Finally saw others use deque, the procedure is similar, copied over. Fortunately, the function names in STL are uniform, vector and deque are called Push_back and pop_back, change the definition is good:
classMinstack {deque<int>h; Deque<int>min; Public: voidPushintx) {h.push_back (x); if(Min.empty () | | min.back () >=x) min.push_back (x); } voidpop () {if(H.back () = =min.back ()) Min.pop_back (); H.pop_back (); } intTop () {returnH.back (); } intgetmin () {returnMin.back (); }};
Looked up the information, and gained knowledge! Deque using arrays and lists of this strategy, initially allocating a piece of contiguous memory, not enough to allocate a piece, and then these allocated memory through the pointer "All together", logically, is a continuous space. This kind of thing has long wanted to write a, did not expect STL already provided, and STL in the stack and queue and so on under the container is it! perfect!
Now compare the following arrays, vectors, and deque:
1: Principle:
Array: continuous and fixed space;
Vector: continuous but not fixed space, the original space is not enough will call allocator to ReAlloc, and then copy the old content through Uninitialized_copy.
Deque: A continuous chain of space, space is not enough to allocate new space, each memory block is chained (not new it?) From the previous study of data structure, there is this implementation of string, learning operating system, where file management is also useful chain method.
2: Direction of growth:
Array: Not very long
Vector: Logically, it's forwardincremental.
Deque: It's obviously two-way.
3: Performance:
Test it with this simple code, because I'm using the ThinkPad X61,n=100000000,sizeof (x, five) last time.
= 12B is almost the limit.
When testing with int, output 4 6;
When using a struct test, the deque loop output 7,vector is bad_alloc directly during the insertion process because there is not enough contiguous memory to be found.
#include <cstdlib>#include<ctime>#include<deque>#include<vector>#include<iostream>using namespacestd;structs{intv; inta[2]; S (inti): V (i) {}~s () {//Make it a non-trivial destructor;}};main () {typedef s T; int ConstN =100000000; inti; Doublestart; Deque<T>D; Vector<T>v; Start= Time (0); for(i=0; i<n; i++) D.push_back (T (i)); for(i=0; i<n; i++) D.pop_back (); cout<<time (0)-start<<Endl; Start= Time (0); for(i=0; i<n; i++) V.push_back (T (i)); for(i=0; i<n; i++) V.pop_back (); cout<<time (0)-start<<Endl;}
Of course not to abandon the vector later, if the memory address of each access to jump, deque efficiency must not be, because to jump to another block, it++ of course not, at least the need to access two times of memory. It is justified to exist.
Distinguish between deque vector and array