mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-24 03:36:29 +08:00
Add Java codes, and license.
This commit is contained in:
parent
e604470259
commit
8f8f6319af
15 changed files with 952 additions and 17 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -7,5 +7,6 @@
|
|||
# mkdocs files
|
||||
overrides/
|
||||
site/
|
||||
codes/
|
||||
codes/python
|
||||
codes/cpp
|
||||
docs/chapter_*
|
100
codes/java/chapter_array_and_linkedlist/array.java
Normal file
100
codes/java/chapter_array_and_linkedlist/array.java
Normal file
|
@ -0,0 +1,100 @@
|
|||
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++;
|
||||
}
|
||||
// 直接遍历数组
|
||||
for (int num : nums) {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
|
||||
/* 在数组中查找指定元素 */
|
||||
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);
|
||||
}
|
||||
}
|
80
codes/java/chapter_array_and_linkedlist/linked_list.java
Normal file
80
codes/java/chapter_array_and_linkedlist/linked_list.java
Normal file
|
@ -0,0 +1,80 @@
|
|||
package chapter_array_and_linkedlist;
|
||||
|
||||
import include.*;
|
||||
|
||||
public class linked_list {
|
||||
/* 在链表的结点 n0 之后插入结点 P */
|
||||
static void insert(ListNode n0, ListNode P) {
|
||||
ListNode n1 = n0.next;
|
||||
n0.next = P;
|
||||
P.next = n1;
|
||||
}
|
||||
|
||||
/* 删除链表的结点 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++) {
|
||||
head = head.next;
|
||||
if (head == null)
|
||||
return null;
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
62
codes/java/chapter_array_and_linkedlist/list.java
Normal file
62
codes/java/chapter_array_and_linkedlist/list.java
Normal file
|
@ -0,0 +1,62 @@
|
|||
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> list = new ArrayList<>(Arrays.asList(numbers));
|
||||
System.out.println("列表 list = " + Arrays.toString(list.toArray()));
|
||||
|
||||
/* 访问元素 */
|
||||
int num = list.get(1);
|
||||
System.out.println("访问索引 1 处的元素,得到 num = " + num);
|
||||
|
||||
/* 更新元素 */
|
||||
list.set(1, 0);
|
||||
System.out.println("将索引 1 处的元素更新为 0 ,得到 list = " + Arrays.toString(list.toArray()));
|
||||
|
||||
/* 清空列表 */
|
||||
list.clear();
|
||||
System.out.println("清空列表后 list = " + Arrays.toString(list.toArray()));
|
||||
|
||||
/* 尾部添加元素 */
|
||||
list.add(1);
|
||||
list.add(3);
|
||||
list.add(2);
|
||||
list.add(5);
|
||||
list.add(4);
|
||||
System.out.println("添加元素后 list = " + Arrays.toString(list.toArray()));
|
||||
|
||||
/* 中间插入元素 */
|
||||
list.add(3, 6);
|
||||
System.out.println("在索引 3 处插入数字 6 ,得到 list = " + Arrays.toString(list.toArray()));
|
||||
|
||||
/* 删除元素 */
|
||||
list.remove(3);
|
||||
System.out.println("删除索引 3 处的元素,得到 list = " + Arrays.toString(list.toArray()));
|
||||
|
||||
/* 通过索引遍历列表 */
|
||||
int count = 0;
|
||||
for (int i = 0; i < list.size(); i++) {
|
||||
count++;
|
||||
}
|
||||
|
||||
/* 直接遍历列表元素 */
|
||||
count = 0;
|
||||
for (int n : list) {
|
||||
count++;
|
||||
}
|
||||
|
||||
/* 拼接两个列表 */
|
||||
List<Integer> list1 = new ArrayList<>(Arrays.asList(new Integer[] { 6, 8, 7, 10, 9 }));
|
||||
list.addAll(list1);
|
||||
System.out.println("将列表 list1 拼接到 list 之后,得到 list = " + Arrays.toString(list.toArray()));
|
||||
|
||||
/* 排序列表 */
|
||||
Collections.sort(list);
|
||||
System.out.println("排序列表后 list = " + Arrays.toString(list.toArray()));
|
||||
}
|
||||
}
|
136
codes/java/chapter_array_and_linkedlist/my_list.java
Normal file
136
codes/java/chapter_array_and_linkedlist/my_list.java
Normal file
|
@ -0,0 +1,136 @@
|
|||
package chapter_array_and_linkedlist;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/* 列表类简易实现 */
|
||||
class MyList {
|
||||
int[] nums; // 数组(存储列表元素)
|
||||
int initialCapacity = 10; // 列表初始容量
|
||||
int size = 0; // 列表长度(即当前元素数量)
|
||||
int extendRatio = 2; // 每次列表扩容的倍数
|
||||
|
||||
/* 构造函数 */
|
||||
public MyList() {
|
||||
nums = new int[initialCapacity];
|
||||
}
|
||||
|
||||
/* 获取列表容量 */
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
/* 获取列表长度(即当前元素数量) */
|
||||
public int capacity() {
|
||||
return nums.length;
|
||||
}
|
||||
|
||||
/* 访问元素 */
|
||||
public int get(int index) {
|
||||
// 索引如果越界则抛出异常,下同
|
||||
if (index >= size)
|
||||
throw new IndexOutOfBoundsException("索引越界");
|
||||
return nums[index];
|
||||
}
|
||||
|
||||
/* 更新元素 */
|
||||
public void set(int index, int num) {
|
||||
if (index >= size)
|
||||
throw new IndexOutOfBoundsException("索引越界");
|
||||
nums[index] = num;
|
||||
}
|
||||
|
||||
/* 尾部添加元素 */
|
||||
public void add(int num) {
|
||||
// 元素数量超出容量时,触发扩容机制
|
||||
if (size == nums.length)
|
||||
extendCapacity();
|
||||
nums[size] = num;
|
||||
// 更新元素数量
|
||||
size++;
|
||||
}
|
||||
|
||||
/* 中间插入元素 */
|
||||
public void add(int index, int num) {
|
||||
if (index >= size)
|
||||
throw new IndexOutOfBoundsException("索引越界");
|
||||
// 元素数量超出容量时,触发扩容机制
|
||||
if (size == nums.length)
|
||||
extendCapacity();
|
||||
// 索引 i 以及之后的元素都向后移动一位
|
||||
for (int j = size - 1; j >= index; j--) {
|
||||
nums[j + 1] = nums[j];
|
||||
}
|
||||
nums[index] = num;
|
||||
// 更新元素数量
|
||||
size++;
|
||||
}
|
||||
|
||||
/* 删除元素 */
|
||||
public void remove(int index) {
|
||||
if (index >= size)
|
||||
throw new IndexOutOfBoundsException("索引越界");
|
||||
// 索引 i 之后的元素都向前移动一位
|
||||
for (int j = index; j < size - 1; j++) {
|
||||
nums[j] = nums[j + 1];
|
||||
}
|
||||
// 更新元素数量
|
||||
size--;
|
||||
}
|
||||
|
||||
/* 列表扩容 */
|
||||
public void extendCapacity() {
|
||||
// 新建一个长度为 size 的数组,并将原数组拷贝到新数组
|
||||
nums = Arrays.copyOf(nums, nums.length * extendRatio);
|
||||
}
|
||||
|
||||
/* 将列表转换为数组 */
|
||||
public int[] toArray() {
|
||||
int size = size();
|
||||
// 仅转换有效长度范围内的列表元素
|
||||
int[] nums = new int[size];
|
||||
for (int i = 0; i < size; i++) {
|
||||
nums[i] = get(i);
|
||||
}
|
||||
return nums;
|
||||
}
|
||||
}
|
||||
|
||||
public class my_list {
|
||||
/* Driver Code */
|
||||
public static void main(String[] args) {
|
||||
/* 初始化列表 */
|
||||
MyList list = new MyList();
|
||||
/* 尾部添加元素 */
|
||||
list.add(1);
|
||||
list.add(3);
|
||||
list.add(2);
|
||||
list.add(5);
|
||||
list.add(4);
|
||||
System.out.println("列表 list = " + Arrays.toString(list.toArray()) +
|
||||
" ,容量 = " + list.capacity() + " ,长度 = " + list.size());
|
||||
|
||||
/* 中间插入元素 */
|
||||
list.add(3, 6);
|
||||
System.out.println("在索引 3 处插入数字 6 ,得到 list = " + Arrays.toString(list.toArray()));
|
||||
|
||||
/* 删除元素 */
|
||||
list.remove(3);
|
||||
System.out.println("删除索引 3 处的元素,得到 list = " + Arrays.toString(list.toArray()));
|
||||
|
||||
/* 访问元素 */
|
||||
int num = list.get(1);
|
||||
System.out.println("访问索引 1 处的元素,得到 num = " + num);
|
||||
|
||||
/* 更新元素 */
|
||||
list.set(1, 0);
|
||||
System.out.println("将索引 1 处的元素更新为 0 ,得到 list = " + Arrays.toString(list.toArray()));
|
||||
|
||||
/* 测试扩容机制 */
|
||||
for (int i = 0; i < 10; i++) {
|
||||
// 在 i = 5 时,列表长度将超出列表容量,此时触发扩容机制
|
||||
list.add(i);
|
||||
}
|
||||
System.out.println("扩容后的列表 list = " + Arrays.toString(list.toArray()) +
|
||||
" ,容量 = " + list.capacity() + " ,长度 = " + list.size());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,100 @@
|
|||
package chapter_computational_complexity.space_complexity;
|
||||
|
||||
import include.*;
|
||||
import java.util.*;
|
||||
|
||||
public class space_complexity_types {
|
||||
/* 函数 */
|
||||
static int function() {
|
||||
// do something
|
||||
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;
|
||||
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 tree = buildTree(n);
|
||||
PrintUtil.printTree(tree);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,47 @@
|
|||
package chapter_computational_complexity.space_time_tradeoff;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
class solution_brute_force {
|
||||
public int[] twoSum(int[] nums, int target) {
|
||||
int size = nums.length;
|
||||
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];
|
||||
}
|
||||
}
|
||||
|
||||
class solution_hash_map {
|
||||
public int[] twoSum(int[] nums, int target) {
|
||||
int size = nums.length;
|
||||
Map<Integer, Integer> dic = new HashMap<>();
|
||||
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 class leetcode_two_sum {
|
||||
public static void main(String[] args) {
|
||||
// ======= Test Case =======
|
||||
int[] nums = { 2,7,11,15 };
|
||||
int target = 9;
|
||||
|
||||
// ====== Driver Code ======
|
||||
solution_brute_force slt1 = new solution_brute_force();
|
||||
int[] res = slt1.twoSum(nums, target);
|
||||
System.out.println(Arrays.toString(res));
|
||||
|
||||
solution_hash_map slt2 = new solution_hash_map();
|
||||
res = slt2.twoSum(nums, target);
|
||||
System.out.println(Arrays.toString(res));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,149 @@
|
|||
package chapter_computational_complexity.time_complexity;
|
||||
|
||||
public class time_complexity_types {
|
||||
/* 常数阶 */
|
||||
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) {
|
||||
// System.out.println(num);
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
/* 平方阶 */
|
||||
static int quadratic(int n) {
|
||||
int count = 0;
|
||||
for (int i = 0; i < n; i++) {
|
||||
for (int j = 0; j < n; j++) {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
/* 平方阶(冒泡排序) */
|
||||
static void bubbleSort(int[] nums) {
|
||||
int n = nums.length;
|
||||
for (int i = 0; i < n - 1; i++) {
|
||||
for (int j = 0; j < n - 1 - 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 int exponential(int n) {
|
||||
int count = 0, base = 1;
|
||||
// cell 每轮一分为二,形成数列 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(float n) {
|
||||
int count = 0;
|
||||
while (n > 1) {
|
||||
n = n / 2;
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
/* 对数阶(递归实现) */
|
||||
static int logRecur(float n) {
|
||||
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);
|
||||
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);
|
||||
|
||||
count = exponential(n);
|
||||
System.out.println("指数阶(循环实现)的计算操作数量 = " + count);
|
||||
count = expRecur(n);
|
||||
System.out.println("指数阶(递归实现)的计算操作数量 = " + count);
|
||||
|
||||
count = logarithmic((float) n);
|
||||
System.out.println("对数阶(循环实现)的计算操作数量 = " + count);
|
||||
count = logRecur((float) n);
|
||||
System.out.println("对数阶(递归实现)的计算操作数量 = " + count);
|
||||
|
||||
count = linearLogRecur((float) n);
|
||||
System.out.println("线性对数阶(递归实现)的计算操作数量 = " + count);
|
||||
|
||||
count = factorialRecur(n);
|
||||
System.out.println("阶乘阶(递归实现)的计算操作数量 = " + count);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,42 @@
|
|||
package chapter_computational_complexity.time_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++) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
41
codes/java/include/ListNode.java
Executable file
41
codes/java/include/ListNode.java
Executable file
|
@ -0,0 +1,41 @@
|
|||
package include;
|
||||
|
||||
/**
|
||||
* Definition for a singly-linked list node
|
||||
*/
|
||||
public class ListNode {
|
||||
public int val;
|
||||
public ListNode next;
|
||||
|
||||
public ListNode(int x) {
|
||||
val = x;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a linked list with an array
|
||||
* @param arr
|
||||
* @return
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list node with specific value from a linked list
|
||||
* @param head
|
||||
* @param val
|
||||
* @return
|
||||
*/
|
||||
public static ListNode getListNode(ListNode head, int val) {
|
||||
while (head != null && head.val != val) {
|
||||
head = head.next;
|
||||
}
|
||||
return head;
|
||||
}
|
||||
}
|
88
codes/java/include/PrintUtil.java
Executable file
88
codes/java/include/PrintUtil.java
Executable file
|
@ -0,0 +1,88 @@
|
|||
package include;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
class Trunk {
|
||||
Trunk prev;
|
||||
String str;
|
||||
|
||||
Trunk(Trunk prev, String str) {
|
||||
this.prev = prev;
|
||||
this.str = str;
|
||||
}
|
||||
};
|
||||
|
||||
public class PrintUtil {
|
||||
/**
|
||||
* Print a linked list
|
||||
* @param head
|
||||
*/
|
||||
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));
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
public static void printTree(TreeNode root, Trunk prev, boolean isLeft) {
|
||||
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 (isLeft) {
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function to print branches of the binary tree
|
||||
* @param p
|
||||
*/
|
||||
public static void showTrunks(Trunk p) {
|
||||
if (p == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
showTrunks(p.prev);
|
||||
System.out.print(p.str);
|
||||
}
|
||||
}
|
80
codes/java/include/TreeNode.java
Executable file
80
codes/java/include/TreeNode.java
Executable file
|
@ -0,0 +1,80 @@
|
|||
package include;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* Definition for a binary tree node.
|
||||
*/
|
||||
public class TreeNode {
|
||||
public int val;
|
||||
public TreeNode left;
|
||||
public TreeNode right;
|
||||
|
||||
public TreeNode(int x) {
|
||||
val = x;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a binary tree with an array
|
||||
* @param arr
|
||||
* @return
|
||||
*/
|
||||
public static TreeNode arrToTree(Integer[] arr) {
|
||||
TreeNode root = new TreeNode(arr[0]);
|
||||
Queue<TreeNode> queue = new LinkedList<>() {{ add(root); }};
|
||||
int i = 1;
|
||||
while(!queue.isEmpty()) {
|
||||
TreeNode node = queue.poll();
|
||||
if(arr[i] != null) {
|
||||
node.left = new TreeNode(arr[i]);
|
||||
queue.add(node.left);
|
||||
}
|
||||
i++;
|
||||
if(arr[i] != null) {
|
||||
node.right = new TreeNode(arr[i]);
|
||||
queue.add(node.right);
|
||||
}
|
||||
i++;
|
||||
}
|
||||
return root;
|
||||
}
|
||||
|
||||
/**
|
||||
* Serialize a binary tree to a list
|
||||
* @param root
|
||||
* @return
|
||||
*/
|
||||
public static List<Integer> treeToList(TreeNode root) {
|
||||
List<Integer> list = new ArrayList<>();
|
||||
if(root == null) return list;
|
||||
Queue<TreeNode> queue = new LinkedList<>() {{ add(root); }};
|
||||
while(!queue.isEmpty()) {
|
||||
TreeNode node = queue.poll();
|
||||
if(node != null) {
|
||||
list.add(node.val);
|
||||
queue.add(node.left);
|
||||
queue.add(node.right);
|
||||
}
|
||||
else {
|
||||
list.add(null);
|
||||
}
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a tree node with specific value in a binary tree
|
||||
* @param root
|
||||
* @param val
|
||||
* @return
|
||||
*/
|
||||
public static TreeNode getTreeNode(TreeNode root, int val) {
|
||||
if (root == null)
|
||||
return null;
|
||||
if (root.val == val)
|
||||
return root;
|
||||
TreeNode left = getTreeNode(root.left, val);
|
||||
TreeNode right = getTreeNode(root.right, val);
|
||||
return left != null ? left : right;
|
||||
}
|
||||
}
|
|
@ -3,5 +3,5 @@ cd site
|
|||
git init
|
||||
git add -A
|
||||
git commit -m 'deploy'
|
||||
git push -f git@github.com:krahets/dsa-021.git master:gh-pages
|
||||
git push -f git@github.com:krahets/hello-algo.git master:gh-pages
|
||||
cd -
|
||||
|
|
|
@ -1,17 +1,23 @@
|
|||
---
|
||||
comments: true
|
||||
hide:
|
||||
- footer
|
||||
---
|
||||
|
||||
# Data Structure And Algorithm: From 0 to 1
|
||||
# Hello,算法!
|
||||
|
||||
## Update Log
|
||||
TODO
|
||||
|
||||
To be updated...
|
||||
## 更新日志
|
||||
|
||||
| Chapter | Date |
|
||||
| 更新内容 | 日期 |
|
||||
| ------------ | ---------- |
|
||||
| 数组与链表 | 2022-10-15 |
|
||||
| 数据结构简介 | 2022-10-20 |
|
||||
| 前言 | 2022-10-23 |
|
||||
| 计算复杂度 | 2022-11-03 |
|
||||
| 新增:算法无处不在 | 2022-10-10 |
|
||||
| 新增:数组与链表 | 2022-10-15 |
|
||||
| 新增:数据结构简介 | 2022-10-20 |
|
||||
| 新增:前言 | 2022-10-23 |
|
||||
| 新增:计算复杂度 | 2022-11-03 |
|
||||
| 更新:配图 | 2022-11-04 |
|
||||
| 新增:数据与内存 | 2022-11-05 |
|
||||
| 更新:各章节 Java 代码 | 2022-11-06 |
|
||||
| 更新:列表 Java 代码、配图 | 2022-11-07 |
|
||||
|
|
17
mkdocs.yml
17
mkdocs.yml
|
@ -1,11 +1,11 @@
|
|||
# Project information
|
||||
site_name: 算法入门速成
|
||||
site_url: https://krahets.github.io/dsa-021/
|
||||
site_name: Hello 算法
|
||||
site_url: https://krahets.github.io/hello-algo/
|
||||
site_author: Krahets
|
||||
site_description: Your first book to learn Data Structure And Algorithm.
|
||||
# Repository
|
||||
repo_name: krahets/dsa-021
|
||||
repo_url: https://github.com/krahets/dsa-021
|
||||
repo_name: krahets/hello-algo
|
||||
repo_url: https://github.com/krahets/hello-algo
|
||||
|
||||
# Copyright
|
||||
copyright: Copyright © 2020 - 2022 Krahets
|
||||
|
@ -115,17 +115,18 @@ extra_css:
|
|||
nav:
|
||||
- 关于本书:
|
||||
- chapter_about/index.md
|
||||
- 前言:
|
||||
- 算法无处不在:
|
||||
- chapter_introduction/index.md
|
||||
- 计算复杂度:
|
||||
- chapter_computational_complexity/index.md
|
||||
- 算法效率评估: chapter_computational_complexity/performance_evaluation.md
|
||||
- 时间复杂度: chapter_computational_complexity/time_complexity.md
|
||||
- 空间复杂度: chapter_computational_complexity/space_complexity.md
|
||||
- 权衡时间与空间: chapter_computational_complexity/space_time_tradeoff.md
|
||||
- 小结: chapter_computational_complexity/summary.md
|
||||
- 数据结构简介:
|
||||
- chapter_data_structure/index.md
|
||||
- 数据与内存: chapter_data_structure/computer_memory.md
|
||||
- 数据与内存: chapter_data_structure/data_and_memory.md
|
||||
- 数据结构分类: chapter_data_structure/classification_of_data_strcuture.md
|
||||
- 小结: chapter_data_structure/summary.md
|
||||
- 数组与链表:
|
||||
|
@ -133,4 +134,6 @@ nav:
|
|||
- 数组: chapter_array_and_linkedlist/array.md
|
||||
- 链表: chapter_array_and_linkedlist/linked_list.md
|
||||
- 列表: chapter_array_and_linkedlist/list.md
|
||||
- 小结: chapter_array_and_linkedlist/summary.md
|
||||
- 小结: chapter_array_and_linkedlist/summary.md
|
||||
- License:
|
||||
- chapter_license/index.md
|
Loading…
Reference in a new issue