Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
ec0f908
feat(lua): add lua code for chapter computational complexity
fisheryv Nov 10, 2025
14ba261
feat(lua): add lua code for chapter array and linkedlist
fisheryv Nov 11, 2025
190e33b
feat(lua): add lua code in markdown for the first 3 chapters
fisheryv Nov 11, 2025
1a3bb36
feat(lua): add lua code for chapter stack and queue
fisheryv Nov 11, 2025
7b94f5f
feat(lua): add lua code for chapter hashing
fisheryv Nov 12, 2025
f60a14e
feat(lua): add lua code for chapter tree
fisheryv Nov 14, 2025
bf8574e
feat(lua): add lua code for chapter heap
fisheryv Nov 15, 2025
89b87ab
feat(lua): add lua comment style and specification to suggestions.md
fisheryv Nov 15, 2025
efb984b
optimize(lua): unify comment style
fisheryv Nov 15, 2025
1ab2359
feat(lua): add lua code for chapter graph
fisheryv Nov 15, 2025
20501b6
feat(lua): add lua code for chapter searching
fisheryv Nov 15, 2025
fd068ba
feat(lua): add lua code for chapter sorting
fisheryv Nov 15, 2025
173ce9c
feat(lua): add lua code for chapter searching
fisheryv Nov 15, 2025
cea24a2
feat(lua): add lua code for chapter divide and conquer
fisheryv Nov 15, 2025
e15bf6e
feat(lua): add lua code for chapter backtracking
fisheryv Nov 15, 2025
6b17a35
feat(lua): add lua code for chapter dynamic programming
fisheryv Nov 16, 2025
cb34ecc
feat(lua): add lua code for chapter greedy
fisheryv Nov 16, 2025
8755eb6
add lua badge to README
fisheryv Nov 16, 2025
ea6e4f7
add lua development environment description
fisheryv Nov 16, 2025
660c2b7
Merge from remote
fisheryv Jan 28, 2026
ed01ace
Merge branch 'krahets:main' into main
fisheryv Mar 30, 2026
8be6b09
Merge branch 'krahets:main' into main
fisheryv Mar 31, 2026
0ff1938
Merge branch 'krahets:main' into main
fisheryv Apr 15, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
<img src="https://img.shields.io/badge/Kotlin-snow?logo=kotlin&logoColor=7F52FF" alt="" />
<img src="https://img.shields.io/badge/TypeScript-snow?logo=typescript&logoColor=3178C6" alt="" />
<img src="https://img.shields.io/badge/Dart-snow?logo=dart&logoColor=0175C2" alt="" />
<img src="https://img.shields.io/badge/Lua-snow?logo=lua&logoColor=000080" alt="" />
</p>

<p align="center">
Expand Down
131 changes: 131 additions & 0 deletions codes/lua/chapter_array_and_linkedlist/array.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
-- @script array.lua
-- @date 2025-11-11
-- @author fisheryv (yue.fisher2025@gdhfi.com)


--- 随机访问元素
--- @param nums table 数组
--- @return number 随机访问到的元素
local function random_access(nums)
-- 在区间 [1, #nums] 中随机抽取一个数字
local random_index = math.random(1, #nums)
-- 获取并返回随机元素
local random_num = nums[random_index]
return random_num
end

--- 扩展数组长度
--- 请注意,Lua 的 table 是动态的,可以直接扩展
--- 为了方便学习,本函数将 table 看作长度不可变的数组
--- @param nums table 数组
--- @param enlarge number 扩展的长度
--- @return table 扩展后的新数组
local function extend(nums, enlarge)
-- 初始化一个扩展长度后的数组
local res = {}
-- 将原数组中的所有元素复制到新数组
for i = 1, #nums do
res[i] = nums[i]
end
-- 填充新增的位置为0
for i = #nums + 1, #nums + enlarge do
res[i] = 0
end
-- 返回扩展后的新数组
return res
end

--- 在数组的索引 index 处插入元素 num
--- @param nums table 数组
--- @param num number 要插入的元素
--- @param index number 插入位置的索引
local function insert(nums, num, index)
-- 把索引 index 以及之后的所有元素向后移动一位
for i = #nums, index, -1 do
nums[i + 1] = nums[i]
end
-- 将 num 赋给 index 处的元素
nums[index] = num
end

--- 删除索引 index 处的元素
--- @param nums table 数组
--- @param index number 要删除元素的索引
local function remove(nums, index)
-- 把索引 index 之后的所有元素向前移动一位
for i = index, #nums - 1 do
nums[i] = nums[i + 1]
end
-- 将最后一个元素设为nil
nums[#nums] = nil
end

--- 遍历数组
--- @param nums table 数组
local function traverse(nums)
local count = 0
-- 通过索引遍历数组
for i = 1, #nums do
count = count + nums[i]
end
-- 直接遍历数组元素
for _, num in ipairs(nums) do
count = count + num
end
-- 同时遍历数据索引和元素(在 Lua 中 ipairs 已经提供了这个功能)
for i, num in ipairs(nums) do
count = count + nums[i]
count = count + num
end
end

--- 在数组中查找指定元素
--- @param nums table 数组
--- @param target number 要查找的元素
--- @return number 元素的索引,未找到则返回 -1
local function find(nums, target)
for i = 1, #nums do
if nums[i] == target then
return i
end
end
return -1
end

-- Driver Code
local function main()
-- 初始化数组
local arr = { 0, 0, 0, 0, 0 }
print("数组 arr = [" .. table.concat(arr, ", ") .. "]")
local nums = { 1, 3, 2, 5, 4 }
print("数组 nums = [" .. table.concat(nums, ", ") .. "]")

-- 随机访问
local random_num = random_access(nums)
print("在 nums 中获取随机元素 " .. random_num)

-- 长度扩展
nums = extend(nums, 3)
print("将数组长度扩展至 8 ,得到 nums = [" .. table.concat(nums, ", ") .. "]")

-- 插入元素
-- 注意:lua 数组索引从 1 开始,所以其他语言的索引 3 处对应 lua 的索引 4
insert(nums, 6, 4)
print("在索引 4 处插入数字 6 ,得到 nums = [" .. table.concat(nums, ", ") .. "]")

-- 删除元素
-- 注意:lua 数组索引从 1 开始,所以其他语言的索引 2 处对应 lua 的索引 3
remove(nums, 3)
print("删除索引 3 处的元素,得到 nums = [" .. table.concat(nums, ", ") .. "]")

-- 遍历数组
traverse(nums)

-- 查找元素
-- 注意:lua 数组索引从 1 开始,所以元素 3 的索引为 2
local index = find(nums, 3)
print("在 nums 中查找元素 3 ,得到索引 = " .. index)
end

-- 执行主函数
main()
117 changes: 117 additions & 0 deletions codes/lua/chapter_array_and_linkedlist/linked_list.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
-- @script linked_list.lua
-- @date 2025-11-11
-- @author fisheryv (yue.fisher2025@gdhfi.com)


-- 添加模块搜索路径
package.path = package.path .. ";codes/lua/modules/?.lua"

local list_node = require("list_node")
local ListNode = list_node.ListNode

--- 在链表的节点 n0 之后插入节点 P
--- @param n0 ListNode 要插入位置的前一个节点
--- @param P ListNode 要插入的节点
local function insert(n0, P)
local n1 = n0.next
P.next = n1
n0.next = P
end

--- 删除链表的节点 n0 之后的首个节点
--- @param n0 ListNode 要删除节点的前一个节点
local function remove(n0)
-- n0 -> P -> n1
local P = n0.next
if not P then
return
end
local n1 = P.next
n0.next = n1
end

--- 访问链表中索引为 index 的节点
--- @param head ListNode 链表头节点
--- @param index integer 要访问的索引
--- @return ListNode|nil 找到的节点或nil
local function access(head, index)
for _ = 1, index do
if not head then
return nil
end
head = head.next
end
return head
end

--- 在链表中查找值为 target 的首个节点
--- @param head ListNode 链表头节点
--- @param target integer 要查找的目标值
--- @return number 节点索引,未找到返回-1
local function find(head, target)
local index = 0
while head do
if head.val == target then
return index
end
head = head.next
index = index + 1
end
return -1
end

--- 打印链表
--- @param head ListNode 链表头节点
local function printLinkedList(head)
local current = head
local output = {}
while current do
table.insert(output, current.val)
current = current.next
end
print(table.concat(output, " -> "))
end

-- Driver Code
local function main()
-- 初始化链表
-- 初始化各个节点
local n0 = ListNode.new(1)
local n1 = ListNode.new(3)
local n2 = ListNode.new(2)
local n3 = ListNode.new(5)
local n4 = ListNode.new(4)
-- 构建节点之间的引用
n0.next = n1
n1.next = n2
n2.next = n3
n3.next = n4
print("初始化的链表为")
printLinkedList(n0)

-- 插入节点
local p = ListNode.new(0)
insert(n0, p)
print("插入节点后的链表为")
printLinkedList(n0)

-- 删除节点
remove(n0)
print("删除节点后的链表为")
printLinkedList(n0)

-- 访问节点
local node = access(n0, 3)
if node then
print("链表中索引 3 处的节点的值 = " .. node.val)
else
print("链表中索引 3 处的节点不存在")
end

-- 查找节点
local index = find(n0, 2)
print("链表中值为 2 的节点的索引 = " .. index)
end

-- 执行主函数
main()
67 changes: 67 additions & 0 deletions codes/lua/chapter_array_and_linkedlist/list.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
-- @script: array.lua
-- @date 2025-11-11
-- @author fisheryv (yue.fisher2025@gdhfi.com)


-- Driver Code
local function main()
-- 初始化列表
local nums = { 1, 3, 2, 5, 4 }
print("\n列表 nums = [" .. table.concat(nums, ", ") .. "]")

-- 访问元素
-- 注意:lua 数组索引从 1 开始,所以其他语言的索引 1 处对应 lua 的索引 2
local x = nums[2]
print("\n访问索引 2 处的元素,得到 x = " .. x)

-- 更新元素
-- 注意:lua 数组索引从 1 开始,所以其他语言的索引 1 处对应 lua 的索引 2
nums[2] = 0
print("\n将索引 2 处的元素更新为 0 ,得到 nums = [" .. table.concat(nums, ", ") .. "]")

-- 清空列表
nums = {}
print("\n清空列表后 nums = [" .. table.concat(nums, ", ") .. "]")

-- 在尾部添加元素
table.insert(nums, 1)
table.insert(nums, 3)
table.insert(nums, 2)
table.insert(nums, 5)
table.insert(nums, 4)
print("\n添加元素后 nums = [" .. table.concat(nums, ", ") .. "]")

-- 在中间插入元素
-- 注意:lua 数组索引从 1 开始,所以其他语言的索引 3 处对应 lua 的索引 4
table.insert(nums, 4, 6) -- 在索引 4 处插入 6
print("\n在索引 4 处插入数字 6 ,得到 nums = [" .. table.concat(nums, ", ") .. "]")

-- 删除元素
-- 注意:lua 数组索引从 1 开始,所以其他语言的索引 3 处对应 lua 的索引 4
table.remove(nums, 4) -- 删除索引 4 处的元素
print("\n删除索引 4 处的元素,得到 nums = [" .. table.concat(nums, ", ") .. "]")

-- 通过索引遍历列表
local count = 0
for i = 1, #nums do
count = count + nums[i]
end
-- 直接遍历列表元素
for _, num in ipairs(nums) do
count = count + num
end

-- 拼接两个列表
local nums1 = { 6, 8, 7, 10, 9 }
for _, num in ipairs(nums1) do
table.insert(nums, num)
end
print("\n将列表 nums1 拼接到 nums 之后,得到 nums = [" .. table.concat(nums, ", ") .. "]")

-- 排序列表
table.sort(nums)
print("\n排序列表后 nums = [" .. table.concat(nums, ", ") .. "]")
end

-- 执行主函数
main()
Loading