在利用push建栈的过程中,边 “建栈” 边 "利用数组保留当前压栈后的最大的那个元素,即只需要比较数组中的前一个元素即可,若比前一个元素大,则保留该元素到数组中,否则保留数组中的前一个元素相同的值。"
则当前栈中的最大的元素即使该数组中的最后一个元素。时间复杂度为O(1)
解决: g++ 编译环境
[cpp] view plaincopy
/*
* main.cpp
*
* Created on: 2009-6-18
* Author: NeeSky
*/
#define MAXSIZE 100
#include
using namespace std;
/**
* The definition of Stack
*/
struct Stack
{
int size;
int data[MAXSIZE];
};
/**
* The Global Variables
*/
Stack stackProblem;
int arrayAssistant[MAXSIZE];
/**
* Push the element to stackProblem ,
* and record the element max to arrayAssistant
* */
inline void push(int element )