实现getMin功能的栈
2016-07-10 16:41
267 查看
第一种方式:
import java.util.Stack;
public class MyStack1 {
public static void main(String[] args) {
MyStack1Real myStack1Real = new MyStack1Real();
for (int i = 0; i < 10; i++) {
myStack1Real.push(i);
}
myStack1Real.push(-1);
System.out.println(myStack1Real.getMin());
}
}
class MyStack1Real {
private Stack<Integer> stackData;
private Stack<Integer> stackMin;
public MyStack1Real() {
this.stackData = new Stack<Integer>();
this.stackMin = new Stack<Integer>();
}
public void push(int newNum) {
if (stackMin.isEmpty()) {
stackMin.push(newNum);
} else if (newNum <= this.getMin()) {
stackMin.push(newNum);
}
stackData.push(newNum);
}
public int pop() {
if (this.stackData.peek() == this.stackMin.peek()) {
this.stackData.pop();
return this.stackMin.pop();
} else {
return this.stackMin.pop();
}
}
public int getMin() {
if (this.stackMin.isEmpty()) {
throw new RuntimeException("your stack is empty!");
} else {
return this.stackMin.peek();
}
}
}
第二种方式:
import java.util.Stack;
public class MyStack1 {
public static void main(String[] args) {
MyStack1Real myStack1Real = new MyStack1Real();
for (int i = 0; i < 10; i++) {
myStack1Real.push(i);
}
myStack1Real.push(-1);
System.out.println(myStack1Real.getMin());
}
}
class MyStack1Real {
private Stack<Integer> stackData;
private Stack<Integer> stackMin;
public MyStack1Real() {
this.stackData = new Stack<Integer>();
this.stackMin = new Stack<Integer>();
}
public void push(int newNum) {
if (stackMin.isEmpty()) {
stackMin.push(newNum);
} else if (newNum <= this.getMin()) {
stackMin.push(newNum);
}
stackData.push(newNum);
}
public int pop() {
if (this.stackData.peek() == this.stackMin.peek()) {
this.stackData.pop();
return this.stackMin.pop();
} else {
return this.stackMin.pop();
}
}
public int getMin() {
if (this.stackMin.isEmpty()) {
throw new RuntimeException("your stack is empty!");
} else {
return this.stackMin.peek();
}
}
}
第二种方式:
package stackAndQueue; import java.util.Stack; public class MyStack2 { public static void main(String[] args) { MyStack1Real myStack1Real = new MyStack1Real(); for (int i = 0; i < 10; i++) { myStack1Real.push(i); } myStack1Real.push(-8); System.out.println(myStack1Real.getMin()); } } class MyStack2Real { private Stack<Integer> stackData; private Stack<Integer> stackMin; public MyStack2Real() { this.stackData = new Stack<Integer>(); this.stackMin = new Stack<Integer>(); } public void push(int newNum) { if (this.stackMin.isEmpty()) { this.stackData.push(newNum); this.stackMin.push(newNum); } else if (newNum <= this.getMin()) { this.stackData.push(newNum); this.stackMin.push(newNum); } else { this.stackData.push(newNum); this.stackMin.push(this.stackMin.peek()); } } public int pop(int newNum) { this.stackMin.pop(); return this.stackData.pop(); } public int getMin() { if (this.stackMin.isEmpty()) { throw new RuntimeException("your stack is empty"); } else { return stackMin.peek(); } } }
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- c++11 + SDL2 + ffmpeg +OpenAL + java = Android播放器
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序
- 二叉查找树