zoukankan      html  css  js  c++  java
  • lintcode12 带最小值操作的栈

    实现一个带有取最小值min方法的栈,min方法将返回当前栈中的最小值。

    你实现的栈将支持pushpop 和 min 操作,所有操作要求都在O(1)时间内完成。

    建一个栈helpStack,用来存放从开始到目前位置的最小值,

     1 /**
     2  * lintcode12:带最小值操作的栈
     3  * 狗剩的美丽家园
     4  * 2017年12月4日15:33:23
     5  */
     6 
     7 class MinStack {
     8 public:
     9     stack<int> mainStack;
    10     stack<int> helpStack;
    11     MinStack() {
    12         // do initialization if necessary
    13     }
    14 
    15     void push(int number) {
    16         // write your code here
    17         int top;
    18         mainStack.push(number);
    19 
    20         if (!helpStack.empty()) {
    21             top = helpStack.top();
    22         } else {
    23             top = number;
    24         }
    25 
    26         if (number > top) {
    27             helpStack.push(top);
    28         } else {
    29             helpStack.push(number);
    30         }
    31     }
    32 
    33     int pop() {
    34         // write your code here
    35         if (!mainStack.empty()) {
    36             int data = mainStack.top();
    37             mainStack.pop();
    38             helpStack.pop();
    39             return data;
    40         }
    41     }
    42 
    43     int min() {
    44         // write your code here
    45         if (!helpStack.empty()) {
    46             int data = helpStack.top();
    47             return data;
    48         }
    49     }
    50 };
  • 相关阅读:
    LSMW TIPS
    Schedule agreement and Delfor
    Running VL10 in the background 13 Oct
    analyse idoc by creation date
    New Journey Prepare
    EDI error
    CBSN NEWS
    Listen and Write 18th Feb 2019
    Microsoft iSCSI Software Target 快照管理
    通过 Microsoft iSCSI Software Target 提供存储服务
  • 原文地址:https://www.cnblogs.com/gousheng/p/7977492.html
Copyright © 2011-2022 走看看