~~~
import java.util.Stack;
/**
* 只使用棧和遞歸逆序一個棧
*/
public class Algorithm02 {
public static void main(String[] args) {
StackUtil stack = new StackUtil();
Stack<Integer> nums = new Stack<>();
nums.push(1);
nums.push(2);
nums.push(3);
nums.push(4);
nums.push(5);
stack.reverse(nums);
Integer result = null;
while((result = nums.peek()) != null){
System.out.println(nums.pop());
}
}
}
class StackUtil {
private Integer getMin(Stack<Integer> stack) {
Integer result = stack.pop();
if (stack.isEmpty()) {
return result;
} else {
Integer minNum = getMin(stack);
stack.push(result);
return minNum;
}
}
public void reverse(Stack stack) {
if (stack != null && !stack.isEmpty()) {
Integer result = getMin(stack);
reverse(stack);
stack.push(result);
}
}
}
~~~
- 基礎
- 數據
- 數據元素
- 數據結構
- 集合結構
- 線性結構
- 樹型結構
- 圖狀結構
- 數據存儲結構
- 算法定義
- 算法效率度量
- 算法效率分析
- 時間復雜度
- O(1)
- O(n)
- O(n2)
- O(logn)
- 空間復雜度
- 線性表
- 數組
- 鏈表
- 串矩陣和廣義表
- 串
- 矩陣
- 廣義表
- 棧和隊列
- 棧
- 隊列
- 樹和二叉樹
- 二叉樹
- 滿二叉樹
- 完全二叉樹
- 哈夫曼樹
- 二叉查找樹-BST樹
- AVL樹
- 紅黑樹
- B樹
- B+樹
- 字典樹
- 跳表
- 算法
- 排序算法
- 冒泡排序
- 選擇排序
- 快速排序
- 插入排序
- 希爾排序
- 歸并排序
- 堆排序
- 基數排序
- 計數排序
- 桶排序
- 查找算法
- 二分查找算法
- Hash算法
- 一致性hash算法
- 算法題
- 001-用兩個棧實現隊列
- 002-只使用棧和遞歸逆序一個棧
- 附錄
- SkipList跳表