Skip to content

Commit

Permalink
Format the Java codes with the Reat Hat extension.
Browse files Browse the repository at this point in the history
  • Loading branch information
krahets committed Apr 13, 2023
1 parent 7273ee2 commit f851345
Show file tree
Hide file tree
Showing 39 changed files with 195 additions and 205 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@ docs/overrides/
build/
site/
utils/

# test script
test.sh
13 changes: 6 additions & 7 deletions codes/java/chapter_array_and_linkedlist/array.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -79,26 +78,26 @@ public static void main(String[] args) {
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);
Expand Down
2 changes: 1 addition & 1 deletion codes/java/chapter_array_and_linkedlist/linked_list.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ static int find(ListNode head, int target) {
/* Driver Code */
public static void main(String[] args) {
/* 初始化链表 */
// 初始化各个节点
// 初始化各个节点
ListNode n0 = new ListNode(1);
ListNode n1 = new ListNode(3);
ListNode n2 = new ListNode(2);
Expand Down
14 changes: 7 additions & 7 deletions codes/java/chapter_array_and_linkedlist/my_list.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@

/* 列表类简易实现 */
class MyList {
private int[] nums; // 数组(存储列表元素)
private int capacity = 10; // 列表容量
private int size = 0; // 列表长度(即当前元素数量)
private int extendRatio = 2; // 每次列表扩容的倍数
private int[] nums; // 数组(存储列表元素)
private int capacity = 10; // 列表容量
private int size = 0; // 列表长度(即当前元素数量)
private int extendRatio = 2; // 每次列表扩容的倍数

/* 构造方法 */
public MyList() {
nums = new int[capacity];
}

/* 获取列表长度(即当前元素数量)*/
/* 获取列表长度(即当前元素数量) */
public int size() {
return size;
}
Expand Down Expand Up @@ -118,7 +118,7 @@ public static void main(String[] args) {
list.add(5);
list.add(4);
System.out.println("列表 list = " + Arrays.toString(list.toArray()) +
" ,容量 = " + list.capacity() + " ,长度 = " + list.size());
" ,容量 = " + list.capacity() + " ,长度 = " + list.size());

/* 中间插入元素 */
list.insert(3, 6);
Expand All @@ -142,6 +142,6 @@ public static void main(String[] args) {
list.add(i);
}
System.out.println("扩容后的列表 list = " + Arrays.toString(list.toArray()) +
" ,容量 = " + list.capacity() + " ,长度 = " + list.size());
" ,容量 = " + list.capacity() + " ,长度 = " + list.size());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

import java.util.*;


public class leetcode_two_sum {
/* 方法一:暴力枚举 */
static int[] twoSumBruteForce(int[] nums, int target) {
Expand Down Expand Up @@ -40,9 +39,9 @@ static int[] twoSumHashTable(int[] nums, int target) {

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 ======
// 方法一
int[] res = twoSumBruteForce(nums, target);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ static int function() {
// do something
return 0;
}

/* 常数阶 */
static void constant(int n) {
// 常量、变量、对象占用 O(1) 空间
Expand Down Expand Up @@ -52,7 +52,8 @@ static void linear(int n) {
/* 线性阶(递归实现) */
static void linearRecur(int n) {
System.out.println("递归 n = " + n);
if (n == 1) return;
if (n == 1)
return;
linearRecur(n - 1);
}

Expand All @@ -73,7 +74,8 @@ static void quadratic(int n) {

/* 平方阶(递归实现) */
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);
Expand All @@ -82,7 +84,8 @@ static int quadraticRecur(int n) {

/* 指数阶(建立满二叉树) */
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);
Expand Down
24 changes: 14 additions & 10 deletions codes/java/chapter_computational_complexity/time_complexity.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ static int linear(int n) {
count++;
return count;
}

/* 线性阶(遍历数组) */
static int arrayTraversal(int[] nums) {
int count = 0;
Expand All @@ -48,7 +48,7 @@ static int quadratic(int n) {

/* 平方阶(冒泡排序) */
static int bubbleSort(int[] nums) {
int count = 0; // 计数器
int count = 0; // 计数器
// 外循环:待排序元素数量为 n-1, n-2, ..., 1
for (int i = nums.length - 1; i > 0; i--) {
// 内循环:冒泡操作
Expand All @@ -58,7 +58,7 @@ static int bubbleSort(int[] nums) {
int tmp = nums[j];
nums[j] = nums[j + 1];
nums[j + 1] = tmp;
count += 3; // 元素交换包含 3 个单元操作
count += 3; // 元素交换包含 3 个单元操作
}
}
}
Expand All @@ -81,7 +81,8 @@ static int exponential(int n) {

/* 指数阶(递归实现) */
static int expRecur(int n) {
if (n == 1) return 1;
if (n == 1)
return 1;
return expRecur(n - 1) + expRecur(n - 1) + 1;
}

Expand All @@ -97,15 +98,17 @@ static int logarithmic(float n) {

/* 对数阶(递归实现) */
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;
int count = linearLogRecur(n / 2) +
linearLogRecur(n / 2);
if (n <= 1)
return 1;
int count = linearLogRecur(n / 2) +
linearLogRecur(n / 2);
for (int i = 0; i < n; i++) {
count++;
}
Expand All @@ -114,7 +117,8 @@ static int linearLogRecur(float n) {

/* 阶乘阶(递归实现) */
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++) {
Expand All @@ -141,7 +145,7 @@ public static void main(String[] args) {
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]
nums[i] = n - i; // [n,n-1,...,2,1]
count = bubbleSort(nums);
System.out.println("平方阶(冒泡排序)的计算操作数量 = " + count);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ static int findOne(int[] nums) {
}
return -1;
}

/* Driver Code */
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
Expand Down
2 changes: 1 addition & 1 deletion codes/java/chapter_graph/graph_adjacency_list.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ 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] },
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初始化后,图为");
Expand Down
2 changes: 1 addition & 1 deletion codes/java/chapter_graph/graph_adjacency_matrix.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

/* 基于邻接矩阵实现的无向图类 */
class GraphAdjMat {
List<Integer> vertices; // 顶点列表,元素代表“顶点值”,索引代表“顶点索引”
List<Integer> vertices; // 顶点列表,元素代表“顶点值”,索引代表“顶点索引”
List<List<Integer>> adjMat; // 邻接矩阵,行列索引对应“顶点索引”

/* 构造方法 */
Expand Down
2 changes: 1 addition & 1 deletion codes/java/chapter_graph/graph_bfs.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ static List<Vertex> graphBFS(GraphAdjList graph, Vertex startVet) {
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] },
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);
Expand Down
2 changes: 1 addition & 1 deletion codes/java/chapter_graph/graph_dfs.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ static List<Vertex> graphDFS(GraphAdjList graph, Vertex startVet) {
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] },
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初始化后,图为");
Expand Down
20 changes: 11 additions & 9 deletions codes/java/chapter_hashing/array_hash_map.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
class Entry {
public int key;
public String val;

public Entry(int key, String val) {
this.key = key;
this.val = val;
Expand All @@ -21,6 +22,7 @@ public Entry(int key, String val) {
/* 基于数组简易实现的哈希表 */
class ArrayHashMap {
private List<Entry> buckets;

public ArrayHashMap() {
// 初始化数组,包含 100 个桶
buckets = new ArrayList<>();
Expand All @@ -39,7 +41,8 @@ private int hashFunc(int key) {
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;
}

Expand Down Expand Up @@ -89,23 +92,22 @@ public List<String> valueSet() {

/* 打印哈希表 */
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) {
/* 初始化哈希表 */
ArrayHashMap map = new ArrayHashMap();

/* 添加操作 */
// 在哈希表中添加键值对 (key, value)
map.put(12836, "小哈");
map.put(15937, "小啰");
map.put(16750, "小算");
map.put(12836, "小哈");
map.put(15937, "小啰");
map.put(16750, "小算");
map.put(13276, "小法");
map.put(10583, "小鸭");
System.out.println("\n添加完成后,哈希表为\nKey -> Value");
Expand All @@ -124,15 +126,15 @@ public static void main(String[] args) {

/* 遍历哈希表 */
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);
}
}
Expand Down
12 changes: 6 additions & 6 deletions codes/java/chapter_hashing/hash_map.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ public static void main(String[] args) {

/* 添加操作 */
// 在哈希表中添加键值对 (key, value)
map.put(12836, "小哈");
map.put(15937, "小啰");
map.put(16750, "小算");
map.put(12836, "小哈");
map.put(15937, "小啰");
map.put(16750, "小算");
map.put(13276, "小法");
map.put(10583, "小鸭");
System.out.println("\n添加完成后,哈希表为\nKey -> Value");
Expand All @@ -37,15 +37,15 @@ public static void main(String[] args) {

/* 遍历哈希表 */
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);
}
}
Expand Down
1 change: 0 additions & 1 deletion codes/java/chapter_heap/heap.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import include.*;
import java.util.*;


public class heap {
public static void testPush(Queue<Integer> heap, int val) {
heap.offer(val); // 元素入堆
Expand Down
Loading

0 comments on commit f851345

Please sign in to comment.