一道笔试题,麻烦大家看哈!

nothing2014 2009-07-02 06:33:56
加精
1)用int数组实现栈,实现pop(),push()方法,并且当栈空间满时,栈的空间增加一倍。
2)在1)的基础之上,增加一个getMax()方法,返回栈中的最大值,要求这个函数具有O(1)的时间复杂度
...全文
335 83 打赏 收藏 转发到动态 举报
写回复
用AI写文章
83 条回复
切换为时间正序
请发表友善的回复…
发表回复
ctlu2ct2t 2009-07-07
  • 打赏
  • 举报
回复
向48楼学习!
Sunny_kaka 2009-07-06
  • 打赏
  • 举报
回复
以前都做过....
xiaobai532 2009-07-06
  • 打赏
  • 举报
回复
mark
MT502 2009-07-06
  • 打赏
  • 举报
回复
设置一个索引指向最大值下标,第一次插入时指向0,以后每次插入时都和当前索引的值比较一下看是否要更新索引,取的时候直接根据索引访问数组就是O(1)了
夕阳_近黄昏 2009-07-06
  • 打赏
  • 举报
回复
学习一下
xt_497296635 2009-07-06
  • 打赏
  • 举报
回复
学习....
  • 打赏
  • 举报
回复
我也以为getMax()是返回最大值呢,如果是返回最大值估计要在Pop和Push上作手脚了

返回容量O(1)还是没什么问题的,可以top-base就没问题了
feversteven 2009-07-06
  • 打赏
  • 举报
回复
学习
icesnowjank 2009-07-06
  • 打赏
  • 举报
回复
push 的时候记录最大值 保存起来 以备不时之需……
小曦子 2009-07-06
  • 打赏
  • 举报
回复
很不错
sd01397055 2009-07-06
  • 打赏
  • 举报
回复
48楼高手
cht_1988 2009-07-06
  • 打赏
  • 举报
回复
不错的资料啊
依然白板 2009-07-06
  • 打赏
  • 举报
回复
保存sec_max没有意义, 当Pop的值正好是max的时候, max=sec_max, 那么sec_max值又取什么值, 结果是还要再扫描数组。

还是弹栈的说法比较适合。。。。。憋一会弹得更有劲
Marty束 2009-07-06
  • 打赏
  • 举报
回复
Take care
  • 打赏
  • 举报
回复
蛮有意思的,自己写了下,大家给拍拍砖
package com.saturday.test;

/**
* 实现堆栈操作
* @author 王建波
*/
public class MyStack {
private int index;
private int[] stack;
private int[] max;
private static int PAGE_SIZE=10;

public static void main(String[] args){
MyStack stack=new MyStack();

try{
//入栈出栈测试
stack.push(1);
stack.push(2);
stack.push(12);
stack.push(17);
stack.push(55);
stack.push(19);
System.out.println(
stack.toString()+"max:"+stack.getMax()
);

System.out.println(stack.pop());
System.out.println(stack.pop());
System.out.println(stack.pop());
System.out.println(
stack.toString()+"max:"+stack.getMax()
);
stack.pop();
stack.pop();
stack.pop();

//测试自动扩容
stack.push(0);
stack.push(110);
stack.push(2);
stack.push(3);
stack.push(4);
stack.push(5);
stack.push(6);
stack.push(7);
stack.push(8);
stack.push(9);
stack.push(10);
stack.push(11);
stack.push(12);
stack.push(13);
System.out.println(stack);
System.out.println(stack.getMax());

}catch (Exception e) {
e.printStackTrace();
}
}


public MyStack(){
index=-1;
stack=new int[PAGE_SIZE];
max=new int[PAGE_SIZE];
}


public void push(int num){
int stackLen=stack.length;

index++;
stack[index]=num;
max[index]=index<1?num
:(max[index-1]>num?max[index-1]:num);

if(index==stackLen-1){
int[] _newStack=new int[stackLen+PAGE_SIZE];
int[] _newMax=new int[stackLen+PAGE_SIZE];

System.arraycopy(stack,0,_newStack,0,stackLen);
System.arraycopy(max,0,_newMax,0,stackLen);

stack=_newStack;
max=_newMax;
}
}


public int pop() throws Exception{
if(index>=0){
return stack[index--];
}else {
throw new Exception("Emputy Stack!");
}
}


public int getValue() throws Exception{
if(index>=0){
return stack[index];
}else {
throw new Exception("Emputy Stack!");
}
}


public int getMax() throws Exception{
if(index>=0){
return max[index];
}else {
throw new Exception("Emputy Stack!");
}
}


public String toString(){
StringBuffer sbOut=new StringBuffer();

sbOut.append('[');
for(int i=0;i<=index;i++){
sbOut.append(i==0?stack[i]:","+stack[i]);
}
sbOut.append(']');

return sbOut.toString();
}
}


xinglongjian 2009-07-06
  • 打赏
  • 举报
回复
顶一下,后者是有难度啊,大家集思广益吧
zhangpeng822 2009-07-06
  • 打赏
  • 举报
回复
顶一下
  • 打赏
  • 举报
回复
先Mark
qgylovelj 2009-07-06
  • 打赏
  • 举报
回复
绑定
hyhymn 2009-07-06
  • 打赏
  • 举报
回复
学习。
加载更多回复(62)

62,628

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧