2016.6.1——Min Stack

Min Stack

本题收获:

1.可以利用两个栈操作。

2.栈的基本操作。

  题目:

  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.

  Example:

  MinStack minStack = new MinStack();
  minStack.push(-2);
  minStack.push(0);
  minStack.push(-3);
  minStack.getMin();   --> Returns -3.
  minStack.pop();
  minStack.top();      --> Returns 0.
  minStack.getMin();   --> Returns -2.

  思路:

    我的思路:没有思路。

    leetcode/dicuss思路:

      弄2个stack,一个realStack,存放真正的数据;另外一个是minStack。

      对于minStack元素中的每一个元素的意义是:push到 该位置的时候,当前最小元素的值。每次push进新元素的时候,更新minStack的值;每次pop的时候,由于minStack的定义,所以只需把 minStack和realStack一起进行一次pop操作就好了。

      minstack只存储最小元素,每次Push时将输入元素与minstack中的元素对比,如果小于minstack则push,否则不操作。pop时,如果minstack的元素等于realstack中元素则pop,否则不操作。

    代码:

 1 class MinStack {
 2 private:
 3     stack<int> s1;
 4     stack<int> s2;
 5 public:
 6     void push(int x) {
 7         s1.push(x);
 8         if (s2.empty() || x <= getMin())  s2.push(x);       
 9     }
10     void pop() {
11         if (s1.top() == getMin())  s2.pop();
12         s1.pop();
13     }
14     int top() {
15         return s1.top();
16     }
17     int getMin() {
18         return s2.top();
19     }
20 };

  我的带mian函数的测试代码:

 1 // minStack.cpp : 定义控制台应用程序的入口点。
 2 //
 3 
 4 #include "stdafx.h"
 5 #include "iostream"
 6 #include "stack"
 7 using namespace std;
 8 
 9 class MyClass
10 {
11 public:
12     void push(int val);
13     void pop();
14     int minStack();    
15     int top();
16 private:
17     stack<int> realstack;
18     stack<int> minstack;
19 };
20 
21 void MyClass::push(int val)
22 {
23     realstack.push(val);
24     if (minstack.empty() || val <= minstack.top()) minstack.push(val);
25 }
26 
27 void MyClass::pop()
28 {
29     if (realstack.top() == minstack.top())  //注意pop的顺序,先比较在pop,不是先pop在比较
30     {
31         minstack.pop();
32     }
33     realstack.pop();
34 }
35 
36 int MyClass::top()
37 {
38     return realstack.top();
39 }
40 
41 int MyClass::minStack()
42 {
43     return minstack.top();
44 }
45 
46 
47 int _tmain(int argc, _TCHAR* argv[])
48 {
49     MyClass solution;
50     int val = 0;
51     cout << "the value is : ";
52     for (int i = 0; i < 5; i++)
53     {
54         cin >> val;
55         solution.push(val);
56     }
57     cout << "the min is : " << solution.minStack() << endl;
58     for (int i = 0; i < 2; i++)
59     {
60         int value = solution.top();
61         cout << "pop value is : " << value << endl;
62         //cin >> val;
63         solution.pop();
64     }
65     cout << "current min is : " << solution.minStack() << endl;
66     system("pause");
67     return 0;
68 }

  测试结果:

    

参考了:http://www.cnblogs.com/lihaozy/archive/2012/12/09/2809840.html 

      写的非常详细!

 

  代码2:

 1 class MinStack {
 2 public:
 3     vector<int> a;
 4     vector<int> min;
 5     MinStack() {
 6         min.push_back(2147483647);
 7     }
 8     void push(int x) {
 9         a.push_back(x);
10         if (x < min.back()) {
11             min.push_back(x);
12         } else {
13             min.push_back(min.back());
14         }
15     }
16 
17     void pop() {
18         a.pop_back();
19         min.pop_back();
20     }
21 
22     int top() {
23         return a.back();
24     }
25 
26     int getMin() {
27         return min.back();
28     }
29 };

 

posted on 2016-06-01 22:53  zhuzhu2016  阅读(152)  评论(0编辑  收藏  举报

导航