Skip to content

Commit

Permalink
Merge pull request #57 from GordenChow/master
Browse files Browse the repository at this point in the history
Jvm第六次作业提交
  • Loading branch information
wizardzhang2017 authored May 10, 2017
2 parents 8fcd5b8 + fb72152 commit 305898c
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.coding.basic.stack;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
* 设计一个栈,支持栈的push和pop操作,以及第三种操作findMin, 它返回改数据结构中的最小元素
* finMin操作最坏的情形下时间复杂度应该是O(1) , 简单来讲,操作一次就可以得到最小值
* @author liuxin
*
*/
public class QuickMinStack {

//存储栈中元素,升序排列,minAscList.get(0)即能取出
private List<Integer> minAscList = new ArrayList<>();

private Stack stack;

public void push(int data){
stack.push(data);
minAscList.add(data);
minAscList.sort(new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return o1.compareTo(o2);
}
});
}
public int pop(){

int val = (Integer)stack.pop();
minAscList.remove(new Integer(val));

return val;
}
public int findMin(){
return minAscList.get(0);
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.coding.basic.stack;


import com.coding.basic.queue.Queue;

public class StackWithTwoQueues {

Queue<Integer> q1 = new Queue<>();
Queue<Integer> q2 = new Queue<>();


public void push(int data) {
if (q1.isEmpty()) {
q2.enQueue(data);
} else {
q1.enQueue(data);
}
}

public int pop() {
if (!q1.isEmpty()) {
while (!q1.isEmpty()) {
int val = q1.deQueue();
if (q1.isEmpty()) {
q2.enQueue(val);
} else {
return val;
}
}
} else {
while (!q2.isEmpty()) {
int val = q2.deQueue();
if (!q2.isEmpty()) {
q1.enQueue(val);
} else {
return val;
}
}

}
return 0;
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.coding.basic.stack;

/**
* 用一个数组实现两个栈
* 将数组的起始位置看作是第一个栈的栈底,将数组的尾部看作第二个栈的栈底,压栈时,栈顶指针分别向中间移动,直到两栈顶指针相遇,则扩容。
* @author liuxin
*
*/
public class TwoStackInOneArray {
Object[] data = new Object[10];

/**
* 向第一个栈中压入元素
* @param o
*/
public void push1(Object o){

}
/**
* 从第一个栈中弹出元素
* @return
*/
public Object pop1(){
return null;
}

/**
* 获取第一个栈的栈顶元素
* @return
*/

public Object peek1(){
return null;
}
/*
* 向第二个栈压入元素
*/
public void push2(Object o){

}
/**
* 从第二个栈弹出元素
* @return
*/
public Object pop2(){
return null;
}
/**
* 获取第二个栈的栈顶元素
* @return
*/

public Object peek2(){
return null;
}

}

0 comments on commit 305898c

Please sign in to comment.