@ -1,4 +0,0 @@
|
||||
{
|
||||
"tabWidth": 4,
|
||||
"useTabs": false
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
FROM python:3.9.0-alpine
|
||||
|
||||
RUN pip install -i https://pypi.tuna.tsinghua.edu.cn/simple --upgrade pip
|
||||
RUN pip install -i https://pypi.tuna.tsinghua.edu.cn/simple mkdocs-material==9.0.2
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
COPY codes /app/codes
|
||||
COPY docs /app/docs
|
||||
COPY mkdocs.yml /app/mkdocs.yml
|
||||
|
||||
RUN mkdir ./docs/overrides && mkdocs build
|
||||
|
||||
EXPOSE 8000
|
||||
|
||||
CMD ["mkdocs", "serve", "-a", "0.0.0.0:8000"]
|
@ -0,0 +1,67 @@
|
||||
/**
|
||||
* File: my_heap.java
|
||||
* Created Time: 2023-01-07
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package chapter_heap;
|
||||
|
||||
import include.*;
|
||||
import java.util.*;
|
||||
|
||||
|
||||
public class heap {
|
||||
public static void testPush(Queue<Integer> heap, int val) {
|
||||
heap.add(val); // 元素入堆
|
||||
System.out.format("\n元素 %d 入堆后\n", val);
|
||||
PrintUtil.printHeap(heap);
|
||||
}
|
||||
|
||||
public static void testPoll(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) -> { return 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);
|
||||
|
||||
/* 堆顶元素出堆 */
|
||||
testPoll(maxHeap);
|
||||
testPoll(maxHeap);
|
||||
testPoll(maxHeap);
|
||||
testPoll(maxHeap);
|
||||
testPoll(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);
|
||||
}
|
||||
}
|
@ -0,0 +1,177 @@
|
||||
/**
|
||||
* File: my_heap.java
|
||||
* Created Time: 2023-01-07
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package chapter_heap;
|
||||
|
||||
import include.*;
|
||||
import java.util.*;
|
||||
|
||||
class MaxHeap {
|
||||
// 使用列表而非数组,这样无需考虑扩容问题
|
||||
private List<Integer> maxHeap;
|
||||
|
||||
/* 构造函数,建立空堆 */
|
||||
public MaxHeap() {
|
||||
maxHeap = new ArrayList<>();
|
||||
}
|
||||
|
||||
/* 构造函数,根据输入列表建堆 */
|
||||
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 a = maxHeap.get(i),
|
||||
b = maxHeap.get(j),
|
||||
tmp = a;
|
||||
maxHeap.set(i, b);
|
||||
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 poll() {
|
||||
// 判空处理
|
||||
if (isEmpty())
|
||||
throw new EmptyStackException();
|
||||
// 交换根结点与最右叶结点(即交换首元素与尾元素)
|
||||
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 testPush(MaxHeap maxHeap, int val) {
|
||||
maxHeap.push(val); // 元素入堆
|
||||
System.out.format("\n添加元素 %d 后\n", val);
|
||||
maxHeap.print();
|
||||
}
|
||||
|
||||
public static void testPoll(MaxHeap maxHeap) {
|
||||
int val = maxHeap.poll(); // 堆顶元素出堆
|
||||
System.out.format("\n出堆元素为 %d\n", val);
|
||||
maxHeap.print();
|
||||
}
|
||||
|
||||
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.poll();
|
||||
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);
|
||||
}
|
||||
}
|
@ -0,0 +1,117 @@
|
||||
// File: array.zig
|
||||
// Created Time: 2023-01-07
|
||||
// Author: sjinzh (sjinzh@gmail.com)
|
||||
|
||||
const std = @import("std");
|
||||
const inc = @import("include");
|
||||
|
||||
// 随机返回一个数组元素
|
||||
pub fn randomAccess(nums: []i32) i32 {
|
||||
// 在区间 [0, nums.len) 中随机抽取一个整数
|
||||
var randomIndex = std.crypto.random.intRangeLessThan(usize, 0, nums.len);
|
||||
// 获取并返回随机元素
|
||||
var randomNum = nums[randomIndex];
|
||||
return randomNum;
|
||||
}
|
||||
|
||||
// 扩展数组长度
|
||||
pub fn extend(mem_allocator: std.mem.Allocator, nums: []i32, enlarge: usize) ![]i32 {
|
||||
// 初始化一个扩展长度后的数组
|
||||
var res = try mem_allocator.alloc(i32, nums.len + enlarge);
|
||||
std.mem.set(i32, res, 0);
|
||||
// 将原数组中的所有元素复制到新数组
|
||||
std.mem.copy(i32, res, nums);
|
||||
// 返回扩展后的新数组
|
||||
return res;
|
||||
}
|
||||
|
||||
// 在数组的索引 index 处插入元素 num
|
||||
pub fn insert(nums: []i32, num: i32, index: usize) void {
|
||||
// 把索引 index 以及之后的所有元素向后移动一位
|
||||
var i = nums.len - 1;
|
||||
while (i > index) : (i -= 1) {
|
||||
nums[i] = nums[i - 1];
|
||||
}
|
||||
// 将 num 赋给 index 处元素
|
||||
nums[index] = num;
|
||||
}
|
||||
|
||||
// 删除索引 index 处元素
|
||||
pub fn remove(nums: []i32, index: usize) void {
|
||||
// 把索引 index 之后的所有元素向前移动一位
|
||||
var i = index;
|
||||
while (i < nums.len - 1) : (i += 1) {
|
||||
nums[i] = nums[i + 1];
|
||||
}
|
||||
}
|
||||
|
||||
// 遍历数组
|
||||
pub fn traverse(nums: []i32) void {
|
||||
var count: i32 = 0;
|
||||
// 通过索引遍历数组
|
||||
var i: i32 = 0;
|
||||
while (i < nums.len) : (i += 1) {
|
||||
count += 1;
|
||||
}
|
||||
count = 0;
|
||||
// 直接遍历数组
|
||||
for (nums) |_| {
|
||||
count += 1;
|
||||
}
|
||||
}
|
||||
|
||||
// 在数组中查找指定元素
|
||||
pub fn find(nums: []i32, target: i32) i32 {
|
||||
for (nums) |num, i| {
|
||||
if (num == target) return @intCast(i32, i);
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Driver Code
|
||||
pub fn main() !void {
|
||||
// 初始化数组
|
||||
const size: i32 = 5;
|
||||
var arr = [_]i32{0} ** size;
|
||||
std.debug.print("数组 arr = ", .{});
|
||||
inc.PrintUtil.printArray(i32, &arr);
|
||||
|
||||
var array = [_]i32{ 1, 3, 2, 5, 4 };
|
||||
std.debug.print("\n数组 nums = ", .{});
|
||||
inc.PrintUtil.printArray(i32, &array);
|
||||
|
||||
// 随机访问
|
||||
var randomNum = randomAccess(&array);
|
||||
std.debug.print("\n在 nums 中获取随机元素 {}", .{randomNum});
|
||||
|
||||
// 长度扩展
|
||||
var known_at_runtime_zero: usize = 0;
|
||||
var nums: []i32 = array[known_at_runtime_zero..array.len];
|
||||
var mem_arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
|
||||
defer mem_arena.deinit();
|
||||
const mem_allocator = mem_arena.allocator();
|
||||
nums = try extend(mem_allocator, nums, 3);
|
||||
std.debug.print("\n将数组长度扩展至 8 ,得到 nums = ", .{});
|
||||
inc.PrintUtil.printArray(i32, nums);
|
||||
|
||||
// 插入元素
|
||||
insert(nums, 6, 3);
|
||||
std.debug.print("\n在索引 3 处插入数字 6 ,得到 nums = ", .{});
|
||||
inc.PrintUtil.printArray(i32, nums);
|
||||
|
||||
// 删除元素
|
||||
remove(nums, 2);
|
||||
std.debug.print("\n删除索引 2 处的元素,得到 nums = ", .{});
|
||||
inc.PrintUtil.printArray(i32, nums);
|
||||
|
||||
// 遍历数组
|
||||
traverse(nums);
|
||||
|
||||
// 查找元素
|
||||
var index = find(nums, 3);
|
||||
std.debug.print("\n在 nums 中查找元素 3 ,得到索引 = {}\n", .{index});
|
||||
|
||||
const getchar = try std.io.getStdIn().reader().readByte();
|
||||
_ = getchar;
|
||||
}
|
||||
|
@ -0,0 +1,85 @@
|
||||
// File: linked_list.zig
|
||||
// Created Time: 2023-01-07
|
||||
// Author: sjinzh (sjinzh@gmail.com)
|
||||
|
||||
const std = @import("std");
|
||||
const inc = @import("include");
|
||||
|
||||
// 在链表的结点 n0 之后插入结点 P
|
||||
pub fn insert(n0: ?*inc.ListNode(i32), P: ?*inc.ListNode(i32)) void {
|
||||
var n1 = n0.?.next;
|
||||
n0.?.next = P;
|
||||
P.?.next = n1;
|
||||
}
|
||||
|
||||
// 删除链表的结点 n0 之后的首个结点
|
||||
pub fn remove(n0: ?*inc.ListNode(i32)) void {
|
||||
if (n0.?.next == null) return;
|
||||
// n0 -> P -> n1
|
||||
var P = n0.?.next;
|
||||
var n1 = P.?.next;
|
||||
n0.?.next = n1;
|
||||
}
|
||||
|
||||
// 访问链表中索引为 index 的结点
|
||||
pub fn access(node: ?*inc.ListNode(i32), index: i32) ?*inc.ListNode(i32) {
|
||||
var head = node;
|
||||
var i: i32 = 0;
|
||||
while (i < index) : (i += 1) {
|
||||
head = head.?.next;
|
||||
if (head == null) return null;
|
||||
}
|
||||
return head;
|
||||
}
|
||||
|
||||
// 在链表中查找值为 target 的首个结点
|
||||
pub fn find(node: ?*inc.ListNode(i32), target: i32) i32 {
|
||||
var head = node;
|
||||
var index: i32 = 0;
|
||||
while (head != null) {
|
||||
if (head.?.val == target) return index;
|
||||
head = head.?.next;
|
||||
index += 1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Driver Code
|
||||
pub fn main() !void {
|
||||
// 初始化链表
|
||||
// 初始化各个结点
|
||||
var n0 = inc.ListNode(i32){.val = 1};
|
||||
var n1 = inc.ListNode(i32){.val = 3};
|
||||
var n2 = inc.ListNode(i32){.val = 2};
|
||||
var n3 = inc.ListNode(i32){.val = 5};
|
||||
var n4 = inc.ListNode(i32){.val = 4};
|
||||
// 构建引用指向
|
||||
n0.next = &n1;
|
||||
n1.next = &n2;
|
||||
n2.next = &n3;
|
||||
n3.next = &n4;
|
||||
std.debug.print("初始化的链表为", .{});
|
||||
try inc.PrintUtil.printLinkedList(i32, &n0);
|
||||
|
||||
// 插入结点
|
||||
var tmp = inc.ListNode(i32){.val = 0};
|
||||
insert(&n0, &tmp);
|
||||
std.debug.print("插入结点后的链表为", .{});
|
||||
try inc.PrintUtil.printLinkedList(i32, &n0);
|
||||
|
||||
// 删除结点
|
||||
remove(&n0);
|
||||
std.debug.print("删除结点后的链表为", .{});
|
||||
try inc.PrintUtil.printLinkedList(i32, &n0);
|
||||
|
||||
// 访问结点
|
||||
var node = access(&n0, 3);
|
||||
std.debug.print("链表中索引 3 处的结点的值 = {}\n", .{node.?.val});
|
||||
|
||||
// 查找结点
|
||||
var index = find(&n0, 2);
|
||||
std.debug.print("链表中值为 2 的结点的索引 = {}\n", .{index});
|
||||
|
||||
const getchar = try std.io.getStdIn().reader().readByte();
|
||||
_ = getchar;
|
||||
}
|
@ -0,0 +1,81 @@
|
||||
// File: list.zig
|
||||
// Created Time: 2023-01-07
|
||||
// Author: sjinzh (sjinzh@gmail.com)
|
||||
|
||||
const std = @import("std");
|
||||
const inc = @import("include");
|
||||
|
||||
// Driver Code
|
||||
pub fn main() !void {
|
||||
// 初始化列表
|
||||
var list = std.ArrayList(i32).init(std.heap.page_allocator);
|
||||
// 延迟释放内存
|
||||
defer list.deinit();
|
||||
try list.appendSlice(&[_]i32{ 1, 3, 2, 5, 4 });
|
||||
std.debug.print("列表 list = ", .{});
|
||||
inc.PrintUtil.printList(i32, list);
|
||||
|
||||
// 访问元素
|
||||
var num = list.items[1];
|
||||
std.debug.print("\n访问索引 1 处的元素,得到 num = {}", .{num});
|
||||
|
||||
// 更新元素
|
||||
list.items[1] = 0;
|
||||
std.debug.print("\n将索引 1 处的元素更新为 0 ,得到 list = ", .{});
|
||||
inc.PrintUtil.printList(i32, list);
|
||||
|
||||
// 清空列表
|
||||
list.clearRetainingCapacity();
|
||||
std.debug.print("\n清空列表后 list = ", .{});
|
||||
inc.PrintUtil.printList(i32, list);
|
||||
|
||||
// 尾部添加元素
|
||||
try list.append(1);
|
||||
try list.append(3);
|
||||
try list.append(2);
|
||||
try list.append(5);
|
||||
try list.append(4);
|
||||
std.debug.print("\n添加元素后 list = ", .{});
|
||||
inc.PrintUtil.printList(i32, list);
|
||||
|
||||
// 中间插入元素
|
||||
try list.insert(3, 6);
|
||||
std.debug.print("\n在索引 3 处插入数字 6 ,得到 list = ", .{});
|
||||
inc.PrintUtil.printList(i32, list);
|
||||
|
||||
// 删除元素
|
||||
var value = list.orderedRemove(3);
|
||||
_ = value;
|
||||
std.debug.print("\n删除索引 3 处的元素,得到 list = ", .{});
|
||||
inc.PrintUtil.printList(i32, list);
|
||||
|
||||
// 通过索引遍历列表
|
||||
var count: i32 = 0;
|
||||
var i: i32 = 0;
|
||||
while (i < list.items.len) : (i += 1) {
|
||||
count += 1;
|
||||
}
|
||||
|
||||
// 直接遍历列表元素
|
||||
count = 0;
|
||||
for (list.items) |_| {
|
||||
count += 1;
|
||||
}
|
||||
|
||||
// 拼接两个列表
|
||||
var list1 = std.ArrayList(i32).init(std.heap.page_allocator);
|
||||
defer list1.deinit();
|
||||
try list1.appendSlice(&[_]i32{ 6, 8, 7, 10, 9 });
|
||||
try list.insertSlice(list.items.len, list1.items);
|
||||
std.debug.print("\n将列表 list1 拼接到 list 之后,得到 list = ", .{});
|
||||
inc.PrintUtil.printList(i32, list);
|
||||
|
||||
// 排序列表
|
||||
std.sort.sort(i32, list.items, {}, comptime std.sort.asc(i32));
|
||||
std.debug.print("\n排序列表后 list = ", .{});
|
||||
inc.PrintUtil.printList(i32, list);
|
||||
|
||||
const getchar = try std.io.getStdIn().reader().readByte();
|
||||
_ = getchar;
|
||||
}
|
||||
|
@ -0,0 +1,7 @@
|
||||
version: '3'
|
||||
services:
|
||||
hello-algo:
|
||||
build: .
|
||||
ports:
|
||||
- "8000:8000"
|
||||
|
After Width: | Height: | Size: 69 KiB |
After Width: | Height: | Size: 89 KiB |
After Width: | Height: | Size: 66 KiB |
After Width: | Height: | Size: 69 KiB |
After Width: | Height: | Size: 81 KiB |
After Width: | Height: | Size: 89 KiB |
After Width: | Height: | Size: 85 KiB |
After Width: | Height: | Size: 94 KiB |
After Width: | Height: | Size: 87 KiB |
After Width: | Height: | Size: 96 KiB |
After Width: | Height: | Size: 69 KiB |
After Width: | Height: | Size: 66 KiB |
After Width: | Height: | Size: 81 KiB |
After Width: | Height: | Size: 84 KiB |
After Width: | Height: | Size: 85 KiB |
After Width: | Height: | Size: 85 KiB |
After Width: | Height: | Size: 64 KiB |
After Width: | Height: | Size: 80 KiB |
After Width: | Height: | Size: 113 KiB |