Format the Java codes with the Reat Hat extension.

This commit is contained in:
krahets 2023-04-14 00:12:10 +08:00
parent 7273ee24e8
commit f8513455b5
39 changed files with 195 additions and 205 deletions

3
.gitignore vendored
View file

@ -13,3 +13,6 @@ docs/overrides/
build/
site/
utils/
# test script
test.sh

View file

@ -13,8 +13,7 @@ public class array {
/* 随机返回一个数组元素 */
static int randomAccess(int[] nums) {
// 在区间 [0, nums.length) 中随机抽取一个数字
int randomIndex = ThreadLocalRandom.current().
nextInt(0, nums.length);
int randomIndex = ThreadLocalRandom.current().nextInt(0, nums.length);
// 获取并返回随机元素
int randomNum = nums[randomIndex];
return randomNum;

View file

@ -20,7 +20,7 @@ class MyList {
nums = new int[capacity];
}
/* 获取列表长度(即当前元素数量)*/
/* 获取列表长度(即当前元素数量) */
public int size() {
return size;
}

View file

@ -8,7 +8,6 @@ package chapter_computational_complexity;
import java.util.*;
public class leetcode_two_sum {
/* 方法一:暴力枚举 */
static int[] twoSumBruteForce(int[] nums, int target) {
@ -40,7 +39,7 @@ public class leetcode_two_sum {
public static void main(String[] args) {
// ======= Test Case =======
int[] nums = { 2,7,11,15 };
int[] nums = { 2, 7, 11, 15 };
int target = 9;
// ====== Driver Code ======

View file

@ -52,7 +52,8 @@ public class space_complexity {
/* 线性阶(递归实现) */
static void linearRecur(int n) {
System.out.println("递归 n = " + n);
if (n == 1) return;
if (n == 1)
return;
linearRecur(n - 1);
}
@ -73,7 +74,8 @@ public class space_complexity {
/* 平方阶(递归实现) */
static int quadraticRecur(int n) {
if (n <= 0) return 0;
if (n <= 0)
return 0;
// 数组 nums 长度为 n, n-1, ..., 2, 1
int[] nums = new int[n];
System.out.println("递归 n = " + n + " 中的 nums 长度 = " + nums.length);
@ -82,7 +84,8 @@ public class space_complexity {
/* 指数阶(建立满二叉树) */
static TreeNode buildTree(int n) {
if (n == 0) return null;
if (n == 0)
return null;
TreeNode root = new TreeNode(0);
root.left = buildTree(n - 1);
root.right = buildTree(n - 1);

View file

@ -81,7 +81,8 @@ public class time_complexity {
/* 指数阶(递归实现) */
static int expRecur(int n) {
if (n == 1) return 1;
if (n == 1)
return 1;
return expRecur(n - 1) + expRecur(n - 1) + 1;
}
@ -97,13 +98,15 @@ public class time_complexity {
/* 对数阶(递归实现) */
static int logRecur(float n) {
if (n <= 1) return 0;
if (n <= 1)
return 0;
return logRecur(n / 2) + 1;
}
/* 线性对数阶 */
static int linearLogRecur(float n) {
if (n <= 1) return 1;
if (n <= 1)
return 1;
int count = linearLogRecur(n / 2) +
linearLogRecur(n / 2);
for (int i = 0; i < n; i++) {
@ -114,7 +117,8 @@ public class time_complexity {
/* 阶乘阶(递归实现) */
static int factorialRecur(int n) {
if (n == 0) return 1;
if (n == 0)
return 1;
int count = 0;
// 1 个分裂出 n
for (int i = 0; i < n; i++) {

View file

@ -12,6 +12,7 @@ import java.util.*;
class Entry {
public int key;
public String val;
public Entry(int key, String val) {
this.key = key;
this.val = val;
@ -21,6 +22,7 @@ class Entry {
/* 基于数组简易实现的哈希表 */
class ArrayHashMap {
private List<Entry> buckets;
public ArrayHashMap() {
// 初始化数组包含 100 个桶
buckets = new ArrayList<>();
@ -39,7 +41,8 @@ class ArrayHashMap {
public String get(int key) {
int index = hashFunc(key);
Entry pair = buckets.get(index);
if (pair == null) return null;
if (pair == null)
return null;
return pair.val;
}
@ -89,13 +92,12 @@ class ArrayHashMap {
/* 打印哈希表 */
public void print() {
for (Entry kv: entrySet()) {
for (Entry kv : entrySet()) {
System.out.println(kv.key + " -> " + kv.val);
}
}
}
public class array_hash_map {
public static void main(String[] args) {
/* 初始化哈希表 */
@ -124,15 +126,15 @@ public class array_hash_map {
/* 遍历哈希表 */
System.out.println("\n遍历键值对 Key->Value");
for (Entry kv: map.entrySet()) {
for (Entry kv : map.entrySet()) {
System.out.println(kv.key + " -> " + kv.val);
}
System.out.println("\n单独遍历键 Key");
for (int key: map.keySet()) {
for (int key : map.keySet()) {
System.out.println(key);
}
System.out.println("\n单独遍历值 Value");
for (String val: map.valueSet()) {
for (String val : map.valueSet()) {
System.out.println(val);
}
}

View file

@ -37,15 +37,15 @@ public class hash_map {
/* 遍历哈希表 */
System.out.println("\n遍历键值对 Key->Value");
for (Map.Entry <Integer, String> kv: map.entrySet()) {
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()) {
for (int key : map.keySet()) {
System.out.println(key);
}
System.out.println("\n单独遍历值 Value");
for (String val: map.values()) {
for (String val : map.values()) {
System.out.println(val);
}
}

View file

@ -9,7 +9,6 @@ package chapter_heap;
import include.*;
import java.util.*;
public class heap {
public static void testPush(Queue<Integer> heap, int val) {
heap.offer(val); // 元素入堆

View file

@ -41,9 +41,9 @@ class MaxHeap {
/* 交换元素 */
private void swap(int i, int j) {
int a = maxHeap.get(i),
b = maxHeap.get(j),
tmp = a;
int a = maxHeap.get(i);
int b = maxHeap.get(j);
int tmp = a;
maxHeap.set(i, b);
maxHeap.set(j, tmp);
}
@ -111,7 +111,8 @@ class MaxHeap {
if (r < size() && maxHeap.get(r) > maxHeap.get(ma))
ma = r;
// 若节点 i 最大或索引 l, r 越界则无需继续堆化跳出
if (ma == i) break;
if (ma == i)
break;
// 交换两节点
swap(i, ma);
// 循环向下堆化
@ -127,7 +128,6 @@ class MaxHeap {
}
}
public class my_heap {
public static void main(String[] args) {
/* 初始化大顶堆 */

View file

@ -25,7 +25,7 @@ public class bubble_sort {
}
}
/* 冒泡排序(标志优化)*/
/* 冒泡排序(标志优化) */
static void bubbleSortWithFlag(int[] nums) {
// 外循环待排序元素数量为 n-1, n-2, ..., 1
for (int i = nums.length - 1; i > 0; i--) {
@ -40,7 +40,8 @@ public class bubble_sort {
flag = true; // 记录交换元素
}
}
if (!flag) break; // 此轮冒泡未交换任何元素直接跳出
if (!flag)
break; // 此轮冒泡未交换任何元素直接跳出
}
}

View file

@ -38,7 +38,8 @@ public class merge_sort {
/* 归并排序 */
static void mergeSort(int[] nums, int left, int right) {
// 终止条件
if (left >= right) return; // 当子数组长度为 1 时终止递归
if (left >= right)
return; // 当子数组长度为 1 时终止递归
// 划分阶段
int mid = (left + right) / 2; // 计算中点
mergeSort(nums, left, mid); // 递归左子数组

View file

@ -47,7 +47,8 @@ public class radix_sort {
// 获取数组的最大元素用于判断最大位数
int m = Integer.MIN_VALUE;
for (int num : nums)
if (num > m) m = num;
if (num > m)
m = num;
// 按照从低位到高位的顺序遍历
for (int exp = 1; exp <= m; exp *= 10)
// 对数组元素的第 k 位执行计数排序

View file

@ -11,6 +11,7 @@ import java.util.*;
/* 基于数组实现的栈 */
class ArrayStack {
private ArrayList<Integer> stack;
public ArrayStack() {
// 初始化列表动态数组
stack = new ArrayList<>();

View file

@ -13,6 +13,7 @@ class ListNode {
int val; // 节点值
ListNode next; // 后继节点引用指针
ListNode prev; // 前驱节点引用指针
ListNode(int val) {
this.val = val;
prev = next = null;

View file

@ -27,7 +27,8 @@ class AVLTree {
/* 获取平衡因子 */
public int balanceFactor(TreeNode node) {
// 空节点平衡因子为 0
if (node == null) return 0;
if (node == null)
return 0;
// 节点平衡因子 = 左子树高度 - 右子树高度
return height(node.left) - height(node.right);
}
@ -98,7 +99,8 @@ class AVLTree {
/* 递归插入节点(辅助方法) */
private TreeNode insertHelper(TreeNode node, int val) {
if (node == null) return new TreeNode(val);
if (node == null)
return new TreeNode(val);
/* 1. 查找插入位置,并插入节点 */
if (val < node.val)
node.left = insertHelper(node.left, val);
@ -121,7 +123,8 @@ class AVLTree {
/* 递归删除节点(辅助方法) */
private TreeNode removeHelper(TreeNode node, int val) {
if (node == null) return null;
if (node == null)
return null;
/* 1. 查找节点,并删除之 */
if (val < node.val)
node.left = removeHelper(node.left, val);
@ -152,7 +155,8 @@ class AVLTree {
/* 获取中序遍历中的下一个节点(仅适用于 root 有左子节点的情况) */
private TreeNode getInOrderNext(TreeNode node) {
if (node == null) return node;
if (node == null)
return node;
// 循环访问左子节点直到叶节点时为最小节点跳出
while (node.left != null) {
node = node.left;

View file

@ -25,7 +25,8 @@ class BinarySearchTree {
/* 构建二叉搜索树 */
public TreeNode buildTree(int[] nums, int i, int j) {
if (i > j) return null;
if (i > j)
return null;
// 将数组中间节点作为根节点
int mid = (i + j) / 2;
TreeNode root = new TreeNode(nums[mid]);
@ -41,11 +42,14 @@ class BinarySearchTree {
// 循环查找越过叶节点后跳出
while (cur != null) {
// 目标节点在 cur 的右子树中
if (cur.val < num) cur = cur.right;
if (cur.val < num)
cur = cur.right;
// 目标节点在 cur 的左子树中
else if (cur.val > num) cur = cur.left;
else if (cur.val > num)
cur = cur.left;
// 找到目标节点跳出循环
else break;
else
break;
}
// 返回目标节点
return cur;
@ -54,49 +58,62 @@ class BinarySearchTree {
/* 插入节点 */
public TreeNode insert(int num) {
// 若树为空直接提前返回
if (root == null) return null;
if (root == null)
return null;
TreeNode cur = root, pre = null;
// 循环查找越过叶节点后跳出
while (cur != null) {
// 找到重复节点直接返回
if (cur.val == num) return null;
if (cur.val == num)
return null;
pre = cur;
// 插入位置在 cur 的右子树中
if (cur.val < num) cur = cur.right;
if (cur.val < num)
cur = cur.right;
// 插入位置在 cur 的左子树中
else cur = cur.left;
else
cur = cur.left;
}
// 插入节点 val
TreeNode node = new TreeNode(num);
if (pre.val < num) pre.right = node;
else pre.left = node;
if (pre.val < num)
pre.right = node;
else
pre.left = node;
return node;
}
/* 删除节点 */
public TreeNode remove(int num) {
// 若树为空直接提前返回
if (root == null) return null;
if (root == null)
return null;
TreeNode cur = root, pre = null;
// 循环查找越过叶节点后跳出
while (cur != null) {
// 找到待删除节点跳出循环
if (cur.val == num) break;
if (cur.val == num)
break;
pre = cur;
// 待删除节点在 cur 的右子树中
if (cur.val < num) cur = cur.right;
if (cur.val < num)
cur = cur.right;
// 待删除节点在 cur 的左子树中
else cur = cur.left;
else
cur = cur.left;
}
// 若无待删除节点则直接返回
if (cur == null) return null;
if (cur == null)
return null;
// 子节点数量 = 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 (pre.left == cur) pre.left = child;
else pre.right = child;
if (pre.left == cur)
pre.left = child;
else
pre.right = child;
}
// 子节点数量 = 2
else {
@ -113,7 +130,8 @@ class BinarySearchTree {
/* 获取中序遍历中的下一个节点(仅适用于 root 有左子节点的情况) */
public TreeNode getInOrderNext(TreeNode root) {
if (root == null) return root;
if (root == null)
return root;
// 循环访问左子节点直到叶节点时为最小节点跳出
while (root.left != null) {
root = root.left;

View file

@ -15,7 +15,8 @@ public class binary_tree_dfs {
/* 前序遍历 */
static void preOrder(TreeNode root) {
if (root == null) return;
if (root == null)
return;
// 访问优先级根节点 -> 左子树 -> 右子树
list.add(root.val);
preOrder(root.left);
@ -24,7 +25,8 @@ public class binary_tree_dfs {
/* 中序遍历 */
static void inOrder(TreeNode root) {
if (root == null) return;
if (root == null)
return;
// 访问优先级左子树 -> 根节点 -> 右子树
inOrder(root.left);
list.add(root.val);
@ -33,7 +35,8 @@ public class binary_tree_dfs {
/* 后序遍历 */
static void postOrder(TreeNode root) {
if (root == null) return;
if (root == null)
return;
// 访问优先级左子树 -> 右子树 -> 根节点
postOrder(root.left);
postOrder(root.right);

View file

@ -6,9 +6,7 @@
package include;
/**
* Definition for a singly-linked list node
*/
/* Definition for a singly-linked list node */
public class ListNode {
public int val;
public ListNode next;
@ -17,11 +15,7 @@ public class ListNode {
val = x;
}
/**
* Generate a linked list with an array
* @param arr
* @return
*/
/* Generate a linked list with an array */
public static ListNode arrToLinkedList(int[] arr) {
ListNode dum = new ListNode(0);
ListNode head = dum;
@ -32,12 +26,7 @@ public class ListNode {
return dum.next;
}
/**
* Get a list node with specific value from a linked list
* @param head
* @param val
* @return
*/
/* Get a list node with specific value from a linked list */
public static ListNode getListNode(ListNode head, int val) {
while (head != null && head.val != val) {
head = head.next;

View file

@ -8,7 +8,6 @@ package include;
import java.util.*;
class Trunk {
Trunk prev;
String str;
@ -21,11 +20,7 @@ class Trunk {
public class PrintUtil {
/**
* Print a matrix (Array)
* @param <T>
* @param matrix
*/
/* Print a matrix (Array) */
public static <T> void printMatrix(T[][] matrix) {
System.out.println("[");
for (T[] row : matrix) {
@ -34,11 +29,7 @@ public class PrintUtil {
System.out.println("]");
}
/**
* Print a matrix (List)
* @param <T>
* @param matrix
*/
/* Print a matrix (List) */
public static <T> void printMatrix(List<List<T>> matrix) {
System.out.println("[");
for (List<T> row : matrix) {
@ -47,10 +38,7 @@ public class PrintUtil {
System.out.println("]");
}
/**
* Print a linked list
* @param head
*/
/* Print a linked list */
public static void printLinkedList(ListNode head) {
List<String> list = new ArrayList<>();
while (head != null) {
@ -64,18 +52,12 @@ public class PrintUtil {
* The interface of the tree printer
* This tree printer is borrowed from TECHIE DELIGHT
* https://www.techiedelight.com/c-program-print-binary-tree/
* @param root
*/
public static void printTree(TreeNode root) {
printTree(root, null, false);
}
/**
* Print a binary tree
* @param root
* @param prev
* @param isLeft
*/
/* Print a binary tree */
public static void printTree(TreeNode root, Trunk prev, boolean isLeft) {
if (root == null) {
return;
@ -107,10 +89,7 @@ public class PrintUtil {
printTree(root.left, trunk, false);
}
/**
* Helper function to print branches of the binary tree
* @param p
*/
/* Helper function to print branches of the binary tree */
public static void showTrunks(Trunk p) {
if (p == null) {
return;
@ -120,22 +99,14 @@ public class PrintUtil {
System.out.print(p.str);
}
/**
* Print a hash map
* @param <K>
* @param <V>
* @param map
*/
/* Print a hash map */
public static <K, V> void printHashMap(Map<K, V> map) {
for (Map.Entry <K, V> kv: map.entrySet()) {
for (Map.Entry<K, V> kv : map.entrySet()) {
System.out.println(kv.getKey() + " -> " + kv.getValue());
}
}
/**
* Print a heap (PriorityQueue)
* @param queue
*/
/* Print a heap (PriorityQueue) */
public static void printHeap(Queue<Integer> queue) {
List<Integer> list = new ArrayList<>(queue);
System.out.print("堆的数组表示:");

View file

@ -8,9 +8,7 @@ package include;
import java.util.*;
/**
* Definition for a binary tree node.
*/
/* Definition for a binary tree node. */
public class TreeNode {
public int val; // 节点值
public int height; // 节点高度
@ -21,11 +19,7 @@ public class TreeNode {
val = x;
}
/**
* Generate a binary tree given an array
* @param list
* @return
*/
/* Generate a binary tree given an array */
public static TreeNode listToTree(List<Integer> list) {
int size = list.size();
if (size == 0)
@ -34,14 +28,16 @@ public class TreeNode {
TreeNode root = new TreeNode(list.get(0));
Queue<TreeNode> queue = new LinkedList<>() {{ add(root); }};
int i = 0;
while(!queue.isEmpty()) {
while (!queue.isEmpty()) {
TreeNode node = queue.poll();
if (++i >= size) break;
if (++i >= size)
break;
if (list.get(i) != null) {
node.left = new TreeNode(list.get(i));
queue.add(node.left);
}
if (++i >= size) break;
if (++i >= size)
break;
if (list.get(i) != null) {
node.right = new TreeNode(list.get(i));
queue.add(node.right);
@ -50,23 +46,19 @@ public class TreeNode {
return root;
}
/**
* Serialize a binary tree to a list
* @param root
* @return
*/
/* Serialize a binary tree to a list */
public static List<Integer> treeToList(TreeNode root) {
List<Integer> list = new ArrayList<>();
if(root == null) return list;
if (root == null)
return list;
Queue<TreeNode> queue = new LinkedList<>() {{ add(root); }};
while(!queue.isEmpty()) {
while (!queue.isEmpty()) {
TreeNode node = queue.poll();
if(node != null) {
if (node != null) {
list.add(node.val);
queue.add(node.left);
queue.add(node.right);
}
else {
} else {
list.add(null);
}
}

View file

@ -11,6 +11,7 @@ import java.util.*;
/* 顶点类 */
public class Vertex {
public int val;
public Vertex(int val) {
this.val = val;
}

View file

@ -1,3 +1 @@
__pycache__
test_all.sh