feat: Traditional Chinese version (#1163)

* First commit

* Update mkdocs.yml

* Translate all the docs to traditional Chinese

* Translate the code files.

* Translate the docker file

* Fix mkdocs.yml

* Translate all the figures from SC to TC

* 二叉搜尋樹 -> 二元搜尋樹

* Update terminology.

* Update terminology

* 构造函数/构造方法 -> 建構子
异或 -> 互斥或

* 擴充套件 -> 擴展

* constant - 常量 - 常數

* 類	-> 類別

* AVL -> AVL 樹

* 數組 -> 陣列

* 係統 -> 系統
斐波那契數列 -> 費波那契數列
運算元量 -> 運算量
引數 -> 參數

* 聯絡 -> 關聯

* 麵試 -> 面試

* 面向物件 -> 物件導向
歸併排序 -> 合併排序
范式 -> 範式

* Fix 算法 -> 演算法

* 錶示 -> 表示
反碼 -> 一補數
補碼 -> 二補數
列列尾部 -> 佇列尾部
區域性性 -> 區域性
一摞 -> 一疊

* Synchronize with main branch

* 賬號 -> 帳號
推匯 -> 推導

* Sync with main branch

* First commit

* Update mkdocs.yml

* Translate all the docs to traditional Chinese

* Translate the code files.

* Translate the docker file

* Fix mkdocs.yml

* Translate all the figures from SC to TC

* 二叉搜尋樹 -> 二元搜尋樹

* Update terminology

* 构造函数/构造方法 -> 建構子
异或 -> 互斥或

* 擴充套件 -> 擴展

* constant - 常量 - 常數

* 類	-> 類別

* AVL -> AVL 樹

* 數組 -> 陣列

* 係統 -> 系統
斐波那契數列 -> 費波那契數列
運算元量 -> 運算量
引數 -> 參數

* 聯絡 -> 關聯

* 麵試 -> 面試

* 面向物件 -> 物件導向
歸併排序 -> 合併排序
范式 -> 範式

* Fix 算法 -> 演算法

* 錶示 -> 表示
反碼 -> 一補數
補碼 -> 二補數
列列尾部 -> 佇列尾部
區域性性 -> 區域性
一摞 -> 一疊

* Synchronize with main branch

* 賬號 -> 帳號
推匯 -> 推導

* Sync with main branch

* Update terminology.md

* 操作数量(num. of operations)-> 操作數量

* 字首和->前綴和

* Update figures

* 歸 -> 迴
記憶體洩漏 -> 記憶體流失

* Fix the bug of the file filter

* 支援 -> 支持
Add zh-Hant/README.md

* Add the zh-Hant chapter covers.
Bug fixes.

* 外掛 -> 擴充功能

* Add the landing page for zh-Hant version

* Unify the font of the chapter covers for the zh, en, and zh-Hant version

* Move zh-Hant/ to zh-hant/

* Translate terminology.md to traditional Chinese
This commit is contained in:
Yudong Jin
2024-04-06 02:30:11 +08:00
committed by GitHub
parent 33d7f8a2e5
commit 5f7385c8a3
1875 changed files with 102923 additions and 18 deletions

1
zh-hant/codes/java/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
build

View File

@@ -0,0 +1,105 @@
/**
* File: array.java
* Created Time: 2022-11-25
* Author: krahets (krahets@163.com)
*/
package chapter_array_and_linkedlist;
import java.util.*;
import java.util.concurrent.ThreadLocalRandom;
public class array {
/* 隨機訪問元素 */
static int randomAccess(int[] nums) {
// 在區間 [0, nums.length) 中隨機抽取一個數字
int randomIndex = ThreadLocalRandom.current().nextInt(0, nums.length);
// 獲取並返回隨機元素
int randomNum = nums[randomIndex];
return randomNum;
}
/* 擴展陣列長度 */
static int[] extend(int[] nums, int enlarge) {
// 初始化一個擴展長度後的陣列
int[] res = new int[nums.length + enlarge];
// 將原陣列中的所有元素複製到新陣列
for (int i = 0; i < nums.length; i++) {
res[i] = nums[i];
}
// 返回擴展後的新陣列
return res;
}
/* 在陣列的索引 index 處插入元素 num */
static void insert(int[] nums, int num, int index) {
// 把索引 index 以及之後的所有元素向後移動一位
for (int i = nums.length - 1; i > index; i--) {
nums[i] = nums[i - 1];
}
// 將 num 賦給 index 處的元素
nums[index] = num;
}
/* 刪除索引 index 處的元素 */
static void remove(int[] nums, int index) {
// 把索引 index 之後的所有元素向前移動一位
for (int i = index; i < nums.length - 1; i++) {
nums[i] = nums[i + 1];
}
}
/* 走訪陣列 */
static void traverse(int[] nums) {
int count = 0;
// 透過索引走訪陣列
for (int i = 0; i < nums.length; i++) {
count += nums[i];
}
// 直接走訪陣列元素
for (int num : nums) {
count += num;
}
}
/* 在陣列中查詢指定元素 */
static int find(int[] nums, int target) {
for (int i = 0; i < nums.length; i++) {
if (nums[i] == target)
return i;
}
return -1;
}
/* Driver Code */
public static void main(String[] args) {
/* 初始化陣列 */
int[] arr = new int[5];
System.out.println("陣列 arr = " + Arrays.toString(arr));
int[] nums = { 1, 3, 2, 5, 4 };
System.out.println("陣列 nums = " + Arrays.toString(nums));
/* 隨機訪問 */
int randomNum = randomAccess(nums);
System.out.println("在 nums 中獲取隨機元素 " + randomNum);
/* 長度擴展 */
nums = extend(nums, 3);
System.out.println("將陣列長度擴展至 8 ,得到 nums = " + Arrays.toString(nums));
/* 插入元素 */
insert(nums, 6, 3);
System.out.println("在索引 3 處插入數字 6 ,得到 nums = " + Arrays.toString(nums));
/* 刪除元素 */
remove(nums, 2);
System.out.println("刪除索引 2 處的元素,得到 nums = " + Arrays.toString(nums));
/* 走訪陣列 */
traverse(nums);
/* 查詢元素 */
int index = find(nums, 3);
System.out.println("在 nums 中查詢元素 3 ,得到索引 = " + index);
}
}

View File

@@ -0,0 +1,86 @@
/**
* File: linked_list.java
* Created Time: 2022-11-25
* Author: krahets (krahets@163.com)
*/
package chapter_array_and_linkedlist;
import utils.*;
public class linked_list {
/* 在鏈結串列的節點 n0 之後插入節點 P */
static void insert(ListNode n0, ListNode P) {
ListNode n1 = n0.next;
P.next = n1;
n0.next = P;
}
/* 刪除鏈結串列的節點 n0 之後的首個節點 */
static void remove(ListNode n0) {
if (n0.next == null)
return;
// n0 -> P -> n1
ListNode P = n0.next;
ListNode n1 = P.next;
n0.next = n1;
}
/* 訪問鏈結串列中索引為 index 的節點 */
static ListNode access(ListNode head, int index) {
for (int i = 0; i < index; i++) {
if (head == null)
return null;
head = head.next;
}
return head;
}
/* 在鏈結串列中查詢值為 target 的首個節點 */
static int find(ListNode head, int target) {
int index = 0;
while (head != null) {
if (head.val == target)
return index;
head = head.next;
index++;
}
return -1;
}
/* Driver Code */
public static void main(String[] args) {
/* 初始化鏈結串列 */
// 初始化各個節點
ListNode n0 = new ListNode(1);
ListNode n1 = new ListNode(3);
ListNode n2 = new ListNode(2);
ListNode n3 = new ListNode(5);
ListNode n4 = new ListNode(4);
// 構建節點之間的引用
n0.next = n1;
n1.next = n2;
n2.next = n3;
n3.next = n4;
System.out.println("初始化的鏈結串列為");
PrintUtil.printLinkedList(n0);
/* 插入節點 */
insert(n0, new ListNode(0));
System.out.println("插入節點後的鏈結串列為");
PrintUtil.printLinkedList(n0);
/* 刪除節點 */
remove(n0);
System.out.println("刪除節點後的鏈結串列為");
PrintUtil.printLinkedList(n0);
/* 訪問節點 */
ListNode node = access(n0, 3);
System.out.println("鏈結串列中索引 3 處的節點的值 = " + node.val);
/* 查詢節點 */
int index = find(n0, 2);
System.out.println("鏈結串列中值為 2 的節點的索引 = " + index);
}
}

View File

@@ -0,0 +1,66 @@
/**
* File: list.java
* Created Time: 2022-11-25
* Author: krahets (krahets@163.com)
*/
package chapter_array_and_linkedlist;
import java.util.*;
public class list {
public static void main(String[] args) {
/* 初始化串列 */
// 注意陣列的元素型別是 int[] 的包裝類別 Integer[]
Integer[] numbers = new Integer[] { 1, 3, 2, 5, 4 };
List<Integer> nums = new ArrayList<>(Arrays.asList(numbers));
System.out.println("串列 nums = " + nums);
/* 訪問元素 */
int num = nums.get(1);
System.out.println("訪問索引 1 處的元素,得到 num = " + num);
/* 更新元素 */
nums.set(1, 0);
System.out.println("將索引 1 處的元素更新為 0 ,得到 nums = " + nums);
/* 清空串列 */
nums.clear();
System.out.println("清空串列後 nums = " + nums);
/* 在尾部新增元素 */
nums.add(1);
nums.add(3);
nums.add(2);
nums.add(5);
nums.add(4);
System.out.println("新增元素後 nums = " + nums);
/* 在中間插入元素 */
nums.add(3, 6);
System.out.println("在索引 3 處插入數字 6 ,得到 nums = " + nums);
/* 刪除元素 */
nums.remove(3);
System.out.println("刪除索引 3 處的元素,得到 nums = " + nums);
/* 透過索引走訪串列 */
int count = 0;
for (int i = 0; i < nums.size(); i++) {
count += nums.get(i);
}
/* 直接走訪串列元素 */
for (int x : nums) {
count += x;
}
/* 拼接兩個串列 */
List<Integer> nums1 = new ArrayList<>(Arrays.asList(new Integer[] { 6, 8, 7, 10, 9 }));
nums.addAll(nums1);
System.out.println("將串列 nums1 拼接到 nums 之後,得到 nums = " + nums);
/* 排序串列 */
Collections.sort(nums);
System.out.println("排序串列後 nums = " + nums);
}
}

View File

@@ -0,0 +1,147 @@
/**
* File: my_list.java
* Created Time: 2022-11-25
* Author: krahets (krahets@163.com)
*/
package chapter_array_and_linkedlist;
import java.util.*;
/* 串列類別 */
class MyList {
private int[] arr; // 陣列(儲存串列元素)
private int capacity = 10; // 串列容量
private int size = 0; // 串列長度(當前元素數量)
private int extendRatio = 2; // 每次串列擴容的倍數
/* 建構子 */
public MyList() {
arr = new int[capacity];
}
/* 獲取串列長度(當前元素數量) */
public int size() {
return size;
}
/* 獲取串列容量 */
public int capacity() {
return capacity;
}
/* 訪問元素 */
public int get(int index) {
// 索引如果越界,則丟擲異常,下同
if (index < 0 || index >= size)
throw new IndexOutOfBoundsException("索引越界");
return arr[index];
}
/* 更新元素 */
public void set(int index, int num) {
if (index < 0 || index >= size)
throw new IndexOutOfBoundsException("索引越界");
arr[index] = num;
}
/* 在尾部新增元素 */
public void add(int num) {
// 元素數量超出容量時,觸發擴容機制
if (size == capacity())
extendCapacity();
arr[size] = num;
// 更新元素數量
size++;
}
/* 在中間插入元素 */
public void insert(int index, int num) {
if (index < 0 || index >= size)
throw new IndexOutOfBoundsException("索引越界");
// 元素數量超出容量時,觸發擴容機制
if (size == capacity())
extendCapacity();
// 將索引 index 以及之後的元素都向後移動一位
for (int j = size - 1; j >= index; j--) {
arr[j + 1] = arr[j];
}
arr[index] = num;
// 更新元素數量
size++;
}
/* 刪除元素 */
public int remove(int index) {
if (index < 0 || index >= size)
throw new IndexOutOfBoundsException("索引越界");
int num = arr[index];
// 將將索引 index 之後的元素都向前移動一位
for (int j = index; j < size - 1; j++) {
arr[j] = arr[j + 1];
}
// 更新元素數量
size--;
// 返回被刪除的元素
return num;
}
/* 串列擴容 */
public void extendCapacity() {
// 新建一個長度為原陣列 extendRatio 倍的新陣列,並將原陣列複製到新陣列
arr = Arrays.copyOf(arr, capacity() * extendRatio);
// 更新串列容量
capacity = arr.length;
}
/* 將串列轉換為陣列 */
public int[] toArray() {
int size = size();
// 僅轉換有效長度範圍內的串列元素
int[] arr = new int[size];
for (int i = 0; i < size; i++) {
arr[i] = get(i);
}
return arr;
}
}
public class my_list {
/* Driver Code */
public static void main(String[] args) {
/* 初始化串列 */
MyList nums = new MyList();
/* 在尾部新增元素 */
nums.add(1);
nums.add(3);
nums.add(2);
nums.add(5);
nums.add(4);
System.out.println("串列 nums = " + Arrays.toString(nums.toArray()) +
" ,容量 = " + nums.capacity() + " ,長度 = " + nums.size());
/* 在中間插入元素 */
nums.insert(3, 6);
System.out.println("在索引 3 處插入數字 6 ,得到 nums = " + Arrays.toString(nums.toArray()));
/* 刪除元素 */
nums.remove(3);
System.out.println("刪除索引 3 處的元素,得到 nums = " + Arrays.toString(nums.toArray()));
/* 訪問元素 */
int num = nums.get(1);
System.out.println("訪問索引 1 處的元素,得到 num = " + num);
/* 更新元素 */
nums.set(1, 0);
System.out.println("將索引 1 處的元素更新為 0 ,得到 nums = " + Arrays.toString(nums.toArray()));
/* 測試擴容機制 */
for (int i = 0; i < 10; i++) {
// 在 i = 5 時,串列長度將超出串列容量,此時觸發擴容機制
nums.add(i);
}
System.out.println("擴容後的串列 nums = " + Arrays.toString(nums.toArray()) +
" ,容量 = " + nums.capacity() + " ,長度 = " + nums.size());
}
}

View File

@@ -0,0 +1,77 @@
/**
* File: n_queens.java
* Created Time: 2023-05-04
* Author: krahets (krahets@163.com)
*/
package chapter_backtracking;
import java.util.*;
public class n_queens {
/* 回溯演算法n 皇后 */
public static void backtrack(int row, int n, List<List<String>> state, List<List<List<String>>> res,
boolean[] cols, boolean[] diags1, boolean[] diags2) {
// 當放置完所有行時,記錄解
if (row == n) {
List<List<String>> copyState = new ArrayList<>();
for (List<String> sRow : state) {
copyState.add(new ArrayList<>(sRow));
}
res.add(copyState);
return;
}
// 走訪所有列
for (int col = 0; col < n; col++) {
// 計算該格子對應的主對角線和次對角線
int diag1 = row - col + n - 1;
int diag2 = row + col;
// 剪枝:不允許該格子所在列、主對角線、次對角線上存在皇后
if (!cols[col] && !diags1[diag1] && !diags2[diag2]) {
// 嘗試:將皇后放置在該格子
state.get(row).set(col, "Q");
cols[col] = diags1[diag1] = diags2[diag2] = true;
// 放置下一行
backtrack(row + 1, n, state, res, cols, diags1, diags2);
// 回退:將該格子恢復為空位
state.get(row).set(col, "#");
cols[col] = diags1[diag1] = diags2[diag2] = false;
}
}
}
/* 求解 n 皇后 */
public static List<List<List<String>>> nQueens(int n) {
// 初始化 n*n 大小的棋盤,其中 'Q' 代表皇后,'#' 代表空位
List<List<String>> state = new ArrayList<>();
for (int i = 0; i < n; i++) {
List<String> row = new ArrayList<>();
for (int j = 0; j < n; j++) {
row.add("#");
}
state.add(row);
}
boolean[] cols = new boolean[n]; // 記錄列是否有皇后
boolean[] diags1 = new boolean[2 * n - 1]; // 記錄主對角線上是否有皇后
boolean[] diags2 = new boolean[2 * n - 1]; // 記錄次對角線上是否有皇后
List<List<List<String>>> res = new ArrayList<>();
backtrack(0, n, state, res, cols, diags1, diags2);
return res;
}
public static void main(String[] args) {
int n = 4;
List<List<List<String>>> res = nQueens(n);
System.out.println("輸入棋盤長寬為 " + n);
System.out.println("皇后放置方案共有 " + res.size() + "");
for (List<List<String>> state : res) {
System.out.println("--------------------");
for (List<String> row : state) {
System.out.println(row);
}
}
}
}

View File

@@ -0,0 +1,51 @@
/**
* File: permutations_i.java
* Created Time: 2023-04-24
* Author: krahets (krahets@163.com)
*/
package chapter_backtracking;
import java.util.*;
public class permutations_i {
/* 回溯演算法:全排列 I */
public static void backtrack(List<Integer> state, int[] choices, boolean[] selected, List<List<Integer>> res) {
// 當狀態長度等於元素數量時,記錄解
if (state.size() == choices.length) {
res.add(new ArrayList<Integer>(state));
return;
}
// 走訪所有選擇
for (int i = 0; i < choices.length; i++) {
int choice = choices[i];
// 剪枝:不允許重複選擇元素
if (!selected[i]) {
// 嘗試:做出選擇,更新狀態
selected[i] = true;
state.add(choice);
// 進行下一輪選擇
backtrack(state, choices, selected, res);
// 回退:撤銷選擇,恢復到之前的狀態
selected[i] = false;
state.remove(state.size() - 1);
}
}
}
/* 全排列 I */
static List<List<Integer>> permutationsI(int[] nums) {
List<List<Integer>> res = new ArrayList<List<Integer>>();
backtrack(new ArrayList<Integer>(), nums, new boolean[nums.length], res);
return res;
}
public static void main(String[] args) {
int[] nums = { 1, 2, 3 };
List<List<Integer>> res = permutationsI(nums);
System.out.println("輸入陣列 nums = " + Arrays.toString(nums));
System.out.println("所有排列 res = " + res);
}
}

View File

@@ -0,0 +1,53 @@
/**
* File: permutations_ii.java
* Created Time: 2023-04-24
* Author: krahets (krahets@163.com)
*/
package chapter_backtracking;
import java.util.*;
public class permutations_ii {
/* 回溯演算法:全排列 II */
static void backtrack(List<Integer> state, int[] choices, boolean[] selected, List<List<Integer>> res) {
// 當狀態長度等於元素數量時,記錄解
if (state.size() == choices.length) {
res.add(new ArrayList<Integer>(state));
return;
}
// 走訪所有選擇
Set<Integer> duplicated = new HashSet<Integer>();
for (int i = 0; i < choices.length; i++) {
int choice = choices[i];
// 剪枝:不允許重複選擇元素 且 不允許重複選擇相等元素
if (!selected[i] && !duplicated.contains(choice)) {
// 嘗試:做出選擇,更新狀態
duplicated.add(choice); // 記錄選擇過的元素值
selected[i] = true;
state.add(choice);
// 進行下一輪選擇
backtrack(state, choices, selected, res);
// 回退:撤銷選擇,恢復到之前的狀態
selected[i] = false;
state.remove(state.size() - 1);
}
}
}
/* 全排列 II */
static List<List<Integer>> permutationsII(int[] nums) {
List<List<Integer>> res = new ArrayList<List<Integer>>();
backtrack(new ArrayList<Integer>(), nums, new boolean[nums.length], res);
return res;
}
public static void main(String[] args) {
int[] nums = { 1, 2, 2 };
List<List<Integer>> res = permutationsII(nums);
System.out.println("輸入陣列 nums = " + Arrays.toString(nums));
System.out.println("所有排列 res = " + res);
}
}

View File

@@ -0,0 +1,44 @@
/**
* File: preorder_traversal_i_compact.java
* Created Time: 2023-04-16
* Author: krahets (krahets@163.com)
*/
package chapter_backtracking;
import utils.*;
import java.util.*;
public class preorder_traversal_i_compact {
static List<TreeNode> res;
/* 前序走訪:例題一 */
static void preOrder(TreeNode root) {
if (root == null) {
return;
}
if (root.val == 7) {
// 記錄解
res.add(root);
}
preOrder(root.left);
preOrder(root.right);
}
public static void main(String[] args) {
TreeNode root = TreeNode.listToTree(Arrays.asList(1, 7, 3, 4, 5, 6, 7));
System.out.println("\n初始化二元樹");
PrintUtil.printTree(root);
// 前序走訪
res = new ArrayList<>();
preOrder(root);
System.out.println("\n輸出所有值為 7 的節點");
List<Integer> vals = new ArrayList<>();
for (TreeNode node : res) {
vals.add(node.val);
}
System.out.println(vals);
}
}

View File

@@ -0,0 +1,52 @@
/**
* File: preorder_traversal_ii_compact.java
* Created Time: 2023-04-16
* Author: krahets (krahets@163.com)
*/
package chapter_backtracking;
import utils.*;
import java.util.*;
public class preorder_traversal_ii_compact {
static List<TreeNode> path;
static List<List<TreeNode>> res;
/* 前序走訪:例題二 */
static void preOrder(TreeNode root) {
if (root == null) {
return;
}
// 嘗試
path.add(root);
if (root.val == 7) {
// 記錄解
res.add(new ArrayList<>(path));
}
preOrder(root.left);
preOrder(root.right);
// 回退
path.remove(path.size() - 1);
}
public static void main(String[] args) {
TreeNode root = TreeNode.listToTree(Arrays.asList(1, 7, 3, 4, 5, 6, 7));
System.out.println("\n初始化二元樹");
PrintUtil.printTree(root);
// 前序走訪
path = new ArrayList<>();
res = new ArrayList<>();
preOrder(root);
System.out.println("\n輸出所有根節點到節點 7 的路徑");
for (List<TreeNode> path : res) {
List<Integer> vals = new ArrayList<>();
for (TreeNode node : path) {
vals.add(node.val);
}
System.out.println(vals);
}
}
}

View File

@@ -0,0 +1,53 @@
/**
* File: preorder_traversal_iii_compact.java
* Created Time: 2023-04-16
* Author: krahets (krahets@163.com)
*/
package chapter_backtracking;
import utils.*;
import java.util.*;
public class preorder_traversal_iii_compact {
static List<TreeNode> path;
static List<List<TreeNode>> res;
/* 前序走訪:例題三 */
static void preOrder(TreeNode root) {
// 剪枝
if (root == null || root.val == 3) {
return;
}
// 嘗試
path.add(root);
if (root.val == 7) {
// 記錄解
res.add(new ArrayList<>(path));
}
preOrder(root.left);
preOrder(root.right);
// 回退
path.remove(path.size() - 1);
}
public static void main(String[] args) {
TreeNode root = TreeNode.listToTree(Arrays.asList(1, 7, 3, 4, 5, 6, 7));
System.out.println("\n初始化二元樹");
PrintUtil.printTree(root);
// 前序走訪
path = new ArrayList<>();
res = new ArrayList<>();
preOrder(root);
System.out.println("\n輸出所有根節點到節點 7 的路徑,路徑中不包含值為 3 的節點");
for (List<TreeNode> path : res) {
List<Integer> vals = new ArrayList<>();
for (TreeNode node : path) {
vals.add(node.val);
}
System.out.println(vals);
}
}
}

View File

@@ -0,0 +1,77 @@
/**
* File: preorder_traversal_iii_template.java
* Created Time: 2023-04-16
* Author: krahets (krahets@163.com)
*/
package chapter_backtracking;
import utils.*;
import java.util.*;
public class preorder_traversal_iii_template {
/* 判斷當前狀態是否為解 */
static boolean isSolution(List<TreeNode> state) {
return !state.isEmpty() && state.get(state.size() - 1).val == 7;
}
/* 記錄解 */
static void recordSolution(List<TreeNode> state, List<List<TreeNode>> res) {
res.add(new ArrayList<>(state));
}
/* 判斷在當前狀態下,該選擇是否合法 */
static boolean isValid(List<TreeNode> state, TreeNode choice) {
return choice != null && choice.val != 3;
}
/* 更新狀態 */
static void makeChoice(List<TreeNode> state, TreeNode choice) {
state.add(choice);
}
/* 恢復狀態 */
static void undoChoice(List<TreeNode> state, TreeNode choice) {
state.remove(state.size() - 1);
}
/* 回溯演算法:例題三 */
static void backtrack(List<TreeNode> state, List<TreeNode> choices, List<List<TreeNode>> res) {
// 檢查是否為解
if (isSolution(state)) {
// 記錄解
recordSolution(state, res);
}
// 走訪所有選擇
for (TreeNode choice : choices) {
// 剪枝:檢查選擇是否合法
if (isValid(state, choice)) {
// 嘗試:做出選擇,更新狀態
makeChoice(state, choice);
// 進行下一輪選擇
backtrack(state, Arrays.asList(choice.left, choice.right), res);
// 回退:撤銷選擇,恢復到之前的狀態
undoChoice(state, choice);
}
}
}
public static void main(String[] args) {
TreeNode root = TreeNode.listToTree(Arrays.asList(1, 7, 3, 4, 5, 6, 7));
System.out.println("\n初始化二元樹");
PrintUtil.printTree(root);
// 回溯演算法
List<List<TreeNode>> res = new ArrayList<>();
backtrack(new ArrayList<>(), Arrays.asList(root), res);
System.out.println("\n輸出所有根節點到節點 7 的路徑,要求路徑中不包含值為 3 的節點");
for (List<TreeNode> path : res) {
List<Integer> vals = new ArrayList<>();
for (TreeNode node : path) {
vals.add(node.val);
}
System.out.println(vals);
}
}
}

View File

@@ -0,0 +1,55 @@
/**
* File: subset_sum_i.java
* Created Time: 2023-06-21
* Author: krahets (krahets@163.com)
*/
package chapter_backtracking;
import java.util.*;
public class subset_sum_i {
/* 回溯演算法:子集和 I */
static void backtrack(List<Integer> state, int target, int[] choices, int start, List<List<Integer>> res) {
// 子集和等於 target 時,記錄解
if (target == 0) {
res.add(new ArrayList<>(state));
return;
}
// 走訪所有選擇
// 剪枝二:從 start 開始走訪,避免生成重複子集
for (int i = start; i < choices.length; i++) {
// 剪枝一:若子集和超過 target ,則直接結束迴圈
// 這是因為陣列已排序,後邊元素更大,子集和一定超過 target
if (target - choices[i] < 0) {
break;
}
// 嘗試:做出選擇,更新 target, start
state.add(choices[i]);
// 進行下一輪選擇
backtrack(state, target - choices[i], choices, i, res);
// 回退:撤銷選擇,恢復到之前的狀態
state.remove(state.size() - 1);
}
}
/* 求解子集和 I */
static List<List<Integer>> subsetSumI(int[] nums, int target) {
List<Integer> state = new ArrayList<>(); // 狀態(子集)
Arrays.sort(nums); // 對 nums 進行排序
int start = 0; // 走訪起始點
List<List<Integer>> res = new ArrayList<>(); // 結果串列(子集串列)
backtrack(state, target, nums, start, res);
return res;
}
public static void main(String[] args) {
int[] nums = { 3, 4, 5 };
int target = 9;
List<List<Integer>> res = subsetSumI(nums, target);
System.out.println("輸入陣列 nums = " + Arrays.toString(nums) + ", target = " + target);
System.out.println("所有和等於 " + target + " 的子集 res = " + res);
}
}

View File

@@ -0,0 +1,53 @@
/**
* File: subset_sum_i_naive.java
* Created Time: 2023-06-21
* Author: krahets (krahets@163.com)
*/
package chapter_backtracking;
import java.util.*;
public class subset_sum_i_naive {
/* 回溯演算法:子集和 I */
static void backtrack(List<Integer> state, int target, int total, int[] choices, List<List<Integer>> res) {
// 子集和等於 target 時,記錄解
if (total == target) {
res.add(new ArrayList<>(state));
return;
}
// 走訪所有選擇
for (int i = 0; i < choices.length; i++) {
// 剪枝:若子集和超過 target ,則跳過該選擇
if (total + choices[i] > target) {
continue;
}
// 嘗試:做出選擇,更新元素和 total
state.add(choices[i]);
// 進行下一輪選擇
backtrack(state, target, total + choices[i], choices, res);
// 回退:撤銷選擇,恢復到之前的狀態
state.remove(state.size() - 1);
}
}
/* 求解子集和 I包含重複子集 */
static List<List<Integer>> subsetSumINaive(int[] nums, int target) {
List<Integer> state = new ArrayList<>(); // 狀態(子集)
int total = 0; // 子集和
List<List<Integer>> res = new ArrayList<>(); // 結果串列(子集串列)
backtrack(state, target, total, nums, res);
return res;
}
public static void main(String[] args) {
int[] nums = { 3, 4, 5 };
int target = 9;
List<List<Integer>> res = subsetSumINaive(nums, target);
System.out.println("輸入陣列 nums = " + Arrays.toString(nums) + ", target = " + target);
System.out.println("所有和等於 " + target + " 的子集 res = " + res);
System.out.println("請注意,該方法輸出的結果包含重複集合");
}
}

View File

@@ -0,0 +1,60 @@
/**
* File: subset_sum_ii.java
* Created Time: 2023-06-21
* Author: krahets (krahets@163.com)
*/
package chapter_backtracking;
import java.util.*;
public class subset_sum_ii {
/* 回溯演算法:子集和 II */
static void backtrack(List<Integer> state, int target, int[] choices, int start, List<List<Integer>> res) {
// 子集和等於 target 時,記錄解
if (target == 0) {
res.add(new ArrayList<>(state));
return;
}
// 走訪所有選擇
// 剪枝二:從 start 開始走訪,避免生成重複子集
// 剪枝三:從 start 開始走訪,避免重複選擇同一元素
for (int i = start; i < choices.length; i++) {
// 剪枝一:若子集和超過 target ,則直接結束迴圈
// 這是因為陣列已排序,後邊元素更大,子集和一定超過 target
if (target - choices[i] < 0) {
break;
}
// 剪枝四:如果該元素與左邊元素相等,說明該搜尋分支重複,直接跳過
if (i > start && choices[i] == choices[i - 1]) {
continue;
}
// 嘗試:做出選擇,更新 target, start
state.add(choices[i]);
// 進行下一輪選擇
backtrack(state, target - choices[i], choices, i + 1, res);
// 回退:撤銷選擇,恢復到之前的狀態
state.remove(state.size() - 1);
}
}
/* 求解子集和 II */
static List<List<Integer>> subsetSumII(int[] nums, int target) {
List<Integer> state = new ArrayList<>(); // 狀態(子集)
Arrays.sort(nums); // 對 nums 進行排序
int start = 0; // 走訪起始點
List<List<Integer>> res = new ArrayList<>(); // 結果串列(子集串列)
backtrack(state, target, nums, start, res);
return res;
}
public static void main(String[] args) {
int[] nums = { 4, 4, 5 };
int target = 9;
List<List<Integer>> res = subsetSumII(nums, target);
System.out.println("輸入陣列 nums = " + Arrays.toString(nums) + ", target = " + target);
System.out.println("所有和等於 " + target + " 的子集 res = " + res);
}
}

View File

@@ -0,0 +1,76 @@
/**
* File: iteration.java
* Created Time: 2023-08-24
* Author: krahets (krahets@163.com)
*/
package chapter_computational_complexity;
public class iteration {
/* for 迴圈 */
static int forLoop(int n) {
int res = 0;
// 迴圈求和 1, 2, ..., n-1, n
for (int i = 1; i <= n; i++) {
res += i;
}
return res;
}
/* while 迴圈 */
static int whileLoop(int n) {
int res = 0;
int i = 1; // 初始化條件變數
// 迴圈求和 1, 2, ..., n-1, n
while (i <= n) {
res += i;
i++; // 更新條件變數
}
return res;
}
/* while 迴圈(兩次更新) */
static int whileLoopII(int n) {
int res = 0;
int i = 1; // 初始化條件變數
// 迴圈求和 1, 4, 10, ...
while (i <= n) {
res += i;
// 更新條件變數
i++;
i *= 2;
}
return res;
}
/* 雙層 for 迴圈 */
static String nestedForLoop(int n) {
StringBuilder res = new StringBuilder();
// 迴圈 i = 1, 2, ..., n-1, n
for (int i = 1; i <= n; i++) {
// 迴圈 j = 1, 2, ..., n-1, n
for (int j = 1; j <= n; j++) {
res.append("(" + i + ", " + j + "), ");
}
}
return res.toString();
}
/* Driver Code */
public static void main(String[] args) {
int n = 5;
int res;
res = forLoop(n);
System.out.println("\nfor 迴圈的求和結果 res = " + res);
res = whileLoop(n);
System.out.println("\nwhile 迴圈的求和結果 res = " + res);
res = whileLoopII(n);
System.out.println("\nwhile 迴圈(兩次更新)求和結果 res = " + res);
String resStr = nestedForLoop(n);
System.out.println("\n雙層 for 迴圈的走訪結果 " + resStr);
}
}

View File

@@ -0,0 +1,79 @@
/**
* File: recursion.java
* Created Time: 2023-08-24
* Author: krahets (krahets@163.com)
*/
package chapter_computational_complexity;
import java.util.Stack;
public class recursion {
/* 遞迴 */
static int recur(int n) {
// 終止條件
if (n == 1)
return 1;
// 遞:遞迴呼叫
int res = recur(n - 1);
// 迴:返回結果
return n + res;
}
/* 使用迭代模擬遞迴 */
static int forLoopRecur(int n) {
// 使用一個顯式的堆疊來模擬系統呼叫堆疊
Stack<Integer> stack = new Stack<>();
int res = 0;
// 遞:遞迴呼叫
for (int i = n; i > 0; i--) {
// 透過“入堆疊操作”模擬“遞”
stack.push(i);
}
// 迴:返回結果
while (!stack.isEmpty()) {
// 透過“出堆疊操作”模擬“迴”
res += stack.pop();
}
// res = 1+2+3+...+n
return res;
}
/* 尾遞迴 */
static int tailRecur(int n, int res) {
// 終止條件
if (n == 0)
return res;
// 尾遞迴呼叫
return tailRecur(n - 1, res + n);
}
/* 費波那契數列:遞迴 */
static int fib(int n) {
// 終止條件 f(1) = 0, f(2) = 1
if (n == 1 || n == 2)
return n - 1;
// 遞迴呼叫 f(n) = f(n-1) + f(n-2)
int res = fib(n - 1) + fib(n - 2);
// 返回結果 f(n)
return res;
}
/* Driver Code */
public static void main(String[] args) {
int n = 5;
int res;
res = recur(n);
System.out.println("\n遞迴函式的求和結果 res = " + res);
res = forLoopRecur(n);
System.out.println("\n使用迭代模擬遞迴求和結果 res = " + res);
res = tailRecur(n, 0);
System.out.println("\n尾遞迴函式的求和結果 res = " + res);
res = fib(n);
System.out.println("\n費波那契數列的第 " + n + " 項為 " + res);
}
}

View File

@@ -0,0 +1,110 @@
/**
* File: space_complexity.java
* Created Time: 2022-11-25
* Author: krahets (krahets@163.com)
*/
package chapter_computational_complexity;
import utils.*;
import java.util.*;
public class space_complexity {
/* 函式 */
static int function() {
// 執行某些操作
return 0;
}
/* 常數階 */
static void constant(int n) {
// 常數、變數、物件佔用 O(1) 空間
final int a = 0;
int b = 0;
int[] nums = new int[10000];
ListNode node = new ListNode(0);
// 迴圈中的變數佔用 O(1) 空間
for (int i = 0; i < n; i++) {
int c = 0;
}
// 迴圈中的函式佔用 O(1) 空間
for (int i = 0; i < n; i++) {
function();
}
}
/* 線性階 */
static void linear(int n) {
// 長度為 n 的陣列佔用 O(n) 空間
int[] nums = new int[n];
// 長度為 n 的串列佔用 O(n) 空間
List<ListNode> nodes = new ArrayList<>();
for (int i = 0; i < n; i++) {
nodes.add(new ListNode(i));
}
// 長度為 n 的雜湊表佔用 O(n) 空間
Map<Integer, String> map = new HashMap<>();
for (int i = 0; i < n; i++) {
map.put(i, String.valueOf(i));
}
}
/* 線性階(遞迴實現) */
static void linearRecur(int n) {
System.out.println("遞迴 n = " + n);
if (n == 1)
return;
linearRecur(n - 1);
}
/* 平方階 */
static void quadratic(int n) {
// 矩陣佔用 O(n^2) 空間
int[][] numMatrix = new int[n][n];
// 二維串列佔用 O(n^2) 空間
List<List<Integer>> numList = new ArrayList<>();
for (int i = 0; i < n; i++) {
List<Integer> tmp = new ArrayList<>();
for (int j = 0; j < n; j++) {
tmp.add(0);
}
numList.add(tmp);
}
}
/* 平方階(遞迴實現) */
static int quadraticRecur(int n) {
if (n <= 0)
return 0;
// 陣列 nums 長度為 n, n-1, ..., 2, 1
int[] nums = new int[n];
System.out.println("遞迴 n = " + n + " 中的 nums 長度 = " + nums.length);
return quadraticRecur(n - 1);
}
/* 指數階(建立滿二元樹) */
static TreeNode buildTree(int n) {
if (n == 0)
return null;
TreeNode root = new TreeNode(0);
root.left = buildTree(n - 1);
root.right = buildTree(n - 1);
return root;
}
/* Driver Code */
public static void main(String[] args) {
int n = 5;
// 常數階
constant(n);
// 線性階
linear(n);
linearRecur(n);
// 平方階
quadratic(n);
quadraticRecur(n);
// 指數階
TreeNode root = buildTree(n);
PrintUtil.printTree(root);
}
}

View File

@@ -0,0 +1,167 @@
/**
* File: time_complexity.java
* Created Time: 2022-11-25
* Author: krahets (krahets@163.com)
*/
package chapter_computational_complexity;
public class time_complexity {
/* 常數階 */
static int constant(int n) {
int count = 0;
int size = 100000;
for (int i = 0; i < size; i++)
count++;
return count;
}
/* 線性階 */
static int linear(int n) {
int count = 0;
for (int i = 0; i < n; i++)
count++;
return count;
}
/* 線性階(走訪陣列) */
static int arrayTraversal(int[] nums) {
int count = 0;
// 迴圈次數與陣列長度成正比
for (int num : nums) {
count++;
}
return count;
}
/* 平方階 */
static int quadratic(int n) {
int count = 0;
// 迴圈次數與資料大小 n 成平方關係
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
count++;
}
}
return count;
}
/* 平方階(泡沫排序) */
static int bubbleSort(int[] nums) {
int count = 0; // 計數器
// 外迴圈:未排序區間為 [0, i]
for (int i = nums.length - 1; i > 0; i--) {
// 內迴圈:將未排序區間 [0, i] 中的最大元素交換至該區間的最右端
for (int j = 0; j < i; j++) {
if (nums[j] > nums[j + 1]) {
// 交換 nums[j] 與 nums[j + 1]
int tmp = nums[j];
nums[j] = nums[j + 1];
nums[j + 1] = tmp;
count += 3; // 元素交換包含 3 個單元操作
}
}
}
return count;
}
/* 指數階(迴圈實現) */
static int exponential(int n) {
int count = 0, base = 1;
// 細胞每輪一分為二,形成數列 1, 2, 4, 8, ..., 2^(n-1)
for (int i = 0; i < n; i++) {
for (int j = 0; j < base; j++) {
count++;
}
base *= 2;
}
// count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1
return count;
}
/* 指數階(遞迴實現) */
static int expRecur(int n) {
if (n == 1)
return 1;
return expRecur(n - 1) + expRecur(n - 1) + 1;
}
/* 對數階(迴圈實現) */
static int logarithmic(int n) {
int count = 0;
while (n > 1) {
n = n / 2;
count++;
}
return count;
}
/* 對數階(遞迴實現) */
static int logRecur(int n) {
if (n <= 1)
return 0;
return logRecur(n / 2) + 1;
}
/* 線性對數階 */
static int linearLogRecur(int n) {
if (n <= 1)
return 1;
int count = linearLogRecur(n / 2) + linearLogRecur(n / 2);
for (int i = 0; i < n; i++) {
count++;
}
return count;
}
/* 階乘階(遞迴實現) */
static int factorialRecur(int n) {
if (n == 0)
return 1;
int count = 0;
// 從 1 個分裂出 n 個
for (int i = 0; i < n; i++) {
count += factorialRecur(n - 1);
}
return count;
}
/* Driver Code */
public static void main(String[] args) {
// 可以修改 n 執行,體會一下各種複雜度的操作數量變化趨勢
int n = 8;
System.out.println("輸入資料大小 n = " + n);
int count = constant(n);
System.out.println("常數階的操作數量 = " + count);
count = linear(n);
System.out.println("線性階的操作數量 = " + count);
count = arrayTraversal(new int[n]);
System.out.println("線性階(走訪陣列)的操作數量 = " + count);
count = quadratic(n);
System.out.println("平方階的操作數量 = " + count);
int[] nums = new int[n];
for (int i = 0; i < n; i++)
nums[i] = n - i; // [n,n-1,...,2,1]
count = bubbleSort(nums);
System.out.println("平方階(泡沫排序)的操作數量 = " + count);
count = exponential(n);
System.out.println("指數階(迴圈實現)的操作數量 = " + count);
count = expRecur(n);
System.out.println("指數階(遞迴實現)的操作數量 = " + count);
count = logarithmic(n);
System.out.println("對數階(迴圈實現)的操作數量 = " + count);
count = logRecur(n);
System.out.println("對數階(遞迴實現)的操作數量 = " + count);
count = linearLogRecur(n);
System.out.println("線性對數階(遞迴實現)的操作數量 = " + count);
count = factorialRecur(n);
System.out.println("階乘階(遞迴實現)的操作數量 = " + count);
}
}

View File

@@ -0,0 +1,50 @@
/**
* File: worst_best_time_complexity.java
* Created Time: 2022-11-25
* Author: krahets (krahets@163.com)
*/
package chapter_computational_complexity;
import java.util.*;
public class worst_best_time_complexity {
/* 生成一個陣列,元素為 { 1, 2, ..., n },順序被打亂 */
static int[] randomNumbers(int n) {
Integer[] nums = new Integer[n];
// 生成陣列 nums = { 1, 2, 3, ..., n }
for (int i = 0; i < n; i++) {
nums[i] = i + 1;
}
// 隨機打亂陣列元素
Collections.shuffle(Arrays.asList(nums));
// Integer[] -> int[]
int[] res = new int[n];
for (int i = 0; i < n; i++) {
res[i] = nums[i];
}
return res;
}
/* 查詢陣列 nums 中數字 1 所在索引 */
static int findOne(int[] nums) {
for (int i = 0; i < nums.length; i++) {
// 當元素 1 在陣列頭部時,達到最佳時間複雜度 O(1)
// 當元素 1 在陣列尾部時,達到最差時間複雜度 O(n)
if (nums[i] == 1)
return i;
}
return -1;
}
/* Driver Code */
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
int n = 100;
int[] nums = randomNumbers(n);
int index = findOne(nums);
System.out.println("\n陣列 [ 1, 2, ..., n ] 被打亂後 = " + Arrays.toString(nums));
System.out.println("數字 1 的索引為 " + index);
}
}
}

View File

@@ -0,0 +1,45 @@
/**
* File: binary_search_recur.java
* Created Time: 2023-07-17
* Author: krahets (krahets@163.com)
*/
package chapter_divide_and_conquer;
public class binary_search_recur {
/* 二分搜尋:問題 f(i, j) */
static int dfs(int[] nums, int target, int i, int j) {
// 若區間為空,代表無目標元素,則返回 -1
if (i > j) {
return -1;
}
// 計算中點索引 m
int m = (i + j) / 2;
if (nums[m] < target) {
// 遞迴子問題 f(m+1, j)
return dfs(nums, target, m + 1, j);
} else if (nums[m] > target) {
// 遞迴子問題 f(i, m-1)
return dfs(nums, target, i, m - 1);
} else {
// 找到目標元素,返回其索引
return m;
}
}
/* 二分搜尋 */
static int binarySearch(int[] nums, int target) {
int n = nums.length;
// 求解問題 f(0, n-1)
return dfs(nums, target, 0, n - 1);
}
public static void main(String[] args) {
int target = 6;
int[] nums = { 1, 3, 6, 8, 12, 15, 23, 26, 31, 35 };
// 二分搜尋(雙閉區間)
int index = binarySearch(nums, target);
System.out.println("目標元素 6 的索引 = " + index);
}
}

View File

@@ -0,0 +1,51 @@
/**
* File: build_tree.java
* Created Time: 2023-07-17
* Author: krahets (krahets@163.com)
*/
package chapter_divide_and_conquer;
import utils.*;
import java.util.*;
public class build_tree {
/* 構建二元樹:分治 */
static TreeNode dfs(int[] preorder, Map<Integer, Integer> inorderMap, int i, int l, int r) {
// 子樹區間為空時終止
if (r - l < 0)
return null;
// 初始化根節點
TreeNode root = new TreeNode(preorder[i]);
// 查詢 m ,從而劃分左右子樹
int m = inorderMap.get(preorder[i]);
// 子問題:構建左子樹
root.left = dfs(preorder, inorderMap, i + 1, l, m - 1);
// 子問題:構建右子樹
root.right = dfs(preorder, inorderMap, i + 1 + m - l, m + 1, r);
// 返回根節點
return root;
}
/* 構建二元樹 */
static TreeNode buildTree(int[] preorder, int[] inorder) {
// 初始化雜湊表,儲存 inorder 元素到索引的對映
Map<Integer, Integer> inorderMap = new HashMap<>();
for (int i = 0; i < inorder.length; i++) {
inorderMap.put(inorder[i], i);
}
TreeNode root = dfs(preorder, inorderMap, 0, 0, inorder.length - 1);
return root;
}
public static void main(String[] args) {
int[] preorder = { 3, 9, 2, 1, 7 };
int[] inorder = { 9, 3, 1, 2, 7 };
System.out.println("前序走訪 = " + Arrays.toString(preorder));
System.out.println("中序走訪 = " + Arrays.toString(inorder));
TreeNode root = buildTree(preorder, inorder);
System.out.println("構建的二元樹為:");
PrintUtil.printTree(root);
}
}

View File

@@ -0,0 +1,59 @@
/**
* File: hanota.java
* Created Time: 2023-07-17
* Author: krahets (krahets@163.com)
*/
package chapter_divide_and_conquer;
import java.util.*;
public class hanota {
/* 移動一個圓盤 */
static void move(List<Integer> src, List<Integer> tar) {
// 從 src 頂部拿出一個圓盤
Integer pan = src.remove(src.size() - 1);
// 將圓盤放入 tar 頂部
tar.add(pan);
}
/* 求解河內塔問題 f(i) */
static void dfs(int i, List<Integer> src, List<Integer> buf, List<Integer> tar) {
// 若 src 只剩下一個圓盤,則直接將其移到 tar
if (i == 1) {
move(src, tar);
return;
}
// 子問題 f(i-1) :將 src 頂部 i-1 個圓盤藉助 tar 移到 buf
dfs(i - 1, src, tar, buf);
// 子問題 f(1) :將 src 剩餘一個圓盤移到 tar
move(src, tar);
// 子問題 f(i-1) :將 buf 頂部 i-1 個圓盤藉助 src 移到 tar
dfs(i - 1, buf, src, tar);
}
/* 求解河內塔問題 */
static void solveHanota(List<Integer> A, List<Integer> B, List<Integer> C) {
int n = A.size();
// 將 A 頂部 n 個圓盤藉助 B 移到 C
dfs(n, A, B, C);
}
public static void main(String[] args) {
// 串列尾部是柱子頂部
List<Integer> A = new ArrayList<>(Arrays.asList(5, 4, 3, 2, 1));
List<Integer> B = new ArrayList<>();
List<Integer> C = new ArrayList<>();
System.out.println("初始狀態下:");
System.out.println("A = " + A);
System.out.println("B = " + B);
System.out.println("C = " + C);
solveHanota(A, B, C);
System.out.println("圓盤移動完成後:");
System.out.println("A = " + A);
System.out.println("B = " + B);
System.out.println("C = " + C);
}
}

View File

@@ -0,0 +1,44 @@
/**
* File: climbing_stairs_backtrack.java
* Created Time: 2023-06-30
* Author: krahets (krahets@163.com)
*/
package chapter_dynamic_programming;
import java.util.*;
public class climbing_stairs_backtrack {
/* 回溯 */
public static void backtrack(List<Integer> choices, int state, int n, List<Integer> res) {
// 當爬到第 n 階時,方案數量加 1
if (state == n)
res.set(0, res.get(0) + 1);
// 走訪所有選擇
for (Integer choice : choices) {
// 剪枝:不允許越過第 n 階
if (state + choice > n)
continue;
// 嘗試:做出選擇,更新狀態
backtrack(choices, state + choice, n, res);
// 回退
}
}
/* 爬樓梯:回溯 */
public static int climbingStairsBacktrack(int n) {
List<Integer> choices = Arrays.asList(1, 2); // 可選擇向上爬 1 階或 2 階
int state = 0; // 從第 0 階開始爬
List<Integer> res = new ArrayList<>();
res.add(0); // 使用 res[0] 記錄方案數量
backtrack(choices, state, n, res);
return res.get(0);
}
public static void main(String[] args) {
int n = 9;
int res = climbingStairsBacktrack(n);
System.out.println(String.format("爬 %d 階樓梯共有 %d 種方案", n, res));
}
}

View File

@@ -0,0 +1,36 @@
/**
* File: climbing_stairs_constraint_dp.java
* Created Time: 2023-07-01
* Author: krahets (krahets@163.com)
*/
package chapter_dynamic_programming;
public class climbing_stairs_constraint_dp {
/* 帶約束爬樓梯:動態規劃 */
static int climbingStairsConstraintDP(int n) {
if (n == 1 || n == 2) {
return 1;
}
// 初始化 dp 表,用於儲存子問題的解
int[][] dp = new int[n + 1][3];
// 初始狀態:預設最小子問題的解
dp[1][1] = 1;
dp[1][2] = 0;
dp[2][1] = 0;
dp[2][2] = 1;
// 狀態轉移:從較小子問題逐步求解較大子問題
for (int i = 3; i <= n; i++) {
dp[i][1] = dp[i - 1][2];
dp[i][2] = dp[i - 2][1] + dp[i - 2][2];
}
return dp[n][1] + dp[n][2];
}
public static void main(String[] args) {
int n = 9;
int res = climbingStairsConstraintDP(n);
System.out.println(String.format("爬 %d 階樓梯共有 %d 種方案", n, res));
}
}

View File

@@ -0,0 +1,31 @@
/**
* File: climbing_stairs_dfs.java
* Created Time: 2023-06-30
* Author: krahets (krahets@163.com)
*/
package chapter_dynamic_programming;
public class climbing_stairs_dfs {
/* 搜尋 */
public static int dfs(int i) {
// 已知 dp[1] 和 dp[2] ,返回之
if (i == 1 || i == 2)
return i;
// dp[i] = dp[i-1] + dp[i-2]
int count = dfs(i - 1) + dfs(i - 2);
return count;
}
/* 爬樓梯:搜尋 */
public static int climbingStairsDFS(int n) {
return dfs(n);
}
public static void main(String[] args) {
int n = 9;
int res = climbingStairsDFS(n);
System.out.println(String.format("爬 %d 階樓梯共有 %d 種方案", n, res));
}
}

View File

@@ -0,0 +1,41 @@
/**
* File: climbing_stairs_dfs_mem.java
* Created Time: 2023-06-30
* Author: krahets (krahets@163.com)
*/
package chapter_dynamic_programming;
import java.util.Arrays;
public class climbing_stairs_dfs_mem {
/* 記憶化搜尋 */
public static int dfs(int i, int[] mem) {
// 已知 dp[1] 和 dp[2] ,返回之
if (i == 1 || i == 2)
return i;
// 若存在記錄 dp[i] ,則直接返回之
if (mem[i] != -1)
return mem[i];
// dp[i] = dp[i-1] + dp[i-2]
int count = dfs(i - 1, mem) + dfs(i - 2, mem);
// 記錄 dp[i]
mem[i] = count;
return count;
}
/* 爬樓梯:記憶化搜尋 */
public static int climbingStairsDFSMem(int n) {
// mem[i] 記錄爬到第 i 階的方案總數,-1 代表無記錄
int[] mem = new int[n + 1];
Arrays.fill(mem, -1);
return dfs(n, mem);
}
public static void main(String[] args) {
int n = 9;
int res = climbingStairsDFSMem(n);
System.out.println(String.format("爬 %d 階樓梯共有 %d 種方案", n, res));
}
}

View File

@@ -0,0 +1,48 @@
/**
* File: climbing_stairs_dp.java
* Created Time: 2023-06-30
* Author: krahets (krahets@163.com)
*/
package chapter_dynamic_programming;
public class climbing_stairs_dp {
/* 爬樓梯:動態規劃 */
public static int climbingStairsDP(int n) {
if (n == 1 || n == 2)
return n;
// 初始化 dp 表,用於儲存子問題的解
int[] dp = new int[n + 1];
// 初始狀態:預設最小子問題的解
dp[1] = 1;
dp[2] = 2;
// 狀態轉移:從較小子問題逐步求解較大子問題
for (int i = 3; i <= n; i++) {
dp[i] = dp[i - 1] + dp[i - 2];
}
return dp[n];
}
/* 爬樓梯:空間最佳化後的動態規劃 */
public static int climbingStairsDPComp(int n) {
if (n == 1 || n == 2)
return n;
int a = 1, b = 2;
for (int i = 3; i <= n; i++) {
int tmp = b;
b = a + b;
a = tmp;
}
return b;
}
public static void main(String[] args) {
int n = 9;
int res = climbingStairsDP(n);
System.out.println(String.format("爬 %d 階樓梯共有 %d 種方案", n, res));
res = climbingStairsDPComp(n);
System.out.println(String.format("爬 %d 階樓梯共有 %d 種方案", n, res));
}
}

View File

@@ -0,0 +1,72 @@
/**
* File: coin_change.java
* Created Time: 2023-07-11
* Author: krahets (krahets@163.com)
*/
package chapter_dynamic_programming;
import java.util.Arrays;
public class coin_change {
/* 零錢兌換:動態規劃 */
static int coinChangeDP(int[] coins, int amt) {
int n = coins.length;
int MAX = amt + 1;
// 初始化 dp 表
int[][] dp = new int[n + 1][amt + 1];
// 狀態轉移:首行首列
for (int a = 1; a <= amt; a++) {
dp[0][a] = MAX;
}
// 狀態轉移:其餘行和列
for (int i = 1; i <= n; i++) {
for (int a = 1; a <= amt; a++) {
if (coins[i - 1] > a) {
// 若超過目標金額,則不選硬幣 i
dp[i][a] = dp[i - 1][a];
} else {
// 不選和選硬幣 i 這兩種方案的較小值
dp[i][a] = Math.min(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1);
}
}
}
return dp[n][amt] != MAX ? dp[n][amt] : -1;
}
/* 零錢兌換:空間最佳化後的動態規劃 */
static int coinChangeDPComp(int[] coins, int amt) {
int n = coins.length;
int MAX = amt + 1;
// 初始化 dp 表
int[] dp = new int[amt + 1];
Arrays.fill(dp, MAX);
dp[0] = 0;
// 狀態轉移
for (int i = 1; i <= n; i++) {
for (int a = 1; a <= amt; a++) {
if (coins[i - 1] > a) {
// 若超過目標金額,則不選硬幣 i
dp[a] = dp[a];
} else {
// 不選和選硬幣 i 這兩種方案的較小值
dp[a] = Math.min(dp[a], dp[a - coins[i - 1]] + 1);
}
}
}
return dp[amt] != MAX ? dp[amt] : -1;
}
public static void main(String[] args) {
int[] coins = { 1, 2, 5 };
int amt = 4;
// 動態規劃
int res = coinChangeDP(coins, amt);
System.out.println("湊到目標金額所需的最少硬幣數量為 " + res);
// 空間最佳化後的動態規劃
res = coinChangeDPComp(coins, amt);
System.out.println("湊到目標金額所需的最少硬幣數量為 " + res);
}
}

View File

@@ -0,0 +1,67 @@
/**
* File: coin_change_ii.java
* Created Time: 2023-07-11
* Author: krahets (krahets@163.com)
*/
package chapter_dynamic_programming;
public class coin_change_ii {
/* 零錢兌換 II動態規劃 */
static int coinChangeIIDP(int[] coins, int amt) {
int n = coins.length;
// 初始化 dp 表
int[][] dp = new int[n + 1][amt + 1];
// 初始化首列
for (int i = 0; i <= n; i++) {
dp[i][0] = 1;
}
// 狀態轉移
for (int i = 1; i <= n; i++) {
for (int a = 1; a <= amt; a++) {
if (coins[i - 1] > a) {
// 若超過目標金額,則不選硬幣 i
dp[i][a] = dp[i - 1][a];
} else {
// 不選和選硬幣 i 這兩種方案之和
dp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]];
}
}
}
return dp[n][amt];
}
/* 零錢兌換 II空間最佳化後的動態規劃 */
static int coinChangeIIDPComp(int[] coins, int amt) {
int n = coins.length;
// 初始化 dp 表
int[] dp = new int[amt + 1];
dp[0] = 1;
// 狀態轉移
for (int i = 1; i <= n; i++) {
for (int a = 1; a <= amt; a++) {
if (coins[i - 1] > a) {
// 若超過目標金額,則不選硬幣 i
dp[a] = dp[a];
} else {
// 不選和選硬幣 i 這兩種方案之和
dp[a] = dp[a] + dp[a - coins[i - 1]];
}
}
}
return dp[amt];
}
public static void main(String[] args) {
int[] coins = { 1, 2, 5 };
int amt = 5;
// 動態規劃
int res = coinChangeIIDP(coins, amt);
System.out.println("湊出目標金額的硬幣組合數量為 " + res);
// 空間最佳化後的動態規劃
res = coinChangeIIDPComp(coins, amt);
System.out.println("湊出目標金額的硬幣組合數量為 " + res);
}
}

View File

@@ -0,0 +1,139 @@
/**
* File: edit_distance.java
* Created Time: 2023-07-13
* Author: krahets (krahets@163.com)
*/
package chapter_dynamic_programming;
import java.util.Arrays;
public class edit_distance {
/* 編輯距離:暴力搜尋 */
static int editDistanceDFS(String s, String t, int i, int j) {
// 若 s 和 t 都為空,則返回 0
if (i == 0 && j == 0)
return 0;
// 若 s 為空,則返回 t 長度
if (i == 0)
return j;
// 若 t 為空,則返回 s 長度
if (j == 0)
return i;
// 若兩字元相等,則直接跳過此兩字元
if (s.charAt(i - 1) == t.charAt(j - 1))
return editDistanceDFS(s, t, i - 1, j - 1);
// 最少編輯步數 = 插入、刪除、替換這三種操作的最少編輯步數 + 1
int insert = editDistanceDFS(s, t, i, j - 1);
int delete = editDistanceDFS(s, t, i - 1, j);
int replace = editDistanceDFS(s, t, i - 1, j - 1);
// 返回最少編輯步數
return Math.min(Math.min(insert, delete), replace) + 1;
}
/* 編輯距離:記憶化搜尋 */
static int editDistanceDFSMem(String s, String t, int[][] mem, int i, int j) {
// 若 s 和 t 都為空,則返回 0
if (i == 0 && j == 0)
return 0;
// 若 s 為空,則返回 t 長度
if (i == 0)
return j;
// 若 t 為空,則返回 s 長度
if (j == 0)
return i;
// 若已有記錄,則直接返回之
if (mem[i][j] != -1)
return mem[i][j];
// 若兩字元相等,則直接跳過此兩字元
if (s.charAt(i - 1) == t.charAt(j - 1))
return editDistanceDFSMem(s, t, mem, i - 1, j - 1);
// 最少編輯步數 = 插入、刪除、替換這三種操作的最少編輯步數 + 1
int insert = editDistanceDFSMem(s, t, mem, i, j - 1);
int delete = editDistanceDFSMem(s, t, mem, i - 1, j);
int replace = editDistanceDFSMem(s, t, mem, i - 1, j - 1);
// 記錄並返回最少編輯步數
mem[i][j] = Math.min(Math.min(insert, delete), replace) + 1;
return mem[i][j];
}
/* 編輯距離:動態規劃 */
static int editDistanceDP(String s, String t) {
int n = s.length(), m = t.length();
int[][] dp = new int[n + 1][m + 1];
// 狀態轉移:首行首列
for (int i = 1; i <= n; i++) {
dp[i][0] = i;
}
for (int j = 1; j <= m; j++) {
dp[0][j] = j;
}
// 狀態轉移:其餘行和列
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (s.charAt(i - 1) == t.charAt(j - 1)) {
// 若兩字元相等,則直接跳過此兩字元
dp[i][j] = dp[i - 1][j - 1];
} else {
// 最少編輯步數 = 插入、刪除、替換這三種操作的最少編輯步數 + 1
dp[i][j] = Math.min(Math.min(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]) + 1;
}
}
}
return dp[n][m];
}
/* 編輯距離:空間最佳化後的動態規劃 */
static int editDistanceDPComp(String s, String t) {
int n = s.length(), m = t.length();
int[] dp = new int[m + 1];
// 狀態轉移:首行
for (int j = 1; j <= m; j++) {
dp[j] = j;
}
// 狀態轉移:其餘行
for (int i = 1; i <= n; i++) {
// 狀態轉移:首列
int leftup = dp[0]; // 暫存 dp[i-1, j-1]
dp[0] = i;
// 狀態轉移:其餘列
for (int j = 1; j <= m; j++) {
int temp = dp[j];
if (s.charAt(i - 1) == t.charAt(j - 1)) {
// 若兩字元相等,則直接跳過此兩字元
dp[j] = leftup;
} else {
// 最少編輯步數 = 插入、刪除、替換這三種操作的最少編輯步數 + 1
dp[j] = Math.min(Math.min(dp[j - 1], dp[j]), leftup) + 1;
}
leftup = temp; // 更新為下一輪的 dp[i-1, j-1]
}
}
return dp[m];
}
public static void main(String[] args) {
String s = "bag";
String t = "pack";
int n = s.length(), m = t.length();
// 暴力搜尋
int res = editDistanceDFS(s, t, n, m);
System.out.println("" + s + " 更改為 " + t + " 最少需要編輯 " + res + "");
// 記憶化搜尋
int[][] mem = new int[n + 1][m + 1];
for (int[] row : mem)
Arrays.fill(row, -1);
res = editDistanceDFSMem(s, t, mem, n, m);
System.out.println("" + s + " 更改為 " + t + " 最少需要編輯 " + res + "");
// 動態規劃
res = editDistanceDP(s, t);
System.out.println("" + s + " 更改為 " + t + " 最少需要編輯 " + res + "");
// 空間最佳化後的動態規劃
res = editDistanceDPComp(s, t);
System.out.println("" + s + " 更改為 " + t + " 最少需要編輯 " + res + "");
}
}

View File

@@ -0,0 +1,116 @@
/**
* File: knapsack.java
* Created Time: 2023-07-10
* Author: krahets (krahets@163.com)
*/
package chapter_dynamic_programming;
import java.util.Arrays;
public class knapsack {
/* 0-1 背包:暴力搜尋 */
static int knapsackDFS(int[] wgt, int[] val, int i, int c) {
// 若已選完所有物品或背包無剩餘容量,則返回價值 0
if (i == 0 || c == 0) {
return 0;
}
// 若超過背包容量,則只能選擇不放入背包
if (wgt[i - 1] > c) {
return knapsackDFS(wgt, val, i - 1, c);
}
// 計算不放入和放入物品 i 的最大價值
int no = knapsackDFS(wgt, val, i - 1, c);
int yes = knapsackDFS(wgt, val, i - 1, c - wgt[i - 1]) + val[i - 1];
// 返回兩種方案中價值更大的那一個
return Math.max(no, yes);
}
/* 0-1 背包:記憶化搜尋 */
static int knapsackDFSMem(int[] wgt, int[] val, int[][] mem, int i, int c) {
// 若已選完所有物品或背包無剩餘容量,則返回價值 0
if (i == 0 || c == 0) {
return 0;
}
// 若已有記錄,則直接返回
if (mem[i][c] != -1) {
return mem[i][c];
}
// 若超過背包容量,則只能選擇不放入背包
if (wgt[i - 1] > c) {
return knapsackDFSMem(wgt, val, mem, i - 1, c);
}
// 計算不放入和放入物品 i 的最大價值
int no = knapsackDFSMem(wgt, val, mem, i - 1, c);
int yes = knapsackDFSMem(wgt, val, mem, i - 1, c - wgt[i - 1]) + val[i - 1];
// 記錄並返回兩種方案中價值更大的那一個
mem[i][c] = Math.max(no, yes);
return mem[i][c];
}
/* 0-1 背包:動態規劃 */
static int knapsackDP(int[] wgt, int[] val, int cap) {
int n = wgt.length;
// 初始化 dp 表
int[][] dp = new int[n + 1][cap + 1];
// 狀態轉移
for (int i = 1; i <= n; i++) {
for (int c = 1; c <= cap; c++) {
if (wgt[i - 1] > c) {
// 若超過背包容量,則不選物品 i
dp[i][c] = dp[i - 1][c];
} else {
// 不選和選物品 i 這兩種方案的較大值
dp[i][c] = Math.max(dp[i - 1][c], dp[i - 1][c - wgt[i - 1]] + val[i - 1]);
}
}
}
return dp[n][cap];
}
/* 0-1 背包:空間最佳化後的動態規劃 */
static int knapsackDPComp(int[] wgt, int[] val, int cap) {
int n = wgt.length;
// 初始化 dp 表
int[] dp = new int[cap + 1];
// 狀態轉移
for (int i = 1; i <= n; i++) {
// 倒序走訪
for (int c = cap; c >= 1; c--) {
if (wgt[i - 1] <= c) {
// 不選和選物品 i 這兩種方案的較大值
dp[c] = Math.max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);
}
}
}
return dp[cap];
}
public static void main(String[] args) {
int[] wgt = { 10, 20, 30, 40, 50 };
int[] val = { 50, 120, 150, 210, 240 };
int cap = 50;
int n = wgt.length;
// 暴力搜尋
int res = knapsackDFS(wgt, val, n, cap);
System.out.println("不超過背包容量的最大物品價值為 " + res);
// 記憶化搜尋
int[][] mem = new int[n + 1][cap + 1];
for (int[] row : mem) {
Arrays.fill(row, -1);
}
res = knapsackDFSMem(wgt, val, mem, n, cap);
System.out.println("不超過背包容量的最大物品價值為 " + res);
// 動態規劃
res = knapsackDP(wgt, val, cap);
System.out.println("不超過背包容量的最大物品價值為 " + res);
// 空間最佳化後的動態規劃
res = knapsackDPComp(wgt, val, cap);
System.out.println("不超過背包容量的最大物品價值為 " + res);
}
}

View File

@@ -0,0 +1,53 @@
/**
* File: min_cost_climbing_stairs_dp.java
* Created Time: 2023-06-30
* Author: krahets (krahets@163.com)
*/
package chapter_dynamic_programming;
import java.util.Arrays;
public class min_cost_climbing_stairs_dp {
/* 爬樓梯最小代價:動態規劃 */
public static int minCostClimbingStairsDP(int[] cost) {
int n = cost.length - 1;
if (n == 1 || n == 2)
return cost[n];
// 初始化 dp 表,用於儲存子問題的解
int[] dp = new int[n + 1];
// 初始狀態:預設最小子問題的解
dp[1] = cost[1];
dp[2] = cost[2];
// 狀態轉移:從較小子問題逐步求解較大子問題
for (int i = 3; i <= n; i++) {
dp[i] = Math.min(dp[i - 1], dp[i - 2]) + cost[i];
}
return dp[n];
}
/* 爬樓梯最小代價:空間最佳化後的動態規劃 */
public static int minCostClimbingStairsDPComp(int[] cost) {
int n = cost.length - 1;
if (n == 1 || n == 2)
return cost[n];
int a = cost[1], b = cost[2];
for (int i = 3; i <= n; i++) {
int tmp = b;
b = Math.min(a, tmp) + cost[i];
a = tmp;
}
return b;
}
public static void main(String[] args) {
int[] cost = { 0, 1, 10, 1, 1, 1, 10, 1, 1, 10, 1 };
System.out.println(String.format("輸入樓梯的代價串列為 %s", Arrays.toString(cost)));
int res = minCostClimbingStairsDP(cost);
System.out.println(String.format("爬完樓梯的最低代價為 %d", res));
res = minCostClimbingStairsDPComp(cost);
System.out.println(String.format("爬完樓梯的最低代價為 %d", res));
}
}

View File

@@ -0,0 +1,125 @@
/**
* File: min_path_sum.java
* Created Time: 2023-07-10
* Author: krahets (krahets@163.com)
*/
package chapter_dynamic_programming;
import java.util.Arrays;
public class min_path_sum {
/* 最小路徑和:暴力搜尋 */
static int minPathSumDFS(int[][] grid, int i, int j) {
// 若為左上角單元格,則終止搜尋
if (i == 0 && j == 0) {
return grid[0][0];
}
// 若行列索引越界,則返回 +∞ 代價
if (i < 0 || j < 0) {
return Integer.MAX_VALUE;
}
// 計算從左上角到 (i-1, j) 和 (i, j-1) 的最小路徑代價
int up = minPathSumDFS(grid, i - 1, j);
int left = minPathSumDFS(grid, i, j - 1);
// 返回從左上角到 (i, j) 的最小路徑代價
return Math.min(left, up) + grid[i][j];
}
/* 最小路徑和:記憶化搜尋 */
static int minPathSumDFSMem(int[][] grid, int[][] mem, int i, int j) {
// 若為左上角單元格,則終止搜尋
if (i == 0 && j == 0) {
return grid[0][0];
}
// 若行列索引越界,則返回 +∞ 代價
if (i < 0 || j < 0) {
return Integer.MAX_VALUE;
}
// 若已有記錄,則直接返回
if (mem[i][j] != -1) {
return mem[i][j];
}
// 左邊和上邊單元格的最小路徑代價
int up = minPathSumDFSMem(grid, mem, i - 1, j);
int left = minPathSumDFSMem(grid, mem, i, j - 1);
// 記錄並返回左上角到 (i, j) 的最小路徑代價
mem[i][j] = Math.min(left, up) + grid[i][j];
return mem[i][j];
}
/* 最小路徑和:動態規劃 */
static int minPathSumDP(int[][] grid) {
int n = grid.length, m = grid[0].length;
// 初始化 dp 表
int[][] dp = new int[n][m];
dp[0][0] = grid[0][0];
// 狀態轉移:首行
for (int j = 1; j < m; j++) {
dp[0][j] = dp[0][j - 1] + grid[0][j];
}
// 狀態轉移:首列
for (int i = 1; i < n; i++) {
dp[i][0] = dp[i - 1][0] + grid[i][0];
}
// 狀態轉移:其餘行和列
for (int i = 1; i < n; i++) {
for (int j = 1; j < m; j++) {
dp[i][j] = Math.min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j];
}
}
return dp[n - 1][m - 1];
}
/* 最小路徑和:空間最佳化後的動態規劃 */
static int minPathSumDPComp(int[][] grid) {
int n = grid.length, m = grid[0].length;
// 初始化 dp 表
int[] dp = new int[m];
// 狀態轉移:首行
dp[0] = grid[0][0];
for (int j = 1; j < m; j++) {
dp[j] = dp[j - 1] + grid[0][j];
}
// 狀態轉移:其餘行
for (int i = 1; i < n; i++) {
// 狀態轉移:首列
dp[0] = dp[0] + grid[i][0];
// 狀態轉移:其餘列
for (int j = 1; j < m; j++) {
dp[j] = Math.min(dp[j - 1], dp[j]) + grid[i][j];
}
}
return dp[m - 1];
}
public static void main(String[] args) {
int[][] grid = {
{ 1, 3, 1, 5 },
{ 2, 2, 4, 2 },
{ 5, 3, 2, 1 },
{ 4, 3, 5, 2 }
};
int n = grid.length, m = grid[0].length;
// 暴力搜尋
int res = minPathSumDFS(grid, n - 1, m - 1);
System.out.println("從左上角到右下角的最小路徑和為 " + res);
// 記憶化搜尋
int[][] mem = new int[n][m];
for (int[] row : mem) {
Arrays.fill(row, -1);
}
res = minPathSumDFSMem(grid, mem, n - 1, m - 1);
System.out.println("從左上角到右下角的最小路徑和為 " + res);
// 動態規劃
res = minPathSumDP(grid);
System.out.println("從左上角到右下角的最小路徑和為 " + res);
// 空間最佳化後的動態規劃
res = minPathSumDPComp(grid);
System.out.println("從左上角到右下角的最小路徑和為 " + res);
}
}

View File

@@ -0,0 +1,63 @@
/**
* File: unbounded_knapsack.java
* Created Time: 2023-07-11
* Author: krahets (krahets@163.com)
*/
package chapter_dynamic_programming;
public class unbounded_knapsack {
/* 完全背包:動態規劃 */
static int unboundedKnapsackDP(int[] wgt, int[] val, int cap) {
int n = wgt.length;
// 初始化 dp 表
int[][] dp = new int[n + 1][cap + 1];
// 狀態轉移
for (int i = 1; i <= n; i++) {
for (int c = 1; c <= cap; c++) {
if (wgt[i - 1] > c) {
// 若超過背包容量,則不選物品 i
dp[i][c] = dp[i - 1][c];
} else {
// 不選和選物品 i 這兩種方案的較大值
dp[i][c] = Math.max(dp[i - 1][c], dp[i][c - wgt[i - 1]] + val[i - 1]);
}
}
}
return dp[n][cap];
}
/* 完全背包:空間最佳化後的動態規劃 */
static int unboundedKnapsackDPComp(int[] wgt, int[] val, int cap) {
int n = wgt.length;
// 初始化 dp 表
int[] dp = new int[cap + 1];
// 狀態轉移
for (int i = 1; i <= n; i++) {
for (int c = 1; c <= cap; c++) {
if (wgt[i - 1] > c) {
// 若超過背包容量,則不選物品 i
dp[c] = dp[c];
} else {
// 不選和選物品 i 這兩種方案的較大值
dp[c] = Math.max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);
}
}
}
return dp[cap];
}
public static void main(String[] args) {
int[] wgt = { 1, 2, 3 };
int[] val = { 5, 11, 15 };
int cap = 4;
// 動態規劃
int res = unboundedKnapsackDP(wgt, val, cap);
System.out.println("不超過背包容量的最大物品價值為 " + res);
// 空間最佳化後的動態規劃
res = unboundedKnapsackDPComp(wgt, val, cap);
System.out.println("不超過背包容量的最大物品價值為 " + res);
}
}

View File

@@ -0,0 +1,117 @@
/**
* File: graph_adjacency_list.java
* Created Time: 2023-01-26
* Author: krahets (krahets@163.com)
*/
package chapter_graph;
import java.util.*;
import utils.*;
/* 基於鄰接表實現的無向圖類別 */
class GraphAdjList {
// 鄰接表key頂點value該頂點的所有鄰接頂點
Map<Vertex, List<Vertex>> adjList;
/* 建構子 */
public GraphAdjList(Vertex[][] edges) {
this.adjList = new HashMap<>();
// 新增所有頂點和邊
for (Vertex[] edge : edges) {
addVertex(edge[0]);
addVertex(edge[1]);
addEdge(edge[0], edge[1]);
}
}
/* 獲取頂點數量 */
public int size() {
return adjList.size();
}
/* 新增邊 */
public void addEdge(Vertex vet1, Vertex vet2) {
if (!adjList.containsKey(vet1) || !adjList.containsKey(vet2) || vet1 == vet2)
throw new IllegalArgumentException();
// 新增邊 vet1 - vet2
adjList.get(vet1).add(vet2);
adjList.get(vet2).add(vet1);
}
/* 刪除邊 */
public void removeEdge(Vertex vet1, Vertex vet2) {
if (!adjList.containsKey(vet1) || !adjList.containsKey(vet2) || vet1 == vet2)
throw new IllegalArgumentException();
// 刪除邊 vet1 - vet2
adjList.get(vet1).remove(vet2);
adjList.get(vet2).remove(vet1);
}
/* 新增頂點 */
public void addVertex(Vertex vet) {
if (adjList.containsKey(vet))
return;
// 在鄰接表中新增一個新鏈結串列
adjList.put(vet, new ArrayList<>());
}
/* 刪除頂點 */
public void removeVertex(Vertex vet) {
if (!adjList.containsKey(vet))
throw new IllegalArgumentException();
// 在鄰接表中刪除頂點 vet 對應的鏈結串列
adjList.remove(vet);
// 走訪其他頂點的鏈結串列,刪除所有包含 vet 的邊
for (List<Vertex> list : adjList.values()) {
list.remove(vet);
}
}
/* 列印鄰接表 */
public void print() {
System.out.println("鄰接表 =");
for (Map.Entry<Vertex, List<Vertex>> pair : adjList.entrySet()) {
List<Integer> tmp = new ArrayList<>();
for (Vertex vertex : pair.getValue())
tmp.add(vertex.val);
System.out.println(pair.getKey().val + ": " + tmp + ",");
}
}
}
public class graph_adjacency_list {
public static void main(String[] args) {
/* 初始化無向圖 */
Vertex[] v = Vertex.valsToVets(new int[] { 1, 3, 2, 5, 4 });
Vertex[][] edges = { { v[0], v[1] }, { v[0], v[3] }, { v[1], v[2] },
{ v[2], v[3] }, { v[2], v[4] }, { v[3], v[4] } };
GraphAdjList graph = new GraphAdjList(edges);
System.out.println("\n初始化後圖為");
graph.print();
/* 新增邊 */
// 頂點 1, 2 即 v[0], v[2]
graph.addEdge(v[0], v[2]);
System.out.println("\n新增邊 1-2 後,圖為");
graph.print();
/* 刪除邊 */
// 頂點 1, 3 即 v[0], v[1]
graph.removeEdge(v[0], v[1]);
System.out.println("\n刪除邊 1-3 後,圖為");
graph.print();
/* 新增頂點 */
Vertex v5 = new Vertex(6);
graph.addVertex(v5);
System.out.println("\n新增頂點 6 後,圖為");
graph.print();
/* 刪除頂點 */
// 頂點 3 即 v[1]
graph.removeVertex(v[1]);
System.out.println("\n刪除頂點 3 後,圖為");
graph.print();
}
}

View File

@@ -0,0 +1,131 @@
/**
* File: graph_adjacency_matrix.java
* Created Time: 2023-01-26
* Author: krahets (krahets@163.com)
*/
package chapter_graph;
import utils.*;
import java.util.*;
/* 基於鄰接矩陣實現的無向圖類別 */
class GraphAdjMat {
List<Integer> vertices; // 頂點串列,元素代表“頂點值”,索引代表“頂點索引”
List<List<Integer>> adjMat; // 鄰接矩陣,行列索引對應“頂點索引”
/* 建構子 */
public GraphAdjMat(int[] vertices, int[][] edges) {
this.vertices = new ArrayList<>();
this.adjMat = new ArrayList<>();
// 新增頂點
for (int val : vertices) {
addVertex(val);
}
// 新增邊
// 請注意edges 元素代表頂點索引,即對應 vertices 元素索引
for (int[] e : edges) {
addEdge(e[0], e[1]);
}
}
/* 獲取頂點數量 */
public int size() {
return vertices.size();
}
/* 新增頂點 */
public void addVertex(int val) {
int n = size();
// 向頂點串列中新增新頂點的值
vertices.add(val);
// 在鄰接矩陣中新增一行
List<Integer> newRow = new ArrayList<>(n);
for (int j = 0; j < n; j++) {
newRow.add(0);
}
adjMat.add(newRow);
// 在鄰接矩陣中新增一列
for (List<Integer> row : adjMat) {
row.add(0);
}
}
/* 刪除頂點 */
public void removeVertex(int index) {
if (index >= size())
throw new IndexOutOfBoundsException();
// 在頂點串列中移除索引 index 的頂點
vertices.remove(index);
// 在鄰接矩陣中刪除索引 index 的行
adjMat.remove(index);
// 在鄰接矩陣中刪除索引 index 的列
for (List<Integer> row : adjMat) {
row.remove(index);
}
}
/* 新增邊 */
// 參數 i, j 對應 vertices 元素索引
public void addEdge(int i, int j) {
// 索引越界與相等處理
if (i < 0 || j < 0 || i >= size() || j >= size() || i == j)
throw new IndexOutOfBoundsException();
// 在無向圖中,鄰接矩陣關於主對角線對稱,即滿足 (i, j) == (j, i)
adjMat.get(i).set(j, 1);
adjMat.get(j).set(i, 1);
}
/* 刪除邊 */
// 參數 i, j 對應 vertices 元素索引
public void removeEdge(int i, int j) {
// 索引越界與相等處理
if (i < 0 || j < 0 || i >= size() || j >= size() || i == j)
throw new IndexOutOfBoundsException();
adjMat.get(i).set(j, 0);
adjMat.get(j).set(i, 0);
}
/* 列印鄰接矩陣 */
public void print() {
System.out.print("頂點串列 = ");
System.out.println(vertices);
System.out.println("鄰接矩陣 =");
PrintUtil.printMatrix(adjMat);
}
}
public class graph_adjacency_matrix {
public static void main(String[] args) {
/* 初始化無向圖 */
// 請注意edges 元素代表頂點索引,即對應 vertices 元素索引
int[] vertices = { 1, 3, 2, 5, 4 };
int[][] edges = { { 0, 1 }, { 0, 3 }, { 1, 2 }, { 2, 3 }, { 2, 4 }, { 3, 4 } };
GraphAdjMat graph = new GraphAdjMat(vertices, edges);
System.out.println("\n初始化後圖為");
graph.print();
/* 新增邊 */
// 頂點 1, 2 的索引分別為 0, 2
graph.addEdge(0, 2);
System.out.println("\n新增邊 1-2 後,圖為");
graph.print();
/* 刪除邊 */
// 頂點 1, 3 的索引分別為 0, 1
graph.removeEdge(0, 1);
System.out.println("\n刪除邊 1-3 後,圖為");
graph.print();
/* 新增頂點 */
graph.addVertex(6);
System.out.println("\n新增頂點 6 後,圖為");
graph.print();
/* 刪除頂點 */
// 頂點 3 的索引為 1
graph.removeVertex(1);
System.out.println("\n刪除頂點 3 後,圖為");
graph.print();
}
}

View File

@@ -0,0 +1,55 @@
/**
* File: graph_bfs.java
* Created Time: 2023-02-12
* Author: krahets (krahets@163.com)
*/
package chapter_graph;
import java.util.*;
import utils.*;
public class graph_bfs {
/* 廣度優先走訪 */
// 使用鄰接表來表示圖,以便獲取指定頂點的所有鄰接頂點
static List<Vertex> graphBFS(GraphAdjList graph, Vertex startVet) {
// 頂點走訪序列
List<Vertex> res = new ArrayList<>();
// 雜湊表,用於記錄已被訪問過的頂點
Set<Vertex> visited = new HashSet<>();
visited.add(startVet);
// 佇列用於實現 BFS
Queue<Vertex> que = new LinkedList<>();
que.offer(startVet);
// 以頂點 vet 為起點,迴圈直至訪問完所有頂點
while (!que.isEmpty()) {
Vertex vet = que.poll(); // 佇列首頂點出隊
res.add(vet); // 記錄訪問頂點
// 走訪該頂點的所有鄰接頂點
for (Vertex adjVet : graph.adjList.get(vet)) {
if (visited.contains(adjVet))
continue; // 跳過已被訪問的頂點
que.offer(adjVet); // 只入列未訪問的頂點
visited.add(adjVet); // 標記該頂點已被訪問
}
}
// 返回頂點走訪序列
return res;
}
public static void main(String[] args) {
/* 初始化無向圖 */
Vertex[] v = Vertex.valsToVets(new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });
Vertex[][] edges = { { v[0], v[1] }, { v[0], v[3] }, { v[1], v[2] }, { v[1], v[4] },
{ v[2], v[5] }, { v[3], v[4] }, { v[3], v[6] }, { v[4], v[5] },
{ v[4], v[7] }, { v[5], v[8] }, { v[6], v[7] }, { v[7], v[8] } };
GraphAdjList graph = new GraphAdjList(edges);
System.out.println("\n初始化後圖為");
graph.print();
/* 廣度優先走訪 */
List<Vertex> res = graphBFS(graph, v[0]);
System.out.println("\n廣度優先走訪BFS頂點序列為");
System.out.println(Vertex.vetsToVals(res));
}
}

View File

@@ -0,0 +1,51 @@
/**
* File: graph_dfs.java
* Created Time: 2023-02-12
* Author: krahets (krahets@163.com)
*/
package chapter_graph;
import java.util.*;
import utils.*;
public class graph_dfs {
/* 深度優先走訪輔助函式 */
static void dfs(GraphAdjList graph, Set<Vertex> visited, List<Vertex> res, Vertex vet) {
res.add(vet); // 記錄訪問頂點
visited.add(vet); // 標記該頂點已被訪問
// 走訪該頂點的所有鄰接頂點
for (Vertex adjVet : graph.adjList.get(vet)) {
if (visited.contains(adjVet))
continue; // 跳過已被訪問的頂點
// 遞迴訪問鄰接頂點
dfs(graph, visited, res, adjVet);
}
}
/* 深度優先走訪 */
// 使用鄰接表來表示圖,以便獲取指定頂點的所有鄰接頂點
static List<Vertex> graphDFS(GraphAdjList graph, Vertex startVet) {
// 頂點走訪序列
List<Vertex> res = new ArrayList<>();
// 雜湊表,用於記錄已被訪問過的頂點
Set<Vertex> visited = new HashSet<>();
dfs(graph, visited, res, startVet);
return res;
}
public static void main(String[] args) {
/* 初始化無向圖 */
Vertex[] v = Vertex.valsToVets(new int[] { 0, 1, 2, 3, 4, 5, 6 });
Vertex[][] edges = { { v[0], v[1] }, { v[0], v[3] }, { v[1], v[2] },
{ v[2], v[5] }, { v[4], v[5] }, { v[5], v[6] } };
GraphAdjList graph = new GraphAdjList(edges);
System.out.println("\n初始化後圖為");
graph.print();
/* 深度優先走訪 */
List<Vertex> res = graphDFS(graph, v[0]);
System.out.println("\n深度優先走訪DFS頂點序列為");
System.out.println(Vertex.vetsToVals(res));
}
}

View File

@@ -0,0 +1,55 @@
/**
* File: coin_change_greedy.java
* Created Time: 2023-07-20
* Author: krahets (krahets@163.com)
*/
package chapter_greedy;
import java.util.Arrays;
public class coin_change_greedy {
/* 零錢兌換:貪婪 */
static int coinChangeGreedy(int[] coins, int amt) {
// 假設 coins 串列有序
int i = coins.length - 1;
int count = 0;
// 迴圈進行貪婪選擇,直到無剩餘金額
while (amt > 0) {
// 找到小於且最接近剩餘金額的硬幣
while (i > 0 && coins[i] > amt) {
i--;
}
// 選擇 coins[i]
amt -= coins[i];
count++;
}
// 若未找到可行方案,則返回 -1
return amt == 0 ? count : -1;
}
public static void main(String[] args) {
// 貪婪:能夠保證找到全域性最優解
int[] coins = { 1, 5, 10, 20, 50, 100 };
int amt = 186;
int res = coinChangeGreedy(coins, amt);
System.out.println("\ncoins = " + Arrays.toString(coins) + ", amt = " + amt);
System.out.println("湊到 " + amt + " 所需的最少硬幣數量為 " + res);
// 貪婪:無法保證找到全域性最優解
coins = new int[] { 1, 20, 50 };
amt = 60;
res = coinChangeGreedy(coins, amt);
System.out.println("\ncoins = " + Arrays.toString(coins) + ", amt = " + amt);
System.out.println("湊到 " + amt + " 所需的最少硬幣數量為 " + res);
System.out.println("實際上需要的最少數量為 3 ,即 20 + 20 + 20");
// 貪婪:無法保證找到全域性最優解
coins = new int[] { 1, 49, 50 };
amt = 98;
res = coinChangeGreedy(coins, amt);
System.out.println("\ncoins = " + Arrays.toString(coins) + ", amt = " + amt);
System.out.println("湊到 " + amt + " 所需的最少硬幣數量為 " + res);
System.out.println("實際上需要的最少數量為 2 ,即 49 + 49");
}
}

View File

@@ -0,0 +1,59 @@
/**
* File: fractional_knapsack.java
* Created Time: 2023-07-20
* Author: krahets (krahets@163.com)
*/
package chapter_greedy;
import java.util.Arrays;
import java.util.Comparator;
/* 物品 */
class Item {
int w; // 物品重量
int v; // 物品價值
public Item(int w, int v) {
this.w = w;
this.v = v;
}
}
public class fractional_knapsack {
/* 分數背包:貪婪 */
static double fractionalKnapsack(int[] wgt, int[] val, int cap) {
// 建立物品串列,包含兩個屬性:重量、價值
Item[] items = new Item[wgt.length];
for (int i = 0; i < wgt.length; i++) {
items[i] = new Item(wgt[i], val[i]);
}
// 按照單位價值 item.v / item.w 從高到低進行排序
Arrays.sort(items, Comparator.comparingDouble(item -> -((double) item.v / item.w)));
// 迴圈貪婪選擇
double res = 0;
for (Item item : items) {
if (item.w <= cap) {
// 若剩餘容量充足,則將當前物品整個裝進背包
res += item.v;
cap -= item.w;
} else {
// 若剩餘容量不足,則將當前物品的一部分裝進背包
res += (double) item.v / item.w * cap;
// 已無剩餘容量,因此跳出迴圈
break;
}
}
return res;
}
public static void main(String[] args) {
int[] wgt = { 10, 20, 30, 40, 50 };
int[] val = { 50, 120, 150, 210, 240 };
int cap = 50;
// 貪婪演算法
double res = fractionalKnapsack(wgt, val, cap);
System.out.println("不超過背包容量的最大物品價值為 " + res);
}
}

View File

@@ -0,0 +1,38 @@
/**
* File: max_capacity.java
* Created Time: 2023-07-21
* Author: krahets (krahets@163.com)
*/
package chapter_greedy;
public class max_capacity {
/* 最大容量:貪婪 */
static int maxCapacity(int[] ht) {
// 初始化 i, j使其分列陣列兩端
int i = 0, j = ht.length - 1;
// 初始最大容量為 0
int res = 0;
// 迴圈貪婪選擇,直至兩板相遇
while (i < j) {
// 更新最大容量
int cap = Math.min(ht[i], ht[j]) * (j - i);
res = Math.max(res, cap);
// 向內移動短板
if (ht[i] < ht[j]) {
i++;
} else {
j--;
}
}
return res;
}
public static void main(String[] args) {
int[] ht = { 3, 8, 5, 2, 7, 7, 3, 4 };
// 貪婪演算法
int res = maxCapacity(ht);
System.out.println("最大容量為 " + res);
}
}

View File

@@ -0,0 +1,40 @@
/**
* File: max_product_cutting.java
* Created Time: 2023-07-21
* Author: krahets (krahets@163.com)
*/
package chapter_greedy;
import java.lang.Math;
public class max_product_cutting {
/* 最大切分乘積:貪婪 */
public static int maxProductCutting(int n) {
// 當 n <= 3 時,必須切分出一個 1
if (n <= 3) {
return 1 * (n - 1);
}
// 貪婪地切分出 3 a 為 3 的個數b 為餘數
int a = n / 3;
int b = n % 3;
if (b == 1) {
// 當餘數為 1 時,將一對 1 * 3 轉化為 2 * 2
return (int) Math.pow(3, a - 1) * 2 * 2;
}
if (b == 2) {
// 當餘數為 2 時,不做處理
return (int) Math.pow(3, a) * 2;
}
// 當餘數為 0 時,不做處理
return (int) Math.pow(3, a);
}
public static void main(String[] args) {
int n = 58;
// 貪婪演算法
int res = maxProductCutting(n);
System.out.println("最大切分乘積為 " + res);
}
}

View File

@@ -0,0 +1,141 @@
/**
* File: array_hash_map.java
* Created Time: 2022-12-04
* Author: krahets (krahets@163.com)
*/
package chapter_hashing;
import java.util.*;
/* 鍵值對 */
class Pair {
public int key;
public String val;
public Pair(int key, String val) {
this.key = key;
this.val = val;
}
}
/* 基於陣列實現的雜湊表 */
class ArrayHashMap {
private List<Pair> buckets;
public ArrayHashMap() {
// 初始化陣列,包含 100 個桶
buckets = new ArrayList<>();
for (int i = 0; i < 100; i++) {
buckets.add(null);
}
}
/* 雜湊函式 */
private int hashFunc(int key) {
int index = key % 100;
return index;
}
/* 查詢操作 */
public String get(int key) {
int index = hashFunc(key);
Pair pair = buckets.get(index);
if (pair == null)
return null;
return pair.val;
}
/* 新增操作 */
public void put(int key, String val) {
Pair pair = new Pair(key, val);
int index = hashFunc(key);
buckets.set(index, pair);
}
/* 刪除操作 */
public void remove(int key) {
int index = hashFunc(key);
// 置為 null ,代表刪除
buckets.set(index, null);
}
/* 獲取所有鍵值對 */
public List<Pair> pairSet() {
List<Pair> pairSet = new ArrayList<>();
for (Pair pair : buckets) {
if (pair != null)
pairSet.add(pair);
}
return pairSet;
}
/* 獲取所有鍵 */
public List<Integer> keySet() {
List<Integer> keySet = new ArrayList<>();
for (Pair pair : buckets) {
if (pair != null)
keySet.add(pair.key);
}
return keySet;
}
/* 獲取所有值 */
public List<String> valueSet() {
List<String> valueSet = new ArrayList<>();
for (Pair pair : buckets) {
if (pair != null)
valueSet.add(pair.val);
}
return valueSet;
}
/* 列印雜湊表 */
public void print() {
for (Pair kv : pairSet()) {
System.out.println(kv.key + " -> " + kv.val);
}
}
}
public class array_hash_map {
public static void main(String[] args) {
/* 初始化雜湊表 */
ArrayHashMap map = new ArrayHashMap();
/* 新增操作 */
// 在雜湊表中新增鍵值對 (key, value)
map.put(12836, "小哈");
map.put(15937, "小囉");
map.put(16750, "小算");
map.put(13276, "小法");
map.put(10583, "小鴨");
System.out.println("\n新增完成後雜湊表為\nKey -> Value");
map.print();
/* 查詢操作 */
// 向雜湊表中輸入鍵 key ,得到值 value
String name = map.get(15937);
System.out.println("\n輸入學號 15937 ,查詢到姓名 " + name);
/* 刪除操作 */
// 在雜湊表中刪除鍵值對 (key, value)
map.remove(10583);
System.out.println("\n刪除 10583 後,雜湊表為\nKey -> Value");
map.print();
/* 走訪雜湊表 */
System.out.println("\n走訪鍵值對 Key->Value");
for (Pair kv : map.pairSet()) {
System.out.println(kv.key + " -> " + kv.val);
}
System.out.println("\n單獨走訪鍵 Key");
for (int key : map.keySet()) {
System.out.println(key);
}
System.out.println("\n單獨走訪值 Value");
for (String val : map.valueSet()) {
System.out.println(val);
}
}
}

View File

@@ -0,0 +1,38 @@
/**
* File: built_in_hash.java
* Created Time: 2023-06-21
* Author: krahets (krahets@163.com)
*/
package chapter_hashing;
import utils.*;
import java.util.*;
public class built_in_hash {
public static void main(String[] args) {
int num = 3;
int hashNum = Integer.hashCode(num);
System.out.println("整數 " + num + " 的雜湊值為 " + hashNum);
boolean bol = true;
int hashBol = Boolean.hashCode(bol);
System.out.println("布林量 " + bol + " 的雜湊值為 " + hashBol);
double dec = 3.14159;
int hashDec = Double.hashCode(dec);
System.out.println("小數 " + dec + " 的雜湊值為 " + hashDec);
String str = "Hello 演算法";
int hashStr = str.hashCode();
System.out.println("字串 " + str + " 的雜湊值為 " + hashStr);
Object[] arr = { 12836, "小哈" };
int hashTup = Arrays.hashCode(arr);
System.out.println("陣列 " + Arrays.toString(arr) + " 的雜湊值為 " + hashTup);
ListNode obj = new ListNode(0);
int hashObj = obj.hashCode();
System.out.println("節點物件 " + obj + " 的雜湊值為 " + hashObj);
}
}

View File

@@ -0,0 +1,52 @@
/**
* File: hash_map.java
* Created Time: 2022-12-04
* Author: krahets (krahets@163.com)
*/
package chapter_hashing;
import java.util.*;
import utils.*;
public class hash_map {
public static void main(String[] args) {
/* 初始化雜湊表 */
Map<Integer, String> map = new HashMap<>();
/* 新增操作 */
// 在雜湊表中新增鍵值對 (key, value)
map.put(12836, "小哈");
map.put(15937, "小囉");
map.put(16750, "小算");
map.put(13276, "小法");
map.put(10583, "小鴨");
System.out.println("\n新增完成後雜湊表為\nKey -> Value");
PrintUtil.printHashMap(map);
/* 查詢操作 */
// 向雜湊表中輸入鍵 key ,得到值 value
String name = map.get(15937);
System.out.println("\n輸入學號 15937 ,查詢到姓名 " + name);
/* 刪除操作 */
// 在雜湊表中刪除鍵值對 (key, value)
map.remove(10583);
System.out.println("\n刪除 10583 後,雜湊表為\nKey -> Value");
PrintUtil.printHashMap(map);
/* 走訪雜湊表 */
System.out.println("\n走訪鍵值對 Key->Value");
for (Map.Entry<Integer, String> kv : map.entrySet()) {
System.out.println(kv.getKey() + " -> " + kv.getValue());
}
System.out.println("\n單獨走訪鍵 Key");
for (int key : map.keySet()) {
System.out.println(key);
}
System.out.println("\n單獨走訪值 Value");
for (String val : map.values()) {
System.out.println(val);
}
}
}

View File

@@ -0,0 +1,148 @@
/**
* File: hash_map_chaining.java
* Created Time: 2023-06-13
* Author: krahets (krahets@163.com)
*/
package chapter_hashing;
import java.util.ArrayList;
import java.util.List;
/* 鏈式位址雜湊表 */
class HashMapChaining {
int size; // 鍵值對數量
int capacity; // 雜湊表容量
double loadThres; // 觸發擴容的負載因子閾值
int extendRatio; // 擴容倍數
List<List<Pair>> buckets; // 桶陣列
/* 建構子 */
public HashMapChaining() {
size = 0;
capacity = 4;
loadThres = 2.0 / 3.0;
extendRatio = 2;
buckets = new ArrayList<>(capacity);
for (int i = 0; i < capacity; i++) {
buckets.add(new ArrayList<>());
}
}
/* 雜湊函式 */
int hashFunc(int key) {
return key % capacity;
}
/* 負載因子 */
double loadFactor() {
return (double) size / capacity;
}
/* 查詢操作 */
String get(int key) {
int index = hashFunc(key);
List<Pair> bucket = buckets.get(index);
// 走訪桶,若找到 key ,則返回對應 val
for (Pair pair : bucket) {
if (pair.key == key) {
return pair.val;
}
}
// 若未找到 key ,則返回 null
return null;
}
/* 新增操作 */
void put(int key, String val) {
// 當負載因子超過閾值時,執行擴容
if (loadFactor() > loadThres) {
extend();
}
int index = hashFunc(key);
List<Pair> bucket = buckets.get(index);
// 走訪桶,若遇到指定 key ,則更新對應 val 並返回
for (Pair pair : bucket) {
if (pair.key == key) {
pair.val = val;
return;
}
}
// 若無該 key ,則將鍵值對新增至尾部
Pair pair = new Pair(key, val);
bucket.add(pair);
size++;
}
/* 刪除操作 */
void remove(int key) {
int index = hashFunc(key);
List<Pair> bucket = buckets.get(index);
// 走訪桶,從中刪除鍵值對
for (Pair pair : bucket) {
if (pair.key == key) {
bucket.remove(pair);
size--;
break;
}
}
}
/* 擴容雜湊表 */
void extend() {
// 暫存原雜湊表
List<List<Pair>> bucketsTmp = buckets;
// 初始化擴容後的新雜湊表
capacity *= extendRatio;
buckets = new ArrayList<>(capacity);
for (int i = 0; i < capacity; i++) {
buckets.add(new ArrayList<>());
}
size = 0;
// 將鍵值對從原雜湊表搬運至新雜湊表
for (List<Pair> bucket : bucketsTmp) {
for (Pair pair : bucket) {
put(pair.key, pair.val);
}
}
}
/* 列印雜湊表 */
void print() {
for (List<Pair> bucket : buckets) {
List<String> res = new ArrayList<>();
for (Pair pair : bucket) {
res.add(pair.key + " -> " + pair.val);
}
System.out.println(res);
}
}
}
public class hash_map_chaining {
public static void main(String[] args) {
/* 初始化雜湊表 */
HashMapChaining map = new HashMapChaining();
/* 新增操作 */
// 在雜湊表中新增鍵值對 (key, value)
map.put(12836, "小哈");
map.put(15937, "小囉");
map.put(16750, "小算");
map.put(13276, "小法");
map.put(10583, "小鴨");
System.out.println("\n新增完成後雜湊表為\nKey -> Value");
map.print();
/* 查詢操作 */
// 向雜湊表中輸入鍵 key ,得到值 value
String name = map.get(13276);
System.out.println("\n輸入學號 13276 ,查詢到姓名 " + name);
/* 刪除操作 */
// 在雜湊表中刪除鍵值對 (key, value)
map.remove(12836);
System.out.println("\n刪除 12836 後,雜湊表為\nKey -> Value");
map.print();
}
}

View File

@@ -0,0 +1,158 @@
/**
* File: hash_map_open_addressing.java
* Created Time: 2023-06-13
* Author: krahets (krahets@163.com)
*/
package chapter_hashing;
/* 開放定址雜湊表 */
class HashMapOpenAddressing {
private int size; // 鍵值對數量
private int capacity = 4; // 雜湊表容量
private final double loadThres = 2.0 / 3.0; // 觸發擴容的負載因子閾值
private final int extendRatio = 2; // 擴容倍數
private Pair[] buckets; // 桶陣列
private final Pair TOMBSTONE = new Pair(-1, "-1"); // 刪除標記
/* 建構子 */
public HashMapOpenAddressing() {
size = 0;
buckets = new Pair[capacity];
}
/* 雜湊函式 */
private int hashFunc(int key) {
return key % capacity;
}
/* 負載因子 */
private double loadFactor() {
return (double) size / capacity;
}
/* 搜尋 key 對應的桶索引 */
private int findBucket(int key) {
int index = hashFunc(key);
int firstTombstone = -1;
// 線性探查,當遇到空桶時跳出
while (buckets[index] != null) {
// 若遇到 key ,返回對應的桶索引
if (buckets[index].key == key) {
// 若之前遇到了刪除標記,則將鍵值對移動至該索引處
if (firstTombstone != -1) {
buckets[firstTombstone] = buckets[index];
buckets[index] = TOMBSTONE;
return firstTombstone; // 返回移動後的桶索引
}
return index; // 返回桶索引
}
// 記錄遇到的首個刪除標記
if (firstTombstone == -1 && buckets[index] == TOMBSTONE) {
firstTombstone = index;
}
// 計算桶索引,越過尾部則返回頭部
index = (index + 1) % capacity;
}
// 若 key 不存在,則返回新增點的索引
return firstTombstone == -1 ? index : firstTombstone;
}
/* 查詢操作 */
public String get(int key) {
// 搜尋 key 對應的桶索引
int index = findBucket(key);
// 若找到鍵值對,則返回對應 val
if (buckets[index] != null && buckets[index] != TOMBSTONE) {
return buckets[index].val;
}
// 若鍵值對不存在,則返回 null
return null;
}
/* 新增操作 */
public void put(int key, String val) {
// 當負載因子超過閾值時,執行擴容
if (loadFactor() > loadThres) {
extend();
}
// 搜尋 key 對應的桶索引
int index = findBucket(key);
// 若找到鍵值對,則覆蓋 val 並返回
if (buckets[index] != null && buckets[index] != TOMBSTONE) {
buckets[index].val = val;
return;
}
// 若鍵值對不存在,則新增該鍵值對
buckets[index] = new Pair(key, val);
size++;
}
/* 刪除操作 */
public void remove(int key) {
// 搜尋 key 對應的桶索引
int index = findBucket(key);
// 若找到鍵值對,則用刪除標記覆蓋它
if (buckets[index] != null && buckets[index] != TOMBSTONE) {
buckets[index] = TOMBSTONE;
size--;
}
}
/* 擴容雜湊表 */
private void extend() {
// 暫存原雜湊表
Pair[] bucketsTmp = buckets;
// 初始化擴容後的新雜湊表
capacity *= extendRatio;
buckets = new Pair[capacity];
size = 0;
// 將鍵值對從原雜湊表搬運至新雜湊表
for (Pair pair : bucketsTmp) {
if (pair != null && pair != TOMBSTONE) {
put(pair.key, pair.val);
}
}
}
/* 列印雜湊表 */
public void print() {
for (Pair pair : buckets) {
if (pair == null) {
System.out.println("null");
} else if (pair == TOMBSTONE) {
System.out.println("TOMBSTONE");
} else {
System.out.println(pair.key + " -> " + pair.val);
}
}
}
}
public class hash_map_open_addressing {
public static void main(String[] args) {
// 初始化雜湊表
HashMapOpenAddressing hashmap = new HashMapOpenAddressing();
// 新增操作
// 在雜湊表中新增鍵值對 (key, val)
hashmap.put(12836, "小哈");
hashmap.put(15937, "小囉");
hashmap.put(16750, "小算");
hashmap.put(13276, "小法");
hashmap.put(10583, "小鴨");
System.out.println("\n新增完成後雜湊表為\nKey -> Value");
hashmap.print();
// 查詢操作
// 向雜湊表中輸入鍵 key ,得到值 val
String name = hashmap.get(13276);
System.out.println("\n輸入學號 13276 ,查詢到姓名 " + name);
// 刪除操作
// 在雜湊表中刪除鍵值對 (key, val)
hashmap.remove(16750);
System.out.println("\n刪除 16750 後,雜湊表為\nKey -> Value");
hashmap.print();
}
}

View File

@@ -0,0 +1,65 @@
/**
* File: simple_hash.java
* Created Time: 2023-06-21
* Author: krahets (krahets@163.com)
*/
package chapter_hashing;
public class simple_hash {
/* 加法雜湊 */
static int addHash(String key) {
long hash = 0;
final int MODULUS = 1000000007;
for (char c : key.toCharArray()) {
hash = (hash + (int) c) % MODULUS;
}
return (int) hash;
}
/* 乘法雜湊 */
static int mulHash(String key) {
long hash = 0;
final int MODULUS = 1000000007;
for (char c : key.toCharArray()) {
hash = (31 * hash + (int) c) % MODULUS;
}
return (int) hash;
}
/* 互斥或雜湊 */
static int xorHash(String key) {
int hash = 0;
final int MODULUS = 1000000007;
for (char c : key.toCharArray()) {
hash ^= (int) c;
}
return hash & MODULUS;
}
/* 旋轉雜湊 */
static int rotHash(String key) {
long hash = 0;
final int MODULUS = 1000000007;
for (char c : key.toCharArray()) {
hash = ((hash << 4) ^ (hash >> 28) ^ (int) c) % MODULUS;
}
return (int) hash;
}
public static void main(String[] args) {
String key = "Hello 演算法";
int hash = addHash(key);
System.out.println("加法雜湊值為 " + hash);
hash = mulHash(key);
System.out.println("乘法雜湊值為 " + hash);
hash = xorHash(key);
System.out.println("互斥或雜湊值為 " + hash);
hash = rotHash(key);
System.out.println("旋轉雜湊值為 " + hash);
}
}

View File

@@ -0,0 +1,66 @@
/**
* File: heap.java
* Created Time: 2023-01-07
* Author: krahets (krahets@163.com)
*/
package chapter_heap;
import utils.*;
import java.util.*;
public class heap {
public static void testPush(Queue<Integer> heap, int val) {
heap.offer(val); // 元素入堆積
System.out.format("\n元素 %d 入堆積後\n", val);
PrintUtil.printHeap(heap);
}
public static void testPop(Queue<Integer> heap) {
int val = heap.poll(); // 堆積頂元素出堆積
System.out.format("\n堆積頂元素 %d 出堆積後\n", val);
PrintUtil.printHeap(heap);
}
public static void main(String[] args) {
/* 初始化堆積 */
// 初始化小頂堆積
Queue<Integer> minHeap = new PriorityQueue<>();
// 初始化大頂堆積(使用 lambda 表示式修改 Comparator 即可)
Queue<Integer> maxHeap = new PriorityQueue<>((a, b) -> b - a);
System.out.println("\n以下測試樣例為大頂堆積");
/* 元素入堆積 */
testPush(maxHeap, 1);
testPush(maxHeap, 3);
testPush(maxHeap, 2);
testPush(maxHeap, 5);
testPush(maxHeap, 4);
/* 獲取堆積頂元素 */
int peek = maxHeap.peek();
System.out.format("\n堆積頂元素為 %d\n", peek);
/* 堆積頂元素出堆積 */
testPop(maxHeap);
testPop(maxHeap);
testPop(maxHeap);
testPop(maxHeap);
testPop(maxHeap);
/* 獲取堆積大小 */
int size = maxHeap.size();
System.out.format("\n堆積元素數量為 %d\n", size);
/* 判斷堆積是否為空 */
boolean isEmpty = maxHeap.isEmpty();
System.out.format("\n堆積是否為空 %b\n", isEmpty);
/* 輸入串列並建堆積 */
// 時間複雜度為 O(n) ,而非 O(nlogn)
minHeap = new PriorityQueue<>(Arrays.asList(1, 3, 2, 5, 4));
System.out.println("\n輸入串列並建立小頂堆積後");
PrintUtil.printHeap(minHeap);
}
}

View File

@@ -0,0 +1,159 @@
/**
* File: my_heap.java
* Created Time: 2023-01-07
* Author: krahets (krahets@163.com)
*/
package chapter_heap;
import utils.*;
import java.util.*;
/* 大頂堆積 */
class MaxHeap {
// 使用串列而非陣列,這樣無須考慮擴容問題
private List<Integer> maxHeap;
/* 建構子,根據輸入串列建堆積 */
public MaxHeap(List<Integer> nums) {
// 將串列元素原封不動新增進堆積
maxHeap = new ArrayList<>(nums);
// 堆積化除葉節點以外的其他所有節點
for (int i = parent(size() - 1); i >= 0; i--) {
siftDown(i);
}
}
/* 獲取左子節點的索引 */
private int left(int i) {
return 2 * i + 1;
}
/* 獲取右子節點的索引 */
private int right(int i) {
return 2 * i + 2;
}
/* 獲取父節點的索引 */
private int parent(int i) {
return (i - 1) / 2; // 向下整除
}
/* 交換元素 */
private void swap(int i, int j) {
int tmp = maxHeap.get(i);
maxHeap.set(i, maxHeap.get(j));
maxHeap.set(j, tmp);
}
/* 獲取堆積大小 */
public int size() {
return maxHeap.size();
}
/* 判斷堆積是否為空 */
public boolean isEmpty() {
return size() == 0;
}
/* 訪問堆積頂元素 */
public int peek() {
return maxHeap.get(0);
}
/* 元素入堆積 */
public void push(int val) {
// 新增節點
maxHeap.add(val);
// 從底至頂堆積化
siftUp(size() - 1);
}
/* 從節點 i 開始,從底至頂堆積化 */
private void siftUp(int i) {
while (true) {
// 獲取節點 i 的父節點
int p = parent(i);
// 當“越過根節點”或“節點無須修復”時,結束堆積化
if (p < 0 || maxHeap.get(i) <= maxHeap.get(p))
break;
// 交換兩節點
swap(i, p);
// 迴圈向上堆積化
i = p;
}
}
/* 元素出堆積 */
public int pop() {
// 判空處理
if (isEmpty())
throw new IndexOutOfBoundsException();
// 交換根節點與最右葉節點(交換首元素與尾元素)
swap(0, size() - 1);
// 刪除節點
int val = maxHeap.remove(size() - 1);
// 從頂至底堆積化
siftDown(0);
// 返回堆積頂元素
return val;
}
/* 從節點 i 開始,從頂至底堆積化 */
private void siftDown(int i) {
while (true) {
// 判斷節點 i, l, r 中值最大的節點,記為 ma
int l = left(i), r = right(i), ma = i;
if (l < size() && maxHeap.get(l) > maxHeap.get(ma))
ma = l;
if (r < size() && maxHeap.get(r) > maxHeap.get(ma))
ma = r;
// 若節點 i 最大或索引 l, r 越界,則無須繼續堆積化,跳出
if (ma == i)
break;
// 交換兩節點
swap(i, ma);
// 迴圈向下堆積化
i = ma;
}
}
/* 列印堆積(二元樹) */
public void print() {
Queue<Integer> queue = new PriorityQueue<>((a, b) -> { return b - a; });
queue.addAll(maxHeap);
PrintUtil.printHeap(queue);
}
}
public class my_heap {
public static void main(String[] args) {
/* 初始化大頂堆積 */
MaxHeap maxHeap = new MaxHeap(Arrays.asList(9, 8, 6, 6, 7, 5, 2, 1, 4, 3, 6, 2));
System.out.println("\n輸入串列並建堆積後");
maxHeap.print();
/* 獲取堆積頂元素 */
int peek = maxHeap.peek();
System.out.format("\n堆積頂元素為 %d\n", peek);
/* 元素入堆積 */
int val = 7;
maxHeap.push(val);
System.out.format("\n元素 %d 入堆積後\n", val);
maxHeap.print();
/* 堆積頂元素出堆積 */
peek = maxHeap.pop();
System.out.format("\n堆積頂元素 %d 出堆積後\n", peek);
maxHeap.print();
/* 獲取堆積大小 */
int size = maxHeap.size();
System.out.format("\n堆積元素數量為 %d\n", size);
/* 判斷堆積是否為空 */
boolean isEmpty = maxHeap.isEmpty();
System.out.format("\n堆積是否為空 %b\n", isEmpty);
}
}

View File

@@ -0,0 +1,40 @@
/**
* File: top_k.java
* Created Time: 2023-06-12
* Author: krahets (krahets@163.com)
*/
package chapter_heap;
import utils.*;
import java.util.*;
public class top_k {
/* 基於堆積查詢陣列中最大的 k 個元素 */
static Queue<Integer> topKHeap(int[] nums, int k) {
// 初始化小頂堆積
Queue<Integer> heap = new PriorityQueue<Integer>();
// 將陣列的前 k 個元素入堆積
for (int i = 0; i < k; i++) {
heap.offer(nums[i]);
}
// 從第 k+1 個元素開始,保持堆積的長度為 k
for (int i = k; i < nums.length; i++) {
// 若當前元素大於堆積頂元素,則將堆積頂元素出堆積、當前元素入堆積
if (nums[i] > heap.peek()) {
heap.poll();
heap.offer(nums[i]);
}
}
return heap;
}
public static void main(String[] args) {
int[] nums = { 1, 7, 6, 3, 2 };
int k = 3;
Queue<Integer> res = topKHeap(nums, k);
System.out.println("最大的 " + k + " 個元素為");
PrintUtil.printHeap(res);
}
}

View File

@@ -0,0 +1,58 @@
/**
* File: binary_search.java
* Created Time: 2022-11-25
* Author: krahets (krahets@163.com)
*/
package chapter_searching;
public class binary_search {
/* 二分搜尋(雙閉區間) */
static int binarySearch(int[] nums, int target) {
// 初始化雙閉區間 [0, n-1] ,即 i, j 分別指向陣列首元素、尾元素
int i = 0, j = nums.length - 1;
// 迴圈,當搜尋區間為空時跳出(當 i > j 時為空)
while (i <= j) {
int m = i + (j - i) / 2; // 計算中點索引 m
if (nums[m] < target) // 此情況說明 target 在區間 [m+1, j] 中
i = m + 1;
else if (nums[m] > target) // 此情況說明 target 在區間 [i, m-1] 中
j = m - 1;
else // 找到目標元素,返回其索引
return m;
}
// 未找到目標元素,返回 -1
return -1;
}
/* 二分搜尋(左閉右開區間) */
static int binarySearchLCRO(int[] nums, int target) {
// 初始化左閉右開區間 [0, n) ,即 i, j 分別指向陣列首元素、尾元素+1
int i = 0, j = nums.length;
// 迴圈,當搜尋區間為空時跳出(當 i = j 時為空)
while (i < j) {
int m = i + (j - i) / 2; // 計算中點索引 m
if (nums[m] < target) // 此情況說明 target 在區間 [m+1, j) 中
i = m + 1;
else if (nums[m] > target) // 此情況說明 target 在區間 [i, m) 中
j = m;
else // 找到目標元素,返回其索引
return m;
}
// 未找到目標元素,返回 -1
return -1;
}
public static void main(String[] args) {
int target = 6;
int[] nums = { 1, 3, 6, 8, 12, 15, 23, 26, 31, 35 };
/* 二分搜尋(雙閉區間) */
int index = binarySearch(nums, target);
System.out.println("目標元素 6 的索引 = " + index);
/* 二分搜尋(左閉右開區間) */
index = binarySearchLCRO(nums, target);
System.out.println("目標元素 6 的索引 = " + index);
}
}

View File

@@ -0,0 +1,49 @@
/**
* File: binary_search_edge.java
* Created Time: 2023-08-04
* Author: krahets (krahets@163.com)
*/
package chapter_searching;
public class binary_search_edge {
/* 二分搜尋最左一個 target */
static int binarySearchLeftEdge(int[] nums, int target) {
// 等價於查詢 target 的插入點
int i = binary_search_insertion.binarySearchInsertion(nums, target);
// 未找到 target ,返回 -1
if (i == nums.length || nums[i] != target) {
return -1;
}
// 找到 target ,返回索引 i
return i;
}
/* 二分搜尋最右一個 target */
static int binarySearchRightEdge(int[] nums, int target) {
// 轉化為查詢最左一個 target + 1
int i = binary_search_insertion.binarySearchInsertion(nums, target + 1);
// j 指向最右一個 target i 指向首個大於 target 的元素
int j = i - 1;
// 未找到 target ,返回 -1
if (j == -1 || nums[j] != target) {
return -1;
}
// 找到 target ,返回索引 j
return j;
}
public static void main(String[] args) {
// 包含重複元素的陣列
int[] nums = { 1, 3, 6, 6, 6, 6, 6, 10, 12, 15 };
System.out.println("\n陣列 nums = " + java.util.Arrays.toString(nums));
// 二分搜尋左邊界和右邊界
for (int target : new int[] { 6, 7 }) {
int index = binarySearchLeftEdge(nums, target);
System.out.println("最左一個元素 " + target + " 的索引為 " + index);
index = binarySearchRightEdge(nums, target);
System.out.println("最右一個元素 " + target + " 的索引為 " + index);
}
}
}

View File

@@ -0,0 +1,63 @@
/**
* File: binary_search_insertion.java
* Created Time: 2023-08-04
* Author: krahets (krahets@163.com)
*/
package chapter_searching;
class binary_search_insertion {
/* 二分搜尋插入點(無重複元素) */
static int binarySearchInsertionSimple(int[] nums, int target) {
int i = 0, j = nums.length - 1; // 初始化雙閉區間 [0, n-1]
while (i <= j) {
int m = i + (j - i) / 2; // 計算中點索引 m
if (nums[m] < target) {
i = m + 1; // target 在區間 [m+1, j] 中
} else if (nums[m] > target) {
j = m - 1; // target 在區間 [i, m-1] 中
} else {
return m; // 找到 target ,返回插入點 m
}
}
// 未找到 target ,返回插入點 i
return i;
}
/* 二分搜尋插入點(存在重複元素) */
static int binarySearchInsertion(int[] nums, int target) {
int i = 0, j = nums.length - 1; // 初始化雙閉區間 [0, n-1]
while (i <= j) {
int m = i + (j - i) / 2; // 計算中點索引 m
if (nums[m] < target) {
i = m + 1; // target 在區間 [m+1, j] 中
} else if (nums[m] > target) {
j = m - 1; // target 在區間 [i, m-1] 中
} else {
j = m - 1; // 首個小於 target 的元素在區間 [i, m-1] 中
}
}
// 返回插入點 i
return i;
}
public static void main(String[] args) {
// 無重複元素的陣列
int[] nums = { 1, 3, 6, 8, 12, 15, 23, 26, 31, 35 };
System.out.println("\n陣列 nums = " + java.util.Arrays.toString(nums));
// 二分搜尋插入點
for (int target : new int[] { 6, 9 }) {
int index = binarySearchInsertionSimple(nums, target);
System.out.println("元素 " + target + " 的插入點的索引為 " + index);
}
// 包含重複元素的陣列
nums = new int[] { 1, 3, 6, 6, 6, 6, 6, 10, 12, 15 };
System.out.println("\n陣列 nums = " + java.util.Arrays.toString(nums));
// 二分搜尋插入點
for (int target : new int[] { 2, 6, 20 }) {
int index = binarySearchInsertion(nums, target);
System.out.println("元素 " + target + " 的插入點的索引為 " + index);
}
}
}

View File

@@ -0,0 +1,51 @@
/**
* File: hashing_search.java
* Created Time: 2022-11-25
* Author: krahets (krahets@163.com)
*/
package chapter_searching;
import utils.*;
import java.util.*;
public class hashing_search {
/* 雜湊查詢(陣列) */
static int hashingSearchArray(Map<Integer, Integer> map, int target) {
// 雜湊表的 key: 目標元素value: 索引
// 若雜湊表中無此 key ,返回 -1
return map.getOrDefault(target, -1);
}
/* 雜湊查詢(鏈結串列) */
static ListNode hashingSearchLinkedList(Map<Integer, ListNode> map, int target) {
// 雜湊表的 key: 目標節點值value: 節點物件
// 若雜湊表中無此 key ,返回 null
return map.getOrDefault(target, null);
}
public static void main(String[] args) {
int target = 3;
/* 雜湊查詢(陣列) */
int[] nums = { 1, 5, 3, 2, 4, 7, 5, 9, 10, 8 };
// 初始化雜湊表
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
map.put(nums[i], i); // key: 元素value: 索引
}
int index = hashingSearchArray(map, target);
System.out.println("目標元素 3 的索引 = " + index);
/* 雜湊查詢(鏈結串列) */
ListNode head = ListNode.arrToLinkedList(nums);
// 初始化雜湊表
Map<Integer, ListNode> map1 = new HashMap<>();
while (head != null) {
map1.put(head.val, head); // key: 節點值value: 節點
head = head.next;
}
ListNode node = hashingSearchLinkedList(map1, target);
System.out.println("目標節點值 3 的對應節點物件為 " + node);
}
}

View File

@@ -0,0 +1,50 @@
/**
* File: linear_search.java
* Created Time: 2022-11-25
* Author: krahets (krahets@163.com)
*/
package chapter_searching;
import utils.*;
public class linear_search {
/* 線性查詢(陣列) */
static int linearSearchArray(int[] nums, int target) {
// 走訪陣列
for (int i = 0; i < nums.length; i++) {
// 找到目標元素,返回其索引
if (nums[i] == target)
return i;
}
// 未找到目標元素,返回 -1
return -1;
}
/* 線性查詢(鏈結串列) */
static ListNode linearSearchLinkedList(ListNode head, int target) {
// 走訪鏈結串列
while (head != null) {
// 找到目標節點,返回之
if (head.val == target)
return head;
head = head.next;
}
// 未找到目標節點,返回 null
return null;
}
public static void main(String[] args) {
int target = 3;
/* 在陣列中執行線性查詢 */
int[] nums = { 1, 5, 3, 2, 4, 7, 5, 9, 10, 8 };
int index = linearSearchArray(nums, target);
System.out.println("目標元素 3 的索引 = " + index);
/* 在鏈結串列中執行線性查詢 */
ListNode head = ListNode.arrToLinkedList(nums);
ListNode node = linearSearchLinkedList(head, target);
System.out.println("目標節點值 3 的對應節點物件為 " + node);
}
}

View File

@@ -0,0 +1,53 @@
/**
* File: two_sum.java
* Created Time: 2022-11-25
* Author: krahets (krahets@163.com)
*/
package chapter_searching;
import java.util.*;
public class two_sum {
/* 方法一:暴力列舉 */
static int[] twoSumBruteForce(int[] nums, int target) {
int size = nums.length;
// 兩層迴圈,時間複雜度為 O(n^2)
for (int i = 0; i < size - 1; i++) {
for (int j = i + 1; j < size; j++) {
if (nums[i] + nums[j] == target)
return new int[] { i, j };
}
}
return new int[0];
}
/* 方法二:輔助雜湊表 */
static int[] twoSumHashTable(int[] nums, int target) {
int size = nums.length;
// 輔助雜湊表,空間複雜度為 O(n)
Map<Integer, Integer> dic = new HashMap<>();
// 單層迴圈,時間複雜度為 O(n)
for (int i = 0; i < size; i++) {
if (dic.containsKey(target - nums[i])) {
return new int[] { dic.get(target - nums[i]), i };
}
dic.put(nums[i], i);
}
return new int[0];
}
public static void main(String[] args) {
// ======= Test Case =======
int[] nums = { 2, 7, 11, 15 };
int target = 13;
// ====== Driver Code ======
// 方法一
int[] res = twoSumBruteForce(nums, target);
System.out.println("方法一 res = " + Arrays.toString(res));
// 方法二
res = twoSumHashTable(nums, target);
System.out.println("方法二 res = " + Arrays.toString(res));
}
}

View File

@@ -0,0 +1,57 @@
/**
* File: bubble_sort.java
* Created Time: 2022-11-25
* Author: krahets (krahets@163.com)
*/
package chapter_sorting;
import java.util.*;
public class bubble_sort {
/* 泡沫排序 */
static void bubbleSort(int[] nums) {
// 外迴圈:未排序區間為 [0, i]
for (int i = nums.length - 1; i > 0; i--) {
// 內迴圈:將未排序區間 [0, i] 中的最大元素交換至該區間的最右端
for (int j = 0; j < i; j++) {
if (nums[j] > nums[j + 1]) {
// 交換 nums[j] 與 nums[j + 1]
int tmp = nums[j];
nums[j] = nums[j + 1];
nums[j + 1] = tmp;
}
}
}
}
/* 泡沫排序(標誌最佳化) */
static void bubbleSortWithFlag(int[] nums) {
// 外迴圈:未排序區間為 [0, i]
for (int i = nums.length - 1; i > 0; i--) {
boolean flag = false; // 初始化標誌位
// 內迴圈:將未排序區間 [0, i] 中的最大元素交換至該區間的最右端
for (int j = 0; j < i; j++) {
if (nums[j] > nums[j + 1]) {
// 交換 nums[j] 與 nums[j + 1]
int tmp = nums[j];
nums[j] = nums[j + 1];
nums[j + 1] = tmp;
flag = true; // 記錄交換元素
}
}
if (!flag)
break; // 此輪“冒泡”未交換任何元素,直接跳出
}
}
public static void main(String[] args) {
int[] nums = { 4, 1, 3, 1, 5, 2 };
bubbleSort(nums);
System.out.println("泡沫排序完成後 nums = " + Arrays.toString(nums));
int[] nums1 = { 4, 1, 3, 1, 5, 2 };
bubbleSortWithFlag(nums1);
System.out.println("泡沫排序完成後 nums1 = " + Arrays.toString(nums1));
}
}

View File

@@ -0,0 +1,47 @@
/**
* File: bucket_sort.java
* Created Time: 2023-03-17
* Author: krahets (krahets@163.com)
*/
package chapter_sorting;
import java.util.*;
public class bucket_sort {
/* 桶排序 */
static void bucketSort(float[] nums) {
// 初始化 k = n/2 個桶,預期向每個桶分配 2 個元素
int k = nums.length / 2;
List<List<Float>> buckets = new ArrayList<>();
for (int i = 0; i < k; i++) {
buckets.add(new ArrayList<>());
}
// 1. 將陣列元素分配到各個桶中
for (float num : nums) {
// 輸入資料範圍為 [0, 1),使用 num * k 對映到索引範圍 [0, k-1]
int i = (int) (num * k);
// 將 num 新增進桶 i
buckets.get(i).add(num);
}
// 2. 對各個桶執行排序
for (List<Float> bucket : buckets) {
// 使用內建排序函式,也可以替換成其他排序演算法
Collections.sort(bucket);
}
// 3. 走訪桶合併結果
int i = 0;
for (List<Float> bucket : buckets) {
for (float num : bucket) {
nums[i++] = num;
}
}
}
public static void main(String[] args) {
// 設輸入資料為浮點數,範圍為 [0, 1)
float[] nums = { 0.49f, 0.96f, 0.82f, 0.09f, 0.57f, 0.43f, 0.91f, 0.75f, 0.15f, 0.37f };
bucketSort(nums);
System.out.println("桶排序完成後 nums = " + Arrays.toString(nums));
}
}

View File

@@ -0,0 +1,78 @@
/**
* File: counting_sort.java
* Created Time: 2023-03-17
* Author: krahets (krahets@163.com)
*/
package chapter_sorting;
import java.util.*;
public class counting_sort {
/* 計數排序 */
// 簡單實現,無法用於排序物件
static void countingSortNaive(int[] nums) {
// 1. 統計陣列最大元素 m
int m = 0;
for (int num : nums) {
m = Math.max(m, num);
}
// 2. 統計各數字的出現次數
// counter[num] 代表 num 的出現次數
int[] counter = new int[m + 1];
for (int num : nums) {
counter[num]++;
}
// 3. 走訪 counter ,將各元素填入原陣列 nums
int i = 0;
for (int num = 0; num < m + 1; num++) {
for (int j = 0; j < counter[num]; j++, i++) {
nums[i] = num;
}
}
}
/* 計數排序 */
// 完整實現,可排序物件,並且是穩定排序
static void countingSort(int[] nums) {
// 1. 統計陣列最大元素 m
int m = 0;
for (int num : nums) {
m = Math.max(m, num);
}
// 2. 統計各數字的出現次數
// counter[num] 代表 num 的出現次數
int[] counter = new int[m + 1];
for (int num : nums) {
counter[num]++;
}
// 3. 求 counter 的前綴和,將“出現次數”轉換為“尾索引”
// 即 counter[num]-1 是 num 在 res 中最後一次出現的索引
for (int i = 0; i < m; i++) {
counter[i + 1] += counter[i];
}
// 4. 倒序走訪 nums ,將各元素填入結果陣列 res
// 初始化陣列 res 用於記錄結果
int n = nums.length;
int[] res = new int[n];
for (int i = n - 1; i >= 0; i--) {
int num = nums[i];
res[counter[num] - 1] = num; // 將 num 放置到對應索引處
counter[num]--; // 令前綴和自減 1 ,得到下次放置 num 的索引
}
// 使用結果陣列 res 覆蓋原陣列 nums
for (int i = 0; i < n; i++) {
nums[i] = res[i];
}
}
public static void main(String[] args) {
int[] nums = { 1, 0, 1, 2, 0, 4, 0, 2, 2, 4 };
countingSortNaive(nums);
System.out.println("計數排序(無法排序物件)完成後 nums = " + Arrays.toString(nums));
int[] nums1 = { 1, 0, 1, 2, 0, 4, 0, 2, 2, 4 };
countingSort(nums1);
System.out.println("計數排序完成後 nums1 = " + Arrays.toString(nums1));
}
}

View File

@@ -0,0 +1,57 @@
/**
* File: heap_sort.java
* Created Time: 2023-05-26
* Author: krahets (krahets@163.com)
*/
package chapter_sorting;
import java.util.Arrays;
public class heap_sort {
/* 堆積的長度為 n ,從節點 i 開始,從頂至底堆積化 */
public static void siftDown(int[] nums, int n, int i) {
while (true) {
// 判斷節點 i, l, r 中值最大的節點,記為 ma
int l = 2 * i + 1;
int r = 2 * i + 2;
int ma = i;
if (l < n && nums[l] > nums[ma])
ma = l;
if (r < n && nums[r] > nums[ma])
ma = r;
// 若節點 i 最大或索引 l, r 越界,則無須繼續堆積化,跳出
if (ma == i)
break;
// 交換兩節點
int temp = nums[i];
nums[i] = nums[ma];
nums[ma] = temp;
// 迴圈向下堆積化
i = ma;
}
}
/* 堆積排序 */
public static void heapSort(int[] nums) {
// 建堆積操作:堆積化除葉節點以外的其他所有節點
for (int i = nums.length / 2 - 1; i >= 0; i--) {
siftDown(nums, nums.length, i);
}
// 從堆積中提取最大元素,迴圈 n-1 輪
for (int i = nums.length - 1; i > 0; i--) {
// 交換根節點與最右葉節點(交換首元素與尾元素)
int tmp = nums[0];
nums[0] = nums[i];
nums[i] = tmp;
// 以根節點為起點,從頂至底進行堆積化
siftDown(nums, i, 0);
}
}
public static void main(String[] args) {
int[] nums = { 4, 1, 3, 1, 5, 2 };
heapSort(nums);
System.out.println("堆積排序完成後 nums = " + Arrays.toString(nums));
}
}

View File

@@ -0,0 +1,31 @@
/**
* File: insertion_sort.java
* Created Time: 2022-11-25
* Author: krahets (krahets@163.com)
*/
package chapter_sorting;
import java.util.*;
public class insertion_sort {
/* 插入排序 */
static void insertionSort(int[] nums) {
// 外迴圈:已排序區間為 [0, i-1]
for (int i = 1; i < nums.length; i++) {
int base = nums[i], j = i - 1;
// 內迴圈:將 base 插入到已排序區間 [0, i-1] 中的正確位置
while (j >= 0 && nums[j] > base) {
nums[j + 1] = nums[j]; // 將 nums[j] 向右移動一位
j--;
}
nums[j + 1] = base; // 將 base 賦值到正確位置
}
}
public static void main(String[] args) {
int[] nums = { 4, 1, 3, 1, 5, 2 };
insertionSort(nums);
System.out.println("插入排序完成後 nums = " + Arrays.toString(nums));
}
}

View File

@@ -0,0 +1,58 @@
/**
* File: merge_sort.java
* Created Time: 2022-11-25
* Author: krahets (krahets@163.com)
*/
package chapter_sorting;
import java.util.*;
public class merge_sort {
/* 合併左子陣列和右子陣列 */
static void merge(int[] nums, int left, int mid, int right) {
// 左子陣列區間為 [left, mid], 右子陣列區間為 [mid+1, right]
// 建立一個臨時陣列 tmp ,用於存放合併後的結果
int[] tmp = new int[right - left + 1];
// 初始化左子陣列和右子陣列的起始索引
int i = left, j = mid + 1, k = 0;
// 當左右子陣列都還有元素時,進行比較並將較小的元素複製到臨時陣列中
while (i <= mid && j <= right) {
if (nums[i] <= nums[j])
tmp[k++] = nums[i++];
else
tmp[k++] = nums[j++];
}
// 將左子陣列和右子陣列的剩餘元素複製到臨時陣列中
while (i <= mid) {
tmp[k++] = nums[i++];
}
while (j <= right) {
tmp[k++] = nums[j++];
}
// 將臨時陣列 tmp 中的元素複製回原陣列 nums 的對應區間
for (k = 0; k < tmp.length; k++) {
nums[left + k] = tmp[k];
}
}
/* 合併排序 */
static void mergeSort(int[] nums, int left, int right) {
// 終止條件
if (left >= right)
return; // 當子陣列長度為 1 時終止遞迴
// 劃分階段
int mid = (left + right) / 2; // 計算中點
mergeSort(nums, left, mid); // 遞迴左子陣列
mergeSort(nums, mid + 1, right); // 遞迴右子陣列
// 合併階段
merge(nums, left, mid, right);
}
public static void main(String[] args) {
/* 合併排序 */
int[] nums = { 7, 3, 2, 6, 0, 1, 5, 4 };
mergeSort(nums, 0, nums.length - 1);
System.out.println("合併排序完成後 nums = " + Arrays.toString(nums));
}
}

View File

@@ -0,0 +1,158 @@
/**
* File: quick_sort.java
* Created Time: 2022-11-25
* Author: krahets (krahets@163.com)
*/
package chapter_sorting;
import java.util.*;
/* 快速排序類別 */
class QuickSort {
/* 元素交換 */
static void swap(int[] nums, int i, int j) {
int tmp = nums[i];
nums[i] = nums[j];
nums[j] = tmp;
}
/* 哨兵劃分 */
static int partition(int[] nums, int left, int right) {
// 以 nums[left] 為基準數
int i = left, j = right;
while (i < j) {
while (i < j && nums[j] >= nums[left])
j--; // 從右向左找首個小於基準數的元素
while (i < j && nums[i] <= nums[left])
i++; // 從左向右找首個大於基準數的元素
swap(nums, i, j); // 交換這兩個元素
}
swap(nums, i, left); // 將基準數交換至兩子陣列的分界線
return i; // 返回基準數的索引
}
/* 快速排序 */
public static void quickSort(int[] nums, int left, int right) {
// 子陣列長度為 1 時終止遞迴
if (left >= right)
return;
// 哨兵劃分
int pivot = partition(nums, left, right);
// 遞迴左子陣列、右子陣列
quickSort(nums, left, pivot - 1);
quickSort(nums, pivot + 1, right);
}
}
/* 快速排序類別(中位基準數最佳化) */
class QuickSortMedian {
/* 元素交換 */
static void swap(int[] nums, int i, int j) {
int tmp = nums[i];
nums[i] = nums[j];
nums[j] = tmp;
}
/* 選取三個候選元素的中位數 */
static int medianThree(int[] nums, int left, int mid, int right) {
int l = nums[left], m = nums[mid], r = nums[right];
if ((l <= m && m <= r) || (r <= m && m <= l))
return mid; // m 在 l 和 r 之間
if ((m <= l && l <= r) || (r <= l && l <= m))
return left; // l 在 m 和 r 之間
return right;
}
/* 哨兵劃分(三數取中值) */
static int partition(int[] nums, int left, int right) {
// 選取三個候選元素的中位數
int med = medianThree(nums, left, (left + right) / 2, right);
// 將中位數交換至陣列最左端
swap(nums, left, med);
// 以 nums[left] 為基準數
int i = left, j = right;
while (i < j) {
while (i < j && nums[j] >= nums[left])
j--; // 從右向左找首個小於基準數的元素
while (i < j && nums[i] <= nums[left])
i++; // 從左向右找首個大於基準數的元素
swap(nums, i, j); // 交換這兩個元素
}
swap(nums, i, left); // 將基準數交換至兩子陣列的分界線
return i; // 返回基準數的索引
}
/* 快速排序 */
public static void quickSort(int[] nums, int left, int right) {
// 子陣列長度為 1 時終止遞迴
if (left >= right)
return;
// 哨兵劃分
int pivot = partition(nums, left, right);
// 遞迴左子陣列、右子陣列
quickSort(nums, left, pivot - 1);
quickSort(nums, pivot + 1, right);
}
}
/* 快速排序類別(尾遞迴最佳化) */
class QuickSortTailCall {
/* 元素交換 */
static void swap(int[] nums, int i, int j) {
int tmp = nums[i];
nums[i] = nums[j];
nums[j] = tmp;
}
/* 哨兵劃分 */
static int partition(int[] nums, int left, int right) {
// 以 nums[left] 為基準數
int i = left, j = right;
while (i < j) {
while (i < j && nums[j] >= nums[left])
j--; // 從右向左找首個小於基準數的元素
while (i < j && nums[i] <= nums[left])
i++; // 從左向右找首個大於基準數的元素
swap(nums, i, j); // 交換這兩個元素
}
swap(nums, i, left); // 將基準數交換至兩子陣列的分界線
return i; // 返回基準數的索引
}
/* 快速排序(尾遞迴最佳化) */
public static void quickSort(int[] nums, int left, int right) {
// 子陣列長度為 1 時終止
while (left < right) {
// 哨兵劃分操作
int pivot = partition(nums, left, right);
// 對兩個子陣列中較短的那個執行快速排序
if (pivot - left < right - pivot) {
quickSort(nums, left, pivot - 1); // 遞迴排序左子陣列
left = pivot + 1; // 剩餘未排序區間為 [pivot + 1, right]
} else {
quickSort(nums, pivot + 1, right); // 遞迴排序右子陣列
right = pivot - 1; // 剩餘未排序區間為 [left, pivot - 1]
}
}
}
}
public class quick_sort {
public static void main(String[] args) {
/* 快速排序 */
int[] nums = { 2, 4, 1, 0, 3, 5 };
QuickSort.quickSort(nums, 0, nums.length - 1);
System.out.println("快速排序完成後 nums = " + Arrays.toString(nums));
/* 快速排序(中位基準數最佳化) */
int[] nums1 = { 2, 4, 1, 0, 3, 5 };
QuickSortMedian.quickSort(nums1, 0, nums1.length - 1);
System.out.println("快速排序(中位基準數最佳化)完成後 nums1 = " + Arrays.toString(nums1));
/* 快速排序(尾遞迴最佳化) */
int[] nums2 = { 2, 4, 1, 0, 3, 5 };
QuickSortTailCall.quickSort(nums2, 0, nums2.length - 1);
System.out.println("快速排序(尾遞迴最佳化)完成後 nums2 = " + Arrays.toString(nums2));
}
}

View File

@@ -0,0 +1,68 @@
/**
* File: radix_sort.java
* Created Time: 2023-01-17
* Author: krahets (krahets@163.com)
*/
package chapter_sorting;
import java.util.*;
public class radix_sort {
/* 獲取元素 num 的第 k 位,其中 exp = 10^(k-1) */
static int digit(int num, int exp) {
// 傳入 exp 而非 k 可以避免在此重複執行昂貴的次方計算
return (num / exp) % 10;
}
/* 計數排序(根據 nums 第 k 位排序) */
static void countingSortDigit(int[] nums, int exp) {
// 十進位制的位範圍為 0~9 ,因此需要長度為 10 的桶陣列
int[] counter = new int[10];
int n = nums.length;
// 統計 0~9 各數字的出現次數
for (int i = 0; i < n; i++) {
int d = digit(nums[i], exp); // 獲取 nums[i] 第 k 位,記為 d
counter[d]++; // 統計數字 d 的出現次數
}
// 求前綴和,將“出現個數”轉換為“陣列索引”
for (int i = 1; i < 10; i++) {
counter[i] += counter[i - 1];
}
// 倒序走訪,根據桶內統計結果,將各元素填入 res
int[] res = new int[n];
for (int i = n - 1; i >= 0; i--) {
int d = digit(nums[i], exp);
int j = counter[d] - 1; // 獲取 d 在陣列中的索引 j
res[j] = nums[i]; // 將當前元素填入索引 j
counter[d]--; // 將 d 的數量減 1
}
// 使用結果覆蓋原陣列 nums
for (int i = 0; i < n; i++)
nums[i] = res[i];
}
/* 基數排序 */
static void radixSort(int[] nums) {
// 獲取陣列的最大元素,用於判斷最大位數
int m = Integer.MIN_VALUE;
for (int num : nums)
if (num > m)
m = num;
// 按照從低位到高位的順序走訪
for (int exp = 1; exp <= m; exp *= 10)
// 對陣列元素的第 k 位執行計數排序
// k = 1 -> exp = 1
// k = 2 -> exp = 10
// 即 exp = 10^(k-1)
countingSortDigit(nums, exp);
}
public static void main(String[] args) {
// 基數排序
int[] nums = { 10546151, 35663510, 42865989, 34862445, 81883077,
88906420, 72429244, 30524779, 82060337, 63832996 };
radixSort(nums);
System.out.println("基數排序完成後 nums = " + Arrays.toString(nums));
}
}

View File

@@ -0,0 +1,35 @@
/**
* File: selection_sort.java
* Created Time: 2023-05-23
* Author: krahets (krahets@163.com)
*/
package chapter_sorting;
import java.util.Arrays;
public class selection_sort {
/* 選擇排序 */
public static void selectionSort(int[] nums) {
int n = nums.length;
// 外迴圈:未排序區間為 [i, n-1]
for (int i = 0; i < n - 1; i++) {
// 內迴圈:找到未排序區間內的最小元素
int k = i;
for (int j = i + 1; j < n; j++) {
if (nums[j] < nums[k])
k = j; // 記錄最小元素的索引
}
// 將該最小元素與未排序區間的首個元素交換
int temp = nums[i];
nums[i] = nums[k];
nums[k] = temp;
}
}
public static void main(String[] args) {
int[] nums = { 4, 1, 3, 1, 5, 2 };
selectionSort(nums);
System.out.println("選擇排序完成後 nums = " + Arrays.toString(nums));
}
}

View File

@@ -0,0 +1,151 @@
/**
* File: array_deque.java
* Created Time: 2023-02-16
* Author: krahets (krahets@163.com), FangYuan33 (374072213@qq.com)
*/
package chapter_stack_and_queue;
import java.util.*;
/* 基於環形陣列實現的雙向佇列 */
class ArrayDeque {
private int[] nums; // 用於儲存雙向佇列元素的陣列
private int front; // 佇列首指標,指向佇列首元素
private int queSize; // 雙向佇列長度
/* 建構子 */
public ArrayDeque(int capacity) {
this.nums = new int[capacity];
front = queSize = 0;
}
/* 獲取雙向佇列的容量 */
public int capacity() {
return nums.length;
}
/* 獲取雙向佇列的長度 */
public int size() {
return queSize;
}
/* 判斷雙向佇列是否為空 */
public boolean isEmpty() {
return queSize == 0;
}
/* 計算環形陣列索引 */
private int index(int i) {
// 透過取餘操作實現陣列首尾相連
// 當 i 越過陣列尾部後,回到頭部
// 當 i 越過陣列頭部後,回到尾部
return (i + capacity()) % capacity();
}
/* 佇列首入列 */
public void pushFirst(int num) {
if (queSize == capacity()) {
System.out.println("雙向佇列已滿");
return;
}
// 佇列首指標向左移動一位
// 透過取餘操作實現 front 越過陣列頭部後回到尾部
front = index(front - 1);
// 將 num 新增至佇列首
nums[front] = num;
queSize++;
}
/* 佇列尾入列 */
public void pushLast(int num) {
if (queSize == capacity()) {
System.out.println("雙向佇列已滿");
return;
}
// 計算佇列尾指標,指向佇列尾索引 + 1
int rear = index(front + queSize);
// 將 num 新增至佇列尾
nums[rear] = num;
queSize++;
}
/* 佇列首出列 */
public int popFirst() {
int num = peekFirst();
// 佇列首指標向後移動一位
front = index(front + 1);
queSize--;
return num;
}
/* 佇列尾出列 */
public int popLast() {
int num = peekLast();
queSize--;
return num;
}
/* 訪問佇列首元素 */
public int peekFirst() {
if (isEmpty())
throw new IndexOutOfBoundsException();
return nums[front];
}
/* 訪問佇列尾元素 */
public int peekLast() {
if (isEmpty())
throw new IndexOutOfBoundsException();
// 計算尾元素索引
int last = index(front + queSize - 1);
return nums[last];
}
/* 返回陣列用於列印 */
public int[] toArray() {
// 僅轉換有效長度範圍內的串列元素
int[] res = new int[queSize];
for (int i = 0, j = front; i < queSize; i++, j++) {
res[i] = nums[index(j)];
}
return res;
}
}
public class array_deque {
public static void main(String[] args) {
/* 初始化雙向佇列 */
ArrayDeque deque = new ArrayDeque(10);
deque.pushLast(3);
deque.pushLast(2);
deque.pushLast(5);
System.out.println("雙向佇列 deque = " + Arrays.toString(deque.toArray()));
/* 訪問元素 */
int peekFirst = deque.peekFirst();
System.out.println("佇列首元素 peekFirst = " + peekFirst);
int peekLast = deque.peekLast();
System.out.println("佇列尾元素 peekLast = " + peekLast);
/* 元素入列 */
deque.pushLast(4);
System.out.println("元素 4 佇列尾入列後 deque = " + Arrays.toString(deque.toArray()));
deque.pushFirst(1);
System.out.println("元素 1 佇列首入列後 deque = " + Arrays.toString(deque.toArray()));
/* 元素出列 */
int popLast = deque.popLast();
System.out.println("佇列尾出列元素 = " + popLast + ",佇列尾出列後 deque = " + Arrays.toString(deque.toArray()));
int popFirst = deque.popFirst();
System.out.println("佇列首出列元素 = " + popFirst + ",佇列首出列後 deque = " + Arrays.toString(deque.toArray()));
/* 獲取雙向佇列的長度 */
int size = deque.size();
System.out.println("雙向佇列長度 size = " + size);
/* 判斷雙向佇列是否為空 */
boolean isEmpty = deque.isEmpty();
System.out.println("雙向佇列是否為空 = " + isEmpty);
}
}

View File

@@ -0,0 +1,115 @@
/**
* File: array_queue.java
* Created Time: 2022-11-25
* Author: krahets (krahets@163.com)
*/
package chapter_stack_and_queue;
import java.util.*;
/* 基於環形陣列實現的佇列 */
class ArrayQueue {
private int[] nums; // 用於儲存佇列元素的陣列
private int front; // 佇列首指標,指向佇列首元素
private int queSize; // 佇列長度
public ArrayQueue(int capacity) {
nums = new int[capacity];
front = queSize = 0;
}
/* 獲取佇列的容量 */
public int capacity() {
return nums.length;
}
/* 獲取佇列的長度 */
public int size() {
return queSize;
}
/* 判斷佇列是否為空 */
public boolean isEmpty() {
return queSize == 0;
}
/* 入列 */
public void push(int num) {
if (queSize == capacity()) {
System.out.println("佇列已滿");
return;
}
// 計算佇列尾指標,指向佇列尾索引 + 1
// 透過取餘操作實現 rear 越過陣列尾部後回到頭部
int rear = (front + queSize) % capacity();
// 將 num 新增至佇列尾
nums[rear] = num;
queSize++;
}
/* 出列 */
public int pop() {
int num = peek();
// 佇列首指標向後移動一位,若越過尾部,則返回到陣列頭部
front = (front + 1) % capacity();
queSize--;
return num;
}
/* 訪問佇列首元素 */
public int peek() {
if (isEmpty())
throw new IndexOutOfBoundsException();
return nums[front];
}
/* 返回陣列 */
public int[] toArray() {
// 僅轉換有效長度範圍內的串列元素
int[] res = new int[queSize];
for (int i = 0, j = front; i < queSize; i++, j++) {
res[i] = nums[j % capacity()];
}
return res;
}
}
public class array_queue {
public static void main(String[] args) {
/* 初始化佇列 */
int capacity = 10;
ArrayQueue queue = new ArrayQueue(capacity);
/* 元素入列 */
queue.push(1);
queue.push(3);
queue.push(2);
queue.push(5);
queue.push(4);
System.out.println("佇列 queue = " + Arrays.toString(queue.toArray()));
/* 訪問佇列首元素 */
int peek = queue.peek();
System.out.println("佇列首元素 peek = " + peek);
/* 元素出列 */
int pop = queue.pop();
System.out.println("出列元素 pop = " + pop + ",出列後 queue = " + Arrays.toString(queue.toArray()));
/* 獲取佇列的長度 */
int size = queue.size();
System.out.println("佇列長度 size = " + size);
/* 判斷佇列是否為空 */
boolean isEmpty = queue.isEmpty();
System.out.println("佇列是否為空 = " + isEmpty);
/* 測試環形陣列 */
for (int i = 0; i < 10; i++) {
queue.push(i);
queue.pop();
System.out.println("" + i + " 輪入列 + 出列後 queue = " + Arrays.toString(queue.toArray()));
}
}
}

View File

@@ -0,0 +1,84 @@
/**
* File: array_stack.java
* Created Time: 2022-11-25
* Author: krahets (krahets@163.com)
*/
package chapter_stack_and_queue;
import java.util.*;
/* 基於陣列實現的堆疊 */
class ArrayStack {
private ArrayList<Integer> stack;
public ArrayStack() {
// 初始化串列(動態陣列)
stack = new ArrayList<>();
}
/* 獲取堆疊的長度 */
public int size() {
return stack.size();
}
/* 判斷堆疊是否為空 */
public boolean isEmpty() {
return size() == 0;
}
/* 入堆疊 */
public void push(int num) {
stack.add(num);
}
/* 出堆疊 */
public int pop() {
if (isEmpty())
throw new IndexOutOfBoundsException();
return stack.remove(size() - 1);
}
/* 訪問堆疊頂元素 */
public int peek() {
if (isEmpty())
throw new IndexOutOfBoundsException();
return stack.get(size() - 1);
}
/* 將 List 轉化為 Array 並返回 */
public Object[] toArray() {
return stack.toArray();
}
}
public class array_stack {
public static void main(String[] args) {
/* 初始化堆疊 */
ArrayStack stack = new ArrayStack();
/* 元素入堆疊 */
stack.push(1);
stack.push(3);
stack.push(2);
stack.push(5);
stack.push(4);
System.out.println("堆疊 stack = " + Arrays.toString(stack.toArray()));
/* 訪問堆疊頂元素 */
int peek = stack.peek();
System.out.println("堆疊頂元素 peek = " + peek);
/* 元素出堆疊 */
int pop = stack.pop();
System.out.println("出堆疊元素 pop = " + pop + ",出堆疊後 stack = " + Arrays.toString(stack.toArray()));
/* 獲取堆疊的長度 */
int size = stack.size();
System.out.println("堆疊的長度 size = " + size);
/* 判斷是否為空 */
boolean isEmpty = stack.isEmpty();
System.out.println("堆疊是否為空 = " + isEmpty);
}
}

View File

@@ -0,0 +1,46 @@
/**
* File: deque.java
* Created Time: 2022-11-25
* Author: krahets (krahets@163.com)
*/
package chapter_stack_and_queue;
import java.util.*;
public class deque {
public static void main(String[] args) {
/* 初始化雙向佇列 */
Deque<Integer> deque = new LinkedList<>();
deque.offerLast(3);
deque.offerLast(2);
deque.offerLast(5);
System.out.println("雙向佇列 deque = " + deque);
/* 訪問元素 */
int peekFirst = deque.peekFirst();
System.out.println("佇列首元素 peekFirst = " + peekFirst);
int peekLast = deque.peekLast();
System.out.println("佇列尾元素 peekLast = " + peekLast);
/* 元素入列 */
deque.offerLast(4);
System.out.println("元素 4 佇列尾入列後 deque = " + deque);
deque.offerFirst(1);
System.out.println("元素 1 佇列首入列後 deque = " + deque);
/* 元素出列 */
int popLast = deque.pollLast();
System.out.println("佇列尾出列元素 = " + popLast + ",佇列尾出列後 deque = " + deque);
int popFirst = deque.pollFirst();
System.out.println("佇列首出列元素 = " + popFirst + ",佇列首出列後 deque = " + deque);
/* 獲取雙向佇列的長度 */
int size = deque.size();
System.out.println("雙向佇列長度 size = " + size);
/* 判斷雙向佇列是否為空 */
boolean isEmpty = deque.isEmpty();
System.out.println("雙向佇列是否為空 = " + isEmpty);
}
}

View File

@@ -0,0 +1,175 @@
/**
* File: linkedlist_deque.java
* Created Time: 2023-01-20
* Author: krahets (krahets@163.com)
*/
package chapter_stack_and_queue;
import java.util.*;
/* 雙向鏈結串列節點 */
class ListNode {
int val; // 節點值
ListNode next; // 後繼節點引用
ListNode prev; // 前驅節點引用
ListNode(int val) {
this.val = val;
prev = next = null;
}
}
/* 基於雙向鏈結串列實現的雙向佇列 */
class LinkedListDeque {
private ListNode front, rear; // 頭節點 front ,尾節點 rear
private int queSize = 0; // 雙向佇列的長度
public LinkedListDeque() {
front = rear = null;
}
/* 獲取雙向佇列的長度 */
public int size() {
return queSize;
}
/* 判斷雙向佇列是否為空 */
public boolean isEmpty() {
return size() == 0;
}
/* 入列操作 */
private void push(int num, boolean isFront) {
ListNode node = new ListNode(num);
// 若鏈結串列為空,則令 front 和 rear 都指向 node
if (isEmpty())
front = rear = node;
// 佇列首入列操作
else if (isFront) {
// 將 node 新增至鏈結串列頭部
front.prev = node;
node.next = front;
front = node; // 更新頭節點
// 佇列尾入列操作
} else {
// 將 node 新增至鏈結串列尾部
rear.next = node;
node.prev = rear;
rear = node; // 更新尾節點
}
queSize++; // 更新佇列長度
}
/* 佇列首入列 */
public void pushFirst(int num) {
push(num, true);
}
/* 佇列尾入列 */
public void pushLast(int num) {
push(num, false);
}
/* 出列操作 */
private int pop(boolean isFront) {
if (isEmpty())
throw new IndexOutOfBoundsException();
int val;
// 佇列首出列操作
if (isFront) {
val = front.val; // 暫存頭節點值
// 刪除頭節點
ListNode fNext = front.next;
if (fNext != null) {
fNext.prev = null;
front.next = null;
}
front = fNext; // 更新頭節點
// 佇列尾出列操作
} else {
val = rear.val; // 暫存尾節點值
// 刪除尾節點
ListNode rPrev = rear.prev;
if (rPrev != null) {
rPrev.next = null;
rear.prev = null;
}
rear = rPrev; // 更新尾節點
}
queSize--; // 更新佇列長度
return val;
}
/* 佇列首出列 */
public int popFirst() {
return pop(true);
}
/* 佇列尾出列 */
public int popLast() {
return pop(false);
}
/* 訪問佇列首元素 */
public int peekFirst() {
if (isEmpty())
throw new IndexOutOfBoundsException();
return front.val;
}
/* 訪問佇列尾元素 */
public int peekLast() {
if (isEmpty())
throw new IndexOutOfBoundsException();
return rear.val;
}
/* 返回陣列用於列印 */
public int[] toArray() {
ListNode node = front;
int[] res = new int[size()];
for (int i = 0; i < res.length; i++) {
res[i] = node.val;
node = node.next;
}
return res;
}
}
public class linkedlist_deque {
public static void main(String[] args) {
/* 初始化雙向佇列 */
LinkedListDeque deque = new LinkedListDeque();
deque.pushLast(3);
deque.pushLast(2);
deque.pushLast(5);
System.out.println("雙向佇列 deque = " + Arrays.toString(deque.toArray()));
/* 訪問元素 */
int peekFirst = deque.peekFirst();
System.out.println("佇列首元素 peekFirst = " + peekFirst);
int peekLast = deque.peekLast();
System.out.println("佇列尾元素 peekLast = " + peekLast);
/* 元素入列 */
deque.pushLast(4);
System.out.println("元素 4 佇列尾入列後 deque = " + Arrays.toString(deque.toArray()));
deque.pushFirst(1);
System.out.println("元素 1 佇列首入列後 deque = " + Arrays.toString(deque.toArray()));
/* 元素出列 */
int popLast = deque.popLast();
System.out.println("佇列尾出列元素 = " + popLast + ",佇列尾出列後 deque = " + Arrays.toString(deque.toArray()));
int popFirst = deque.popFirst();
System.out.println("佇列首出列元素 = " + popFirst + ",佇列首出列後 deque = " + Arrays.toString(deque.toArray()));
/* 獲取雙向佇列的長度 */
int size = deque.size();
System.out.println("雙向佇列長度 size = " + size);
/* 判斷雙向佇列是否為空 */
boolean isEmpty = deque.isEmpty();
System.out.println("雙向佇列是否為空 = " + isEmpty);
}
}

View File

@@ -0,0 +1,104 @@
/**
* File: linkedlist_queue.java
* Created Time: 2022-11-25
* Author: krahets (krahets@163.com)
*/
package chapter_stack_and_queue;
import java.util.*;
/* 基於鏈結串列實現的佇列 */
class LinkedListQueue {
private ListNode front, rear; // 頭節點 front ,尾節點 rear
private int queSize = 0;
public LinkedListQueue() {
front = null;
rear = null;
}
/* 獲取佇列的長度 */
public int size() {
return queSize;
}
/* 判斷佇列是否為空 */
public boolean isEmpty() {
return size() == 0;
}
/* 入列 */
public void push(int num) {
// 在尾節點後新增 num
ListNode node = new ListNode(num);
// 如果佇列為空,則令頭、尾節點都指向該節點
if (front == null) {
front = node;
rear = node;
// 如果佇列不為空,則將該節點新增到尾節點後
} else {
rear.next = node;
rear = node;
}
queSize++;
}
/* 出列 */
public int pop() {
int num = peek();
// 刪除頭節點
front = front.next;
queSize--;
return num;
}
/* 訪問佇列首元素 */
public int peek() {
if (isEmpty())
throw new IndexOutOfBoundsException();
return front.val;
}
/* 將鏈結串列轉化為 Array 並返回 */
public int[] toArray() {
ListNode node = front;
int[] res = new int[size()];
for (int i = 0; i < res.length; i++) {
res[i] = node.val;
node = node.next;
}
return res;
}
}
public class linkedlist_queue {
public static void main(String[] args) {
/* 初始化佇列 */
LinkedListQueue queue = new LinkedListQueue();
/* 元素入列 */
queue.push(1);
queue.push(3);
queue.push(2);
queue.push(5);
queue.push(4);
System.out.println("佇列 queue = " + Arrays.toString(queue.toArray()));
/* 訪問佇列首元素 */
int peek = queue.peek();
System.out.println("佇列首元素 peek = " + peek);
/* 元素出列 */
int pop = queue.pop();
System.out.println("出列元素 pop = " + pop + ",出列後 queue = " + Arrays.toString(queue.toArray()));
/* 獲取佇列的長度 */
int size = queue.size();
System.out.println("佇列長度 size = " + size);
/* 判斷佇列是否為空 */
boolean isEmpty = queue.isEmpty();
System.out.println("佇列是否為空 = " + isEmpty);
}
}

View File

@@ -0,0 +1,95 @@
/**
* File: linkedlist_stack.java
* Created Time: 2022-11-25
* Author: krahets (krahets@163.com)
*/
package chapter_stack_and_queue;
import java.util.*;
import utils.*;
/* 基於鏈結串列實現的堆疊 */
class LinkedListStack {
private ListNode stackPeek; // 將頭節點作為堆疊頂
private int stkSize = 0; // 堆疊的長度
public LinkedListStack() {
stackPeek = null;
}
/* 獲取堆疊的長度 */
public int size() {
return stkSize;
}
/* 判斷堆疊是否為空 */
public boolean isEmpty() {
return size() == 0;
}
/* 入堆疊 */
public void push(int num) {
ListNode node = new ListNode(num);
node.next = stackPeek;
stackPeek = node;
stkSize++;
}
/* 出堆疊 */
public int pop() {
int num = peek();
stackPeek = stackPeek.next;
stkSize--;
return num;
}
/* 訪問堆疊頂元素 */
public int peek() {
if (isEmpty())
throw new IndexOutOfBoundsException();
return stackPeek.val;
}
/* 將 List 轉化為 Array 並返回 */
public int[] toArray() {
ListNode node = stackPeek;
int[] res = new int[size()];
for (int i = res.length - 1; i >= 0; i--) {
res[i] = node.val;
node = node.next;
}
return res;
}
}
public class linkedlist_stack {
public static void main(String[] args) {
/* 初始化堆疊 */
LinkedListStack stack = new LinkedListStack();
/* 元素入堆疊 */
stack.push(1);
stack.push(3);
stack.push(2);
stack.push(5);
stack.push(4);
System.out.println("堆疊 stack = " + Arrays.toString(stack.toArray()));
/* 訪問堆疊頂元素 */
int peek = stack.peek();
System.out.println("堆疊頂元素 peek = " + peek);
/* 元素出堆疊 */
int pop = stack.pop();
System.out.println("出堆疊元素 pop = " + pop + ",出堆疊後 stack = " + Arrays.toString(stack.toArray()));
/* 獲取堆疊的長度 */
int size = stack.size();
System.out.println("堆疊的長度 size = " + size);
/* 判斷是否為空 */
boolean isEmpty = stack.isEmpty();
System.out.println("堆疊是否為空 = " + isEmpty);
}
}

View File

@@ -0,0 +1,40 @@
/**
* File: queue.java
* Created Time: 2022-11-25
* Author: krahets (krahets@163.com)
*/
package chapter_stack_and_queue;
import java.util.*;
public class queue {
public static void main(String[] args) {
/* 初始化佇列 */
Queue<Integer> queue = new LinkedList<>();
/* 元素入列 */
queue.offer(1);
queue.offer(3);
queue.offer(2);
queue.offer(5);
queue.offer(4);
System.out.println("佇列 queue = " + queue);
/* 訪問佇列首元素 */
int peek = queue.peek();
System.out.println("佇列首元素 peek = " + peek);
/* 元素出列 */
int pop = queue.poll();
System.out.println("出列元素 pop = " + pop + ",出列後 queue = " + queue);
/* 獲取佇列的長度 */
int size = queue.size();
System.out.println("佇列長度 size = " + size);
/* 判斷佇列是否為空 */
boolean isEmpty = queue.isEmpty();
System.out.println("佇列是否為空 = " + isEmpty);
}
}

View File

@@ -0,0 +1,40 @@
/**
* File: stack.java
* Created Time: 2022-11-25
* Author: krahets (krahets@163.com)
*/
package chapter_stack_and_queue;
import java.util.*;
public class stack {
public static void main(String[] args) {
/* 初始化堆疊 */
Stack<Integer> stack = new Stack<>();
/* 元素入堆疊 */
stack.push(1);
stack.push(3);
stack.push(2);
stack.push(5);
stack.push(4);
System.out.println("堆疊 stack = " + stack);
/* 訪問堆疊頂元素 */
int peek = stack.peek();
System.out.println("堆疊頂元素 peek = " + peek);
/* 元素出堆疊 */
int pop = stack.pop();
System.out.println("出堆疊元素 pop = " + pop + ",出堆疊後 stack = " + stack);
/* 獲取堆疊的長度 */
int size = stack.size();
System.out.println("堆疊的長度 size = " + size);
/* 判斷是否為空 */
boolean isEmpty = stack.isEmpty();
System.out.println("堆疊是否為空 = " + isEmpty);
}
}

View File

@@ -0,0 +1,136 @@
/**
* File: array_binary_tree.java
* Created Time: 2023-07-19
* Author: krahets (krahets@163.com)
*/
package chapter_tree;
import utils.*;
import java.util.*;
/* 陣列表示下的二元樹類別 */
class ArrayBinaryTree {
private List<Integer> tree;
/* 建構子 */
public ArrayBinaryTree(List<Integer> arr) {
tree = new ArrayList<>(arr);
}
/* 串列容量 */
public int size() {
return tree.size();
}
/* 獲取索引為 i 節點的值 */
public Integer val(int i) {
// 若索引越界,則返回 null ,代表空位
if (i < 0 || i >= size())
return null;
return tree.get(i);
}
/* 獲取索引為 i 節點的左子節點的索引 */
public Integer left(int i) {
return 2 * i + 1;
}
/* 獲取索引為 i 節點的右子節點的索引 */
public Integer right(int i) {
return 2 * i + 2;
}
/* 獲取索引為 i 節點的父節點的索引 */
public Integer parent(int i) {
return (i - 1) / 2;
}
/* 層序走訪 */
public List<Integer> levelOrder() {
List<Integer> res = new ArrayList<>();
// 直接走訪陣列
for (int i = 0; i < size(); i++) {
if (val(i) != null)
res.add(val(i));
}
return res;
}
/* 深度優先走訪 */
private void dfs(Integer i, String order, List<Integer> res) {
// 若為空位,則返回
if (val(i) == null)
return;
// 前序走訪
if ("pre".equals(order))
res.add(val(i));
dfs(left(i), order, res);
// 中序走訪
if ("in".equals(order))
res.add(val(i));
dfs(right(i), order, res);
// 後序走訪
if ("post".equals(order))
res.add(val(i));
}
/* 前序走訪 */
public List<Integer> preOrder() {
List<Integer> res = new ArrayList<>();
dfs(0, "pre", res);
return res;
}
/* 中序走訪 */
public List<Integer> inOrder() {
List<Integer> res = new ArrayList<>();
dfs(0, "in", res);
return res;
}
/* 後序走訪 */
public List<Integer> postOrder() {
List<Integer> res = new ArrayList<>();
dfs(0, "post", res);
return res;
}
}
public class array_binary_tree {
public static void main(String[] args) {
// 初始化二元樹
// 這裡藉助了一個從陣列直接生成二元樹的函式
List<Integer> arr = Arrays.asList(1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15);
TreeNode root = TreeNode.listToTree(arr);
System.out.println("\n初始化二元樹\n");
System.out.println("二元樹的陣列表示:");
System.out.println(arr);
System.out.println("二元樹的鏈結串列表示:");
PrintUtil.printTree(root);
// 陣列表示下的二元樹類別
ArrayBinaryTree abt = new ArrayBinaryTree(arr);
// 訪問節點
int i = 1;
Integer l = abt.left(i);
Integer r = abt.right(i);
Integer p = abt.parent(i);
System.out.println("\n當前節點的索引為 " + i + " ,值為 " + abt.val(i));
System.out.println("其左子節點的索引為 " + l + " ,值為 " + (l == null ? "null" : abt.val(l)));
System.out.println("其右子節點的索引為 " + r + " ,值為 " + (r == null ? "null" : abt.val(r)));
System.out.println("其父節點的索引為 " + p + " ,值為 " + (p == null ? "null" : abt.val(p)));
// 走訪樹
List<Integer> res = abt.levelOrder();
System.out.println("\n層序走訪為" + res);
res = abt.preOrder();
System.out.println("前序走訪為:" + res);
res = abt.inOrder();
System.out.println("中序走訪為:" + res);
res = abt.postOrder();
System.out.println("後序走訪為:" + res);
}
}

View File

@@ -0,0 +1,220 @@
/**
* File: avl_tree.java
* Created Time: 2022-12-10
* Author: krahets (krahets@163.com)
*/
package chapter_tree;
import utils.*;
/* AVL 樹 */
class AVLTree {
TreeNode root; // 根節點
/* 獲取節點高度 */
public int height(TreeNode node) {
// 空節點高度為 -1 ,葉節點高度為 0
return node == null ? -1 : node.height;
}
/* 更新節點高度 */
private void updateHeight(TreeNode node) {
// 節點高度等於最高子樹高度 + 1
node.height = Math.max(height(node.left), height(node.right)) + 1;
}
/* 獲取平衡因子 */
public int balanceFactor(TreeNode node) {
// 空節點平衡因子為 0
if (node == null)
return 0;
// 節點平衡因子 = 左子樹高度 - 右子樹高度
return height(node.left) - height(node.right);
}
/* 右旋操作 */
private TreeNode rightRotate(TreeNode node) {
TreeNode child = node.left;
TreeNode grandChild = child.right;
// 以 child 為原點,將 node 向右旋轉
child.right = node;
node.left = grandChild;
// 更新節點高度
updateHeight(node);
updateHeight(child);
// 返回旋轉後子樹的根節點
return child;
}
/* 左旋操作 */
private TreeNode leftRotate(TreeNode node) {
TreeNode child = node.right;
TreeNode grandChild = child.left;
// 以 child 為原點,將 node 向左旋轉
child.left = node;
node.right = grandChild;
// 更新節點高度
updateHeight(node);
updateHeight(child);
// 返回旋轉後子樹的根節點
return child;
}
/* 執行旋轉操作,使該子樹重新恢復平衡 */
private TreeNode rotate(TreeNode node) {
// 獲取節點 node 的平衡因子
int balanceFactor = balanceFactor(node);
// 左偏樹
if (balanceFactor > 1) {
if (balanceFactor(node.left) >= 0) {
// 右旋
return rightRotate(node);
} else {
// 先左旋後右旋
node.left = leftRotate(node.left);
return rightRotate(node);
}
}
// 右偏樹
if (balanceFactor < -1) {
if (balanceFactor(node.right) <= 0) {
// 左旋
return leftRotate(node);
} else {
// 先右旋後左旋
node.right = rightRotate(node.right);
return leftRotate(node);
}
}
// 平衡樹,無須旋轉,直接返回
return node;
}
/* 插入節點 */
public void insert(int val) {
root = insertHelper(root, val);
}
/* 遞迴插入節點(輔助方法) */
private TreeNode insertHelper(TreeNode node, int val) {
if (node == null)
return new TreeNode(val);
/* 1. 查詢插入位置並插入節點 */
if (val < node.val)
node.left = insertHelper(node.left, val);
else if (val > node.val)
node.right = insertHelper(node.right, val);
else
return node; // 重複節點不插入,直接返回
updateHeight(node); // 更新節點高度
/* 2. 執行旋轉操作,使該子樹重新恢復平衡 */
node = rotate(node);
// 返回子樹的根節點
return node;
}
/* 刪除節點 */
public void remove(int val) {
root = removeHelper(root, val);
}
/* 遞迴刪除節點(輔助方法) */
private TreeNode removeHelper(TreeNode node, int val) {
if (node == null)
return null;
/* 1. 查詢節點並刪除 */
if (val < node.val)
node.left = removeHelper(node.left, val);
else if (val > node.val)
node.right = removeHelper(node.right, val);
else {
if (node.left == null || node.right == null) {
TreeNode child = node.left != null ? node.left : node.right;
// 子節點數量 = 0 ,直接刪除 node 並返回
if (child == null)
return null;
// 子節點數量 = 1 ,直接刪除 node
else
node = child;
} else {
// 子節點數量 = 2 ,則將中序走訪的下個節點刪除,並用該節點替換當前節點
TreeNode temp = node.right;
while (temp.left != null) {
temp = temp.left;
}
node.right = removeHelper(node.right, temp.val);
node.val = temp.val;
}
}
updateHeight(node); // 更新節點高度
/* 2. 執行旋轉操作,使該子樹重新恢復平衡 */
node = rotate(node);
// 返回子樹的根節點
return node;
}
/* 查詢節點 */
public TreeNode search(int val) {
TreeNode cur = root;
// 迴圈查詢,越過葉節點後跳出
while (cur != null) {
// 目標節點在 cur 的右子樹中
if (cur.val < val)
cur = cur.right;
// 目標節點在 cur 的左子樹中
else if (cur.val > val)
cur = cur.left;
// 找到目標節點,跳出迴圈
else
break;
}
// 返回目標節點
return cur;
}
}
public class avl_tree {
static void testInsert(AVLTree tree, int val) {
tree.insert(val);
System.out.println("\n插入節點 " + val + "AVL 樹為");
PrintUtil.printTree(tree.root);
}
static void testRemove(AVLTree tree, int val) {
tree.remove(val);
System.out.println("\n刪除節點 " + val + "AVL 樹為");
PrintUtil.printTree(tree.root);
}
public static void main(String[] args) {
/* 初始化空 AVL 樹 */
AVLTree avlTree = new AVLTree();
/* 插入節點 */
// 請關注插入節點後AVL 樹是如何保持平衡的
testInsert(avlTree, 1);
testInsert(avlTree, 2);
testInsert(avlTree, 3);
testInsert(avlTree, 4);
testInsert(avlTree, 5);
testInsert(avlTree, 8);
testInsert(avlTree, 7);
testInsert(avlTree, 9);
testInsert(avlTree, 10);
testInsert(avlTree, 6);
/* 插入重複節點 */
testInsert(avlTree, 7);
/* 刪除節點 */
// 請關注刪除節點後AVL 樹是如何保持平衡的
testRemove(avlTree, 8); // 刪除度為 0 的節點
testRemove(avlTree, 5); // 刪除度為 1 的節點
testRemove(avlTree, 4); // 刪除度為 2 的節點
/* 查詢節點 */
TreeNode node = avlTree.search(7);
System.out.println("\n查詢到的節點物件為 " + node + ",節點值 = " + node.val);
}
}

View File

@@ -0,0 +1,158 @@
/**
* File: binary_search_tree.java
* Created Time: 2022-11-25
* Author: krahets (krahets@163.com)
*/
package chapter_tree;
import utils.*;
/* 二元搜尋樹 */
class BinarySearchTree {
private TreeNode root;
/* 建構子 */
public BinarySearchTree() {
// 初始化空樹
root = null;
}
/* 獲取二元樹根節點 */
public TreeNode getRoot() {
return root;
}
/* 查詢節點 */
public TreeNode search(int num) {
TreeNode cur = root;
// 迴圈查詢,越過葉節點後跳出
while (cur != null) {
// 目標節點在 cur 的右子樹中
if (cur.val < num)
cur = cur.right;
// 目標節點在 cur 的左子樹中
else if (cur.val > num)
cur = cur.left;
// 找到目標節點,跳出迴圈
else
break;
}
// 返回目標節點
return cur;
}
/* 插入節點 */
public void insert(int num) {
// 若樹為空,則初始化根節點
if (root == null) {
root = new TreeNode(num);
return;
}
TreeNode cur = root, pre = null;
// 迴圈查詢,越過葉節點後跳出
while (cur != null) {
// 找到重複節點,直接返回
if (cur.val == num)
return;
pre = cur;
// 插入位置在 cur 的右子樹中
if (cur.val < num)
cur = cur.right;
// 插入位置在 cur 的左子樹中
else
cur = cur.left;
}
// 插入節點
TreeNode node = new TreeNode(num);
if (pre.val < num)
pre.right = node;
else
pre.left = node;
}
/* 刪除節點 */
public void remove(int num) {
// 若樹為空,直接提前返回
if (root == null)
return;
TreeNode cur = root, pre = null;
// 迴圈查詢,越過葉節點後跳出
while (cur != null) {
// 找到待刪除節點,跳出迴圈
if (cur.val == num)
break;
pre = cur;
// 待刪除節點在 cur 的右子樹中
if (cur.val < num)
cur = cur.right;
// 待刪除節點在 cur 的左子樹中
else
cur = cur.left;
}
// 若無待刪除節點,則直接返回
if (cur == null)
return;
// 子節點數量 = 0 or 1
if (cur.left == null || cur.right == null) {
// 當子節點數量 = 0 / 1 時, child = null / 該子節點
TreeNode child = cur.left != null ? cur.left : cur.right;
// 刪除節點 cur
if (cur != root) {
if (pre.left == cur)
pre.left = child;
else
pre.right = child;
} else {
// 若刪除節點為根節點,則重新指定根節點
root = child;
}
}
// 子節點數量 = 2
else {
// 獲取中序走訪中 cur 的下一個節點
TreeNode tmp = cur.right;
while (tmp.left != null) {
tmp = tmp.left;
}
// 遞迴刪除節點 tmp
remove(tmp.val);
// 用 tmp 覆蓋 cur
cur.val = tmp.val;
}
}
}
public class binary_search_tree {
public static void main(String[] args) {
/* 初始化二元搜尋樹 */
BinarySearchTree bst = new BinarySearchTree();
// 請注意,不同的插入順序會生成不同的二元樹,該序列可以生成一個完美二元樹
int[] nums = { 8, 4, 12, 2, 6, 10, 14, 1, 3, 5, 7, 9, 11, 13, 15 };
for (int num : nums) {
bst.insert(num);
}
System.out.println("\n初始化的二元樹為\n");
PrintUtil.printTree(bst.getRoot());
/* 查詢節點 */
TreeNode node = bst.search(7);
System.out.println("\n查詢到的節點物件為 " + node + ",節點值 = " + node.val);
/* 插入節點 */
bst.insert(16);
System.out.println("\n插入節點 16 後,二元樹為\n");
PrintUtil.printTree(bst.getRoot());
/* 刪除節點 */
bst.remove(1);
System.out.println("\n刪除節點 1 後,二元樹為\n");
PrintUtil.printTree(bst.getRoot());
bst.remove(2);
System.out.println("\n刪除節點 2 後,二元樹為\n");
PrintUtil.printTree(bst.getRoot());
bst.remove(4);
System.out.println("\n刪除節點 4 後,二元樹為\n");
PrintUtil.printTree(bst.getRoot());
}
}

View File

@@ -0,0 +1,40 @@
/**
* File: binary_tree.java
* Created Time: 2022-11-25
* Author: krahets (krahets@163.com)
*/
package chapter_tree;
import utils.*;
public class binary_tree {
public static void main(String[] args) {
/* 初始化二元樹 */
// 初始化節點
TreeNode n1 = new TreeNode(1);
TreeNode n2 = new TreeNode(2);
TreeNode n3 = new TreeNode(3);
TreeNode n4 = new TreeNode(4);
TreeNode n5 = new TreeNode(5);
// 構建節點之間的引用(指標)
n1.left = n2;
n1.right = n3;
n2.left = n4;
n2.right = n5;
System.out.println("\n初始化二元樹\n");
PrintUtil.printTree(n1);
/* 插入與刪除節點 */
TreeNode P = new TreeNode(0);
// 在 n1 -> n2 中間插入節點 P
n1.left = P;
P.left = n2;
System.out.println("\n插入節點 P 後\n");
PrintUtil.printTree(n1);
// 刪除節點 P
n1.left = n2;
System.out.println("\n刪除節點 P 後\n");
PrintUtil.printTree(n1);
}
}

View File

@@ -0,0 +1,42 @@
/**
* File: binary_tree_bfs.java
* Created Time: 2022-11-25
* Author: krahets (krahets@163.com)
*/
package chapter_tree;
import utils.*;
import java.util.*;
public class binary_tree_bfs {
/* 層序走訪 */
static List<Integer> levelOrder(TreeNode root) {
// 初始化佇列,加入根節點
Queue<TreeNode> queue = new LinkedList<>();
queue.add(root);
// 初始化一個串列,用於儲存走訪序列
List<Integer> list = new ArrayList<>();
while (!queue.isEmpty()) {
TreeNode node = queue.poll(); // 隊列出隊
list.add(node.val); // 儲存節點值
if (node.left != null)
queue.offer(node.left); // 左子節點入列
if (node.right != null)
queue.offer(node.right); // 右子節點入列
}
return list;
}
public static void main(String[] args) {
/* 初始化二元樹 */
// 這裡藉助了一個從陣列直接生成二元樹的函式
TreeNode root = TreeNode.listToTree(Arrays.asList(1, 2, 3, 4, 5, 6, 7));
System.out.println("\n初始化二元樹\n");
PrintUtil.printTree(root);
/* 層序走訪 */
List<Integer> list = levelOrder(root);
System.out.println("\n層序走訪的節點列印序列 = " + list);
}
}

View File

@@ -0,0 +1,68 @@
/**
* File: binary_tree_dfs.java
* Created Time: 2022-11-25
* Author: krahets (krahets@163.com)
*/
package chapter_tree;
import utils.*;
import java.util.*;
public class binary_tree_dfs {
// 初始化串列,用於儲存走訪序列
static ArrayList<Integer> list = new ArrayList<>();
/* 前序走訪 */
static void preOrder(TreeNode root) {
if (root == null)
return;
// 訪問優先順序:根節點 -> 左子樹 -> 右子樹
list.add(root.val);
preOrder(root.left);
preOrder(root.right);
}
/* 中序走訪 */
static void inOrder(TreeNode root) {
if (root == null)
return;
// 訪問優先順序:左子樹 -> 根節點 -> 右子樹
inOrder(root.left);
list.add(root.val);
inOrder(root.right);
}
/* 後序走訪 */
static void postOrder(TreeNode root) {
if (root == null)
return;
// 訪問優先順序:左子樹 -> 右子樹 -> 根節點
postOrder(root.left);
postOrder(root.right);
list.add(root.val);
}
public static void main(String[] args) {
/* 初始化二元樹 */
// 這裡藉助了一個從陣列直接生成二元樹的函式
TreeNode root = TreeNode.listToTree(Arrays.asList(1, 2, 3, 4, 5, 6, 7));
System.out.println("\n初始化二元樹\n");
PrintUtil.printTree(root);
/* 前序走訪 */
list.clear();
preOrder(root);
System.out.println("\n前序走訪的節點列印序列 = " + list);
/* 中序走訪 */
list.clear();
inOrder(root);
System.out.println("\n中序走訪的節點列印序列 = " + list);
/* 後序走訪 */
list.clear();
postOrder(root);
System.out.println("\n後序走訪的節點列印序列 = " + list);
}
}

View File

@@ -0,0 +1,28 @@
/**
* File: ListNode.java
* Created Time: 2022-11-25
* Author: krahets (krahets@163.com)
*/
package utils;
/* 鏈結串列節點 */
public class ListNode {
public int val;
public ListNode next;
public ListNode(int x) {
val = x;
}
/* 將串列反序列化為鏈結串列 */
public static ListNode arrToLinkedList(int[] arr) {
ListNode dum = new ListNode(0);
ListNode head = dum;
for (int val : arr) {
head.next = new ListNode(val);
head = head.next;
}
return dum.next;
}
}

View File

@@ -0,0 +1,116 @@
/**
* File: PrintUtil.java
* Created Time: 2022-11-25
* Author: krahets (krahets@163.com)
*/
package utils;
import java.util.*;
class Trunk {
Trunk prev;
String str;
Trunk(Trunk prev, String str) {
this.prev = prev;
this.str = str;
}
};
public class PrintUtil {
/* 列印矩陣Array */
public static <T> void printMatrix(T[][] matrix) {
System.out.println("[");
for (T[] row : matrix) {
System.out.println(" " + row + ",");
}
System.out.println("]");
}
/* 列印矩陣List */
public static <T> void printMatrix(List<List<T>> matrix) {
System.out.println("[");
for (List<T> row : matrix) {
System.out.println(" " + row + ",");
}
System.out.println("]");
}
/* 列印鏈結串列 */
public static void printLinkedList(ListNode head) {
List<String> list = new ArrayList<>();
while (head != null) {
list.add(String.valueOf(head.val));
head = head.next;
}
System.out.println(String.join(" -> ", list));
}
/* 列印二元樹 */
public static void printTree(TreeNode root) {
printTree(root, null, false);
}
/**
* 列印二元樹
* This tree printer is borrowed from TECHIE DELIGHT
* https://www.techiedelight.com/c-program-print-binary-tree/
*/
public static void printTree(TreeNode root, Trunk prev, boolean isRight) {
if (root == null) {
return;
}
String prev_str = " ";
Trunk trunk = new Trunk(prev, prev_str);
printTree(root.right, trunk, true);
if (prev == null) {
trunk.str = "———";
} else if (isRight) {
trunk.str = "/———";
prev_str = " |";
} else {
trunk.str = "\\———";
prev.str = prev_str;
}
showTrunks(trunk);
System.out.println(" " + root.val);
if (prev != null) {
prev.str = prev_str;
}
trunk.str = " |";
printTree(root.left, trunk, false);
}
public static void showTrunks(Trunk p) {
if (p == null) {
return;
}
showTrunks(p.prev);
System.out.print(p.str);
}
/* 列印雜湊表 */
public static <K, V> void printHashMap(Map<K, V> map) {
for (Map.Entry<K, V> kv : map.entrySet()) {
System.out.println(kv.getKey() + " -> " + kv.getValue());
}
}
/* 列印堆積(優先佇列) */
public static void printHeap(Queue<Integer> queue) {
List<Integer> list = new ArrayList<>(queue);
System.out.print("堆積的陣列表示:");
System.out.println(list);
System.out.println("堆積的樹狀表示:");
TreeNode root = TreeNode.listToTree(list);
printTree(root);
}
}

View File

@@ -0,0 +1,73 @@
/**
* File: TreeNode.java
* Created Time: 2022-11-25
* Author: krahets (krahets@163.com)
*/
package utils;
import java.util.*;
/* 二元樹節點類別 */
public class TreeNode {
public int val; // 節點值
public int height; // 節點高度
public TreeNode left; // 左子節點引用
public TreeNode right; // 右子節點引用
/* 建構子 */
public TreeNode(int x) {
val = x;
}
// 序列化編碼規則請參考:
// https://www.hello-algo.com/chapter_tree/array_representation_of_tree/
// 二元樹的陣列表示:
// [1, 2, 3, 4, None, 6, 7, 8, 9, None, None, 12, None, None, 15]
// 二元樹的鏈結串列表示:
// /——— 15
// /——— 7
// /——— 3
// | \——— 6
// | \——— 12
// ——— 1
// \——— 2
// | /——— 9
// \——— 4
// \——— 8
/* 將串列反序列化為二元樹:遞迴 */
private static TreeNode listToTreeDFS(List<Integer> arr, int i) {
if (i < 0 || i >= arr.size() || arr.get(i) == null) {
return null;
}
TreeNode root = new TreeNode(arr.get(i));
root.left = listToTreeDFS(arr, 2 * i + 1);
root.right = listToTreeDFS(arr, 2 * i + 2);
return root;
}
/* 將串列反序列化為二元樹 */
public static TreeNode listToTree(List<Integer> arr) {
return listToTreeDFS(arr, 0);
}
/* 將二元樹序列化為串列:遞迴 */
private static void treeToListDFS(TreeNode root, int i, List<Integer> res) {
if (root == null)
return;
while (i >= res.size()) {
res.add(null);
}
res.set(i, root.val);
treeToListDFS(root.left, 2 * i + 1, res);
treeToListDFS(root.right, 2 * i + 2, res);
}
/* 將二元樹序列化為串列 */
public static List<Integer> treeToList(TreeNode root) {
List<Integer> res = new ArrayList<>();
treeToListDFS(root, 0, res);
return res;
}
}

View File

@@ -0,0 +1,36 @@
/**
* File: Vertex.java
* Created Time: 2023-02-15
* Author: krahets (krahets@163.com)
*/
package utils;
import java.util.*;
/* 頂點類別 */
public class Vertex {
public int val;
public Vertex(int val) {
this.val = val;
}
/* 輸入值串列 vals ,返回頂點串列 vets */
public static Vertex[] valsToVets(int[] vals) {
Vertex[] vets = new Vertex[vals.length];
for (int i = 0; i < vals.length; i++) {
vets[i] = new Vertex(vals[i]);
}
return vets;
}
/* 輸入頂點串列 vets ,返回值串列 vals */
public static List<Integer> vetsToVals(List<Vertex> vets) {
List<Integer> vals = new ArrayList<>();
for (Vertex vet : vets) {
vals.add(vet.val);
}
return vals;
}
}