标签:false update main span val minstack color fine can
#include<stdio.h> #include<stdlib.h> #define STACK_SIZE 500000 class Stack { public: Stack() :m_index(0) { //m_data[STACK_SIZE] = { 0 }; //m_minData[STACK_SIZE] = { 0 }; m_data = new int[STACK_SIZE]; m_minData = new int[STACK_SIZE]; // memset(m_data,0,STACK_SIZE); // memset(m_minData,0,STACK_SIZE); }; ~Stack() { if (0 != m_data) delete m_data; if (0 != m_minData) delete m_minData; }; bool Push(int val); bool Pop(int& val); bool GetMin(int& val); private: int *m_data; int *m_minData; int m_index; }; bool Stack::Push(int val) { if (m_index >= STACK_SIZE - 1) return false; m_data[m_index] = val; //update minStack if (0 == m_index) { m_minData[m_index] = val; m_index++; return true; } if (m_minData[m_index - 1] > val) { m_minData[m_index] = val; } else { m_minData[m_index]=m_minData[m_index-1]; } m_index++; return true; } bool Stack::Pop(int& val) { if (m_index - 1 < 0) return false; val = m_data[--m_index]; return true; } bool Stack::GetMin(int& val) { if (m_index - 1 < 0) return false; val = m_minData[m_index - 1]; return true; } int main(void) { int num = 0; Stack obj; scanf("%d", &num); for ( int i = 0;i < num;i++) { int op = 0; int val = 0; int tmp = 0; scanf("%d", &op); switch (op) { case 0: if (obj.GetMin(tmp)) printf("%d\n", tmp); break; case 1: scanf("%d", &val); obj.Push(val); break; case 2: if (obj.Pop(tmp)) printf("%d\n", tmp); break; default: break; } } return 0; }
标签:false update main span val minstack color fine can
原文地址:https://www.cnblogs.com/hitzzq/p/13371504.html