目录
第一章 栈和队列
CD5 设计getMin功能的栈
解答
import java.util.Scanner;
import java.util.Stack;
public class Main
{
public static class MyStack1
{
private Stack<Integer> stackData;
private Stack<Integer> stackMin;
public MyStack1()
{
this.stackData = new Stack<Integer>();
this.stackMin = new Stack<Integer>();
}
public void push(int newNum)
{
if (this.stackMin.isEmpty())
{
this.stackMin.push(newNum);
}
else if (newNum <= this.getMin())
{
this.stackMin.push(newNum);
}
this.stackData.push(newNum);
}
public int getMin()
{
if (this.stackMin.isEmpty())
{
throw new RuntimeException("Your stack is empty.");
}
return this.stackMin.peek();
}
public int pop()
{
if (this.stackData.isEmpty())
{
throw new RuntimeException("Your stack is empty.");
}
int value = this.stackData.pop();
if (value == this.getMin())
{
this.stackMin.pop();
}
return value;
}
}
public static class MyStack2
{
private Stack<Integer> stackData;
private Stack<Integer> stackMin;
public MyStack2()
{
this.stackData = new Stack<Integer>();
this.stackMin = new Stack<Integer>();
}
public void push(int newNum)
{
if (stackMin.isEmpty())
{
this.stackMin.push(newNum);
}
else if (newNum <= this.getMin())
{
this.stackMin.push(newNum);
}
else
{
int newMin = this.stackMin.peek();
this.stackMin.push(newMin);
}
this.stackData.push(newNum);
}
public int getMin()
{
if (this.stackData.isEmpty())
{
throw new RuntimeException("Your stack is empty");
}
return this.stackMin.peek();
}
public int pop()
{
if (this.stackData.isEmpty())
{
throw new RuntimeException("Your stack is empty");
}
this.stackMin.pop();
return this.stackData.pop();
}
}
public static void main(String[] args)
{
/*
MyStack1 myStack1 = new MyStack1();
Scanner scanner = new Scanner(System.in);
int n = Integer.parseInt(scanner.nextLine());
for (int i = 1; i <= n; i++)
{
String[] inputData = scanner.nextLine().split(" ");
if (inputData[0].equals("push"))
{
myStack1.push(Integer.parseInt(inputData[1]));
}
else if (inputData[0].equals("pop"))
{
myStack1.pop();
}
else if (inputData[0].equals("getMin"))
{
System.out.println(myStack1.getMin());
}
}
scanner.close();
*/
MyStack2 myStack2 = new MyStack2();
Scanner scanner = new Scanner(System.in);
int n = Integer.parseInt(scanner.nextLine());
for (int i = 1; i <= n; i++)
{
String[] inputData = scanner.nextLine().split(" ");
if (inputData[0].equals("push"))
{
myStack2.push(Integer.parseInt(inputData[1]));
}
else if (inputData[0].equals("pop"))
{
myStack2.pop();
}
else if (inputData[0].equals("getMin"))
{
System.out.println(myStack2.getMin());
}
}
scanner.close();
}
}
CD7 用递归函数和栈逆序一个栈
解答
import java.util.Scanner;
import java.util.Stack;
public class Main
{
// 递归函数一:将栈底的元素返回并移除
public static int getAndRemoveLastElement(Stack<Integer> stack)
{
// 记录位置方便递归
int x = stack.pop();
if (stack.isEmpty())
{
return x;
}
else
{
int last = getAndRemoveLastElement(stack);
stack.push(x); // 恢复原来顺序
return last;
}
}
public static void reverse(Stack<Integer> stack)
{
if (stack.isEmpty())
{
return ;
}
int x = getAndRemoveLastElement(stack);
reverse(stack);
stack.push(x);
}
public static void main(String[] args)
{
Stack<Integer> stack = new Stack<>();
Scanner scanner = new Scanner(System.in);
int n = Integer.parseInt(scanner.nextLine());
for (int i = 1; i <= n; i++)
{
int x = scanner.nextInt();
stack.push(x);
}
reverse(stack);
while (!stack.isEmpty())
{
System.out.print(stack.pop() + " ");
}
System.out.println();
}
}
CD13 用一个栈实现另一个栈的排序
代码
import java.util.Scanner;
import java.util.Stack;
public class Main
{
// 要排序的栈为stack, 辅助栈为help, 在stack栈进行pop操作,弹出的元素记作cur
// 分两种情况讨论: 1、 cur <= help栈顶元素, 则将cur直接压入help栈中
// 2、 cur > help栈顶元素 stack.push(help.pop())直到cur <= help栈顶元素位置, 最后把cur压入help中
public static void sortStackByStack(Stack<Integer> stack)
{
Stack<Integer> help = new Stack<>();
while (!stack.isEmpty())
{
int cur = stack.pop();
while (!help.isEmpty() && cur > help.peek())
{
stack.push(help.pop());
}
help.push(cur);
}
while (!help.isEmpty())
{
stack.push(help.pop());
}
}
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
Stack<Integer> stack = new Stack<>();
int n = Integer.valueOf(scanner.nextLine());
// 读入待排序数据
for (int i = 0; i < n; i++)
{
int x = scanner.nextInt();
stack.push(x);
}
sortStackByStack(stack);
// 输出Stack中排好序的结果
while (!stack.isEmpty())
{
System.out.print(stack.pop() + " ");
}
System.out.println();
}
}
CD15 生成窗口最大值数组
解答
import java.util.LinkedList;
import java.util.Scanner;
public class Main
{
public static int[] getMaxWindow(int[] arr, int w)
{
// 检验参数是否有效
if (arr == null && w < 1 && arr.length < w)
{
return null;
}
LinkedList<Integer> qmax = new LinkedList<>();
int[] res = new int[arr.length - w + 1];
int index = 0;
for (int i = 0; i < arr.length; i++)
{
while (!qmax.isEmpty() && arr[qmax.peekLast()] <= arr[i] )
{
qmax.pollLast();
}
qmax.addLast(i);
// {0, 1,} 2, 3, 4... w = 2
// 当 i = 2时, 0出队 也就是 i - w == qmax.peekFirst() 队首元素出队
// qmax要溢出时, 队首元素出队
if (qmax.peekFirst() == i - w)
{
qmax.pollFirst();
}
if (i >= w - 1)
{
// 至少检索了w个元素后,才能把队首元素下标对应值放入res数组中
res[index++] = arr[qmax.peekFirst()];
}
}
return res;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String[] inputData = scanner.nextLine().split(" ");
int len = Integer.parseInt(inputData[0]);
int w = Integer.parseInt(inputData[1]);
int[] arr = new int[len];
int[] res = new int[len - w + 1];
int i = 0;
for (i = 0; i < len; i++)
{
int x = scanner.nextInt();
arr[i] = x;
}
res = getMaxWindow(arr, w);
for (i = 0; i < res.length; i++)
{
System.out.print(res[i] + " ");
}
System.out.println();
}
}