LeetCode_最小栈

2023/11/29 LeetCode

题目详情请查看:https://leetcode-cn.com/leetbook/read/top-interview-questions/xa7r55/ (opens new window)

常规解法:

var MinStack = function () {
  this.min = undefined;
  this.stack = [];
};

/**
 * @param {number} val
 * @return {void}
 */
MinStack.prototype.push = function (val) {
  this.stack.push(val);

  if (typeof this.min === 'undefined') {
    this.min = val;
  }
  if (val < this.min) {
    this.min = val;
  }
};

/**
 * @return {void}
 */
MinStack.prototype.pop = function () {
  let el = this.stack.pop();
  if (el === this.min) {
    this.min = this._getMin();
  }
};

/**
 * @return {number}
 */
MinStack.prototype.top = function () {
  return this.stack[this.stack.length - 1];
};

/**
 * @return {number}
 */
MinStack.prototype._getMin = function () {
  if (this.stack.length > 0) {
    let min = this.stack[0];
    for (let num of this.stack) {
      if (num < min) {
        min = num;
      }
    }
    return min;
  }
};

MinStack.prototype.getMin = function () {
  return this.min;
};
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

利用现有的数据来发现其可以利用的规律,最小栈方法,是可以利用一个同步栈来标记最小值,是因为结合了实际的栈操作和题目要求而采取的一种方法。代码如下:

var MinStack = function () {
  this.stack2 = [];
  this.stack = [];
};

/**
 * @param {number} val
 * @return {void}
 */
MinStack.prototype.push = function (val) {
  this.stack.push(val);
  if (this.stack2.length && this.stack2[this.stack2.length - 1] <= val) {
    this.stack2.push(this.stack2[this.stack2.length - 1]);
  } else {
    this.stack2.push(val);
  }
};

/**
 * @return {void}
 */
MinStack.prototype.pop = function () {
  this.stack.pop();
  this.stack2.pop();
};

/**
 * @return {number}
 */
MinStack.prototype.top = function () {
  return this.stack[this.stack.length - 1];
};

MinStack.prototype.getMin = function () {
  return this.stack2[this.stack2.length-1];
};
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
更新时间: 2023/11/29 15:42:16