solution.md 3.7 KB
Newer Older
每日一练社区's avatar
每日一练社区 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
# 最小栈
<p>设计一个支持 <code>push</code><code>pop</code><code>top</code> 操作,并能在常数时间内检索到最小元素的栈。</p>

<ul>
	<li><code>push(x)</code> &mdash;&mdash; 将元素 x 推入栈中。</li>
	<li><code>pop()</code>&nbsp;&mdash;&mdash; 删除栈顶的元素。</li>
	<li><code>top()</code>&nbsp;&mdash;&mdash; 获取栈顶元素。</li>
	<li><code>getMin()</code> &mdash;&mdash; 检索栈中的最小元素。</li>
</ul>

<p>&nbsp;</p>

<p><strong>示例:</strong></p>

<pre><strong>输入:</strong>
[&quot;MinStack&quot;,&quot;push&quot;,&quot;push&quot;,&quot;push&quot;,&quot;getMin&quot;,&quot;pop&quot;,&quot;top&quot;,&quot;getMin&quot;]
[[],[-2],[0],[-3],[],[],[],[]]

<strong>输出:</strong>
[null,null,null,null,-3,null,0,-2]

<strong>解释:</strong>
MinStack minStack = new MinStack();
minStack.push(-2);
minStack.push(0);
minStack.push(-3);
minStack.getMin();   --&gt; 返回 -3.
minStack.pop();
minStack.top();      --&gt; 返回 0.
minStack.getMin();   --&gt; 返回 -2.
</pre>

<p>&nbsp;</p>

<p><strong>提示:</strong></p>

<ul>
	<li><code>pop</code><code>top</code><code>getMin</code> 操作总是在 <strong>非空栈</strong> 上调用。</li>
</ul>

<p>以下错误的选项是?</p>
## aop
### before
```cpp
#include <bits/stdc++.h>
using namespace std;
```
### after
```cpp
/**
 * Your MinStack object will be instantiated and called as such:
 * MinStack obj = new MinStack();
 * obj.push(x);
 * obj.pop();
 * int param_3 = obj.top();
 * int param_4 = obj.getMin();
 */
```

## 答案
```cpp
其他三个都是错的
```
## 选项

### A
```cpp
class MinStack
{
public:
    /** initialize your data structure here. */
private:
    stack<long int> s; 
    stack<long int> mins;

public:
    MinStack()
    {
        while (!s.empty())
        {
            s.pop();
        }
        while (!mins.empty())
        {
            mins.pop();
        }
    }
    void push(int x)
    {
        if (s.empty())
        {
            s.push(x);
            mins.push(x);
        }
        else
        {
            if (mins.top() >= x)
            {
                s.push(x);
                mins.push(x);
            }
            else
            {
                mins.push(mins.top());
                s.push(x);
            }
        }
    }
    void pop()
    {
        s.pop();
        mins.pop();
    }

    int top()
    {
        return s.top();
    }
    int getMin()
    {
        return mins.top();
    }
};
```

### B
```cpp
class MinStack
{
public:
    /** initialize your data structure here. */
    stack<int> s;
    MinStack()
    {
    }

    void push(int x)
    {
        if (s.empty())
        {
            s.push(x);
            s.push(x);
        }
        else
        {
            int temp = s.top();
            s.push(x);
            if (x < temp)
            {
                s.push(x);
            }
            else
            {
                s.push(temp);
            }
        }
    }

    void pop()
    {
        s.pop();
        s.pop();
    }

    int top()
    {
        int temp = s.top();
        s.pop();
        int top = s.top();
        s.push(temp);
        return top;
    }

    int getMin()
    {
        return s.top();
    }
};
```

### C
```cpp
class MinStack
{
public:
    stack<int> s;
    stack<int> min;
    /** initialize your data structure here. */
    MinStack()
    {
    }

    void push(int x)
    {
        s.push(x);
        if (min.empty() || x <= min.top())
        {
            min.push(x);
        }
    }

    void pop()
    {
        if (s.top() == min.top())
            min.pop();
        s.pop();
    }

    int top()
    {
        return s.top();
    }
    int getMin()
    {
        return min.top();
    }
};
```