Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2786363
  • 博文数量: 471
  • 博客积分: 7081
  • 博客等级: 少将
  • 技术积分: 5369
  • 用 户 组: 普通用户
  • 注册时间: 2012-01-04 21:55
文章分类

全部博文(471)

文章存档

2014年(90)

2013年(69)

2012年(312)

分类: Java

2012-07-16 09:55:34

点击(此处)折叠或打开

  1. 百度一面算法题(常数时间内求栈中最大值)
  2. 算法描述:
  3. 一个栈stack,具有push和pop操作,其时间复杂度皆为O(1)
  4. 设计算法max操作,求栈中的最大值,该操作的时间复杂度也要求为O(1)
  5. 可以修改栈的存储方式,push,pop的操作,但是要保证O(1)的时间复杂度,空间时间复杂度无要求。

我的maxNumList并没有存储所有数字啊,比如我按顺序入栈这些数字:[1, 5, 3, 2, 1, 5, 7, 6, 7, 8, -1, -5, 2, 7, 2, 3, 5, 9, 9],然后我的maxNumList存入的数字是:[1, 5, 5, 7, 7, 8, 9, 9]; 

再然后我的栈连续POP两次以后变成:[1, 5, 3, 2, 1, 5, 7, 6, 7, 8, -1, -5, 2, 7, 2, 3, 5], 
这时我的maxNumList也变成了[1, 5, 5, 7, 7, 8], 

1、如果数字的区间不是特别大的话,可以考虑除栈本身的数据结构外(不管是用顺序表还是链表),用一个BitSet存储所有的数,在存储的时候相当于对所有数进行了排序,并且取最大值也很容易,然后每次pop的时候对相应位set 0就可以了,而且用多一个maxIndex存储最大值坐标。

2、做一链状列表; 
push的时候链表插入元素,并保持链表降序排列; 
pop的时候直接在链表种找到结点删除即可。 链表第一个结点始终为栈中元素最大值。

用第二种方法模拟实现:
 



点击(此处)折叠或打开

  1. package littlejava;

  2. import java.util.LinkedList;

  3. public class LinkedListMaxMinVal
  4. {
  5.     LinkedList stack = new LinkedList();//就是一个栈
  6.     LinkedList linklist = new LinkedList();//当链表用
  7.     
  8.     /*add(int index, E element)
  9.     Inserts the specified element at the specified position in this list.
  10.     remove(int index)
  11.     Removes the element at the specified position in this list.
  12.     indexOf(Object o)
  13.     Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.
  14.     */

  15.     public void push(int x)
  16.     {
  17.         stack.push(x);
  18.         int index=0,i;
  19.         for(i=0;i
  20.         { //5 4 3 2 1
  21.             if(x >=linklist.get(i))
  22.             {
  23.                 index=i;
  24.                 break;
  25.             }
  26.         }
  27.         if(i!=0 && i==linklist.size())
  28.             linklist.add(i,x);//在最后加
  29.         linklist.add(index, x);
  30.             
  31.     }
  32.     public int pop()
  33.     {
  34.         int x=stack.getFirst();
  35.         int index=linklist.indexOf(x);
  36.         linklist.remove(index);
  37.         return stack.pop();// Pops an element from the stack represented by this list.
  38.     }
  39.     
  40.     public int getMax()
  41.     {
  42.         return linklist.getFirst();
  43.     }
  44.     
  45.     public int getMin()
  46.     {
  47.         return linklist.getLast();
  48.     }
  49.     public int top()
  50.     {
  51.          /*取栈顶元素相当于取LinkedList容器的头位置的元素
  52.          * Returns the first element in this list.
  53.          **/
  54.         return stack.getFirst();
  55.         
  56.     }
  57.     
  58.     public LinkedList getLinklist() {
  59.         return linklist;
  60.     }
  61.     
  62.     public LinkedList getStack() {
  63.         return stack;
  64.     }

  65.     
  66.     public static void main(String []args)
  67.     {
  68.         LinkedListMaxMinVal mystack = new LinkedListMaxMinVal();
  69.         mystack.push(1);
  70.         mystack.push(2);
  71.         mystack.push(4);
  72.         System.out.println(mystack.getMax());
  73.         mystack.push(6);
  74.         mystack.push(5);
  75.         mystack.push(3);
  76.         System.out.println(mystack.getMax());
  77.         mystack.push(5);
  78.         System.out.println(mystack.getMax());
  79.         
  80.         for(int i=0;i
  81.         {
  82.             System.out.print(+mystack.getLinklist().get(i)+" ");
  83.         }
  84.         
  85.         mystack.pop();
  86.         mystack.pop();
  87.         mystack.pop();
  88.         System.out.println();
  89.         for(int i=0;i
  90.         {
  91.             System.out.print(+mystack.getLinklist().get(i)+" ");
  92.         }
  93.         mystack.push(8);
  94.         System.out.println("\n"+mystack.getMax());
  95.     }
  96. }

阅读(1643) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~