题目
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
push(x) -- Push element x onto stack.
pop() -- Removes the element on top of the stack.
top() -- Get the top element.
getMin() -- Retrieve the minimum element in the stack.
思路
对于栈我们都比较熟悉,特别是对于面向对象的语言C++、java都存在栈,因此选择了用C来实现。
#define MAXSIZE 1000
int g_index=0;
int g_totalLen=0;
int g_minIndex=-1;
int g_minValue=INT_MAX;
typedef struct {
int val;
} MinStack;
void minStackCreate(MinStack *stack, int maxSize) {
if(maxSize<1){
return;
}
stack=(MinStack *)malloc(maxSize*sizeof(MinStack));
if(stack==NULL){
exit(EXIT_FAILURE);
}
g_index=0;
g_totalLen=maxSize;
}
void minStackPush(MinStack *stack, int element) {
if(stack==NULL){
return;
}
if(element<g_minValue){//更新最小值的位置
g_minIndex=g_index;
g_minValue=element;
}
if(g_index<g_totalLen){
stack[g_index].val=element;
g_index++;
}
else{//开辟一个2* maxSize的栈
MinStack *newstack=NULL;
int len=g_totalLen;
minStackCreate(newstack, g_totalLen*2);
//将stack中的元素拷贝到newStack
int i=0;
for(;i<len;i++){
newstack[i]=stack[i];
}
stack=newstack;
stack[len].val=element;
g_index=len+1;
}
}
void minStackPop(MinStack *stack) {
if(stack==NULL||g_index<=0){
return;
}
else{
g_index--;
//如果将最小值弹出去了,则重新找到最小值的位置
if(g_minIndex==g_index){
g_minValue=INT_MAX;
for(int i=0;i<g_index;i++){
if(stack[i].val<g_minValue){
g_minValue=stack[i].val;
g_minIndex=i;
}
}
}
}
}
int minStackTop(MinStack *stack) {
if(stack==NULL||g_index<=0){
return -1;
}
else{
return stack[g_index-1].val;
}
}
int minStackGetMin(MinStack *stack) {
if(stack==NULL||g_index<=0){//栈中是没有元素的
return 0;
}
return g_minValue;
}
void minStackDestroy(MinStack *stack) {
if(stack==NULL){
return;
}
free(stack);
stack==NULL;
}
不能AC,目前不知道问题出现在哪。
java实现(不使用栈)
class MinStack {
public Node head;
public void push(int x) {
if(head==null){
head=new Node(x,x,null);
}
else{
head=new Node(x,min(x,head.min),head);
}
}
private int min(int a, int b) {
if(a<=b){
return a;
}
return b;
}
public void pop() {
head=head.next;
}
public int top() {
return head.val;
}
public int getMin() {
return head.min;
}
class Node{
public int val;
public int min;
public Node next;
public Node(int val, int min, Node next) {
super();
this.val = val;
this.min = min;
this.next = next;
}
}
}