中级农民
- 积分
- 251
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2015-4-23
- 最后登录
- 1970-1-1
|
1:null
2:
2-1:stack
2-2:map
2-3:set
3:
3-1:use two maps to store k-v and v-k pairs respectively.
for the method numLessThan(K), just use int count to recore the result, and get the value by iterating through the map.
3-2:use two priority queues, qu1 and qu2, and a count variable to record the number of items in the current ADT. when we need getMedian():
if(n%2!=0), we dequeue count/2 items to qu2, and count/2+1 is what we want. we then dequeue the remaining count/2 items to qu2, and then move items in qu2 back to qu1; The procedure is similar for n%2==0;
4:
public class SQueue{
Stack s1;
Stack s2;
public SQueue(){
s1=new Stack();
s2=new Stack();
}
public void enqueue(int item){
while(!s2.isEmpty()){
s1.push(s2.pop());
}
s1.push(item);
while(!s1.isEmpty()){
s2.push(s1.pop());
}
}
public int dequeue(){
return s2.pop();
}
public static void main(String args[]){
SQueue test= new SQueue();
test.enqueue(1);
test.enqueue(2);
test.enqueue(3);
test.enqueue(4);
test.enqueue(5);
System.out.println(test.dequeue());
System.out.println(test.dequeue());
System.out.println(test.dequeue());
System.out.println(test.dequeue());
System.out.println(test.dequeue());
}
}
//离最新进度越来越近了。。。。 |
|