mirror of
https://github.com/krahets/hello-algo.git
synced 2025-11-02 04:31:55 +08:00
Bug fixes and improvements (#1298)
* Fix is_empty() implementation in the stack and queue chapter * Update en/CONTRIBUTING.md * Remove "剩余" from the state definition of knapsack problem * Sync zh and zh-hant versions * Update the stylesheets of code tabs * Fix quick_sort.rb * Fix TS code * Update chapter_paperbook * Upload the manuscript of 0.1 section * Fix binary_tree_dfs.rb * Bug fixes * Update README * Update README * Update README * Update README.md * Update README * Sync zh and zh-hant versions * Bug fixes
This commit is contained in:
@ -18,7 +18,7 @@ class ArrayStack:
|
||||
|
||||
def is_empty(self) -> bool:
|
||||
"""判断栈是否为空"""
|
||||
return self._stack == []
|
||||
return self.size() == 0
|
||||
|
||||
def push(self, item: int):
|
||||
"""入栈"""
|
||||
|
||||
@ -30,7 +30,7 @@ class LinkedListDeque:
|
||||
|
||||
def is_empty(self) -> bool:
|
||||
"""判断双向队列是否为空"""
|
||||
return self.size() == 0
|
||||
return self._size == 0
|
||||
|
||||
def push(self, num: int, is_front: bool):
|
||||
"""入队操作"""
|
||||
|
||||
@ -26,7 +26,7 @@ class LinkedListQueue:
|
||||
|
||||
def is_empty(self) -> bool:
|
||||
"""判断队列是否为空"""
|
||||
return not self._front
|
||||
return self._size == 0
|
||||
|
||||
def push(self, num: int):
|
||||
"""入队"""
|
||||
|
||||
@ -25,7 +25,7 @@ class LinkedListStack:
|
||||
|
||||
def is_empty(self) -> bool:
|
||||
"""判断栈是否为空"""
|
||||
return not self._peek
|
||||
return self._size == 0
|
||||
|
||||
def push(self, val: int):
|
||||
"""入栈"""
|
||||
|
||||
@ -136,16 +136,17 @@ class QuickSortTailCall
|
||||
end
|
||||
|
||||
### Driver Code ###
|
||||
|
||||
if __FILE__ == $0
|
||||
# 快速排序
|
||||
nums = [2, 4, 1, 0, 3, 5]
|
||||
QuickSort.quick_sort(nums, 0, nums.length - 1)
|
||||
puts "快速排序完成后 nums = #{nums}"
|
||||
|
||||
# 快速排序(中位基准数优化)
|
||||
nums1 = [2, 4, 1, 0, 3, 5]
|
||||
QuickSortMedian.quick_sort(nums1, 0, nums1.length - 1)
|
||||
puts "快速排序(中位基准数优化)完成后 nums1 = #{nums1}"
|
||||
|
||||
# 快速排序(尾递归优化)
|
||||
nums2 = [2, 4, 1, 0, 3, 5]
|
||||
QuickSortTailCall.quick_sort(nums2, 0, nums2.length - 1)
|
||||
|
||||
@ -8,13 +8,13 @@ require_relative '../utils/tree_node'
|
||||
require_relative '../utils/print_util'
|
||||
|
||||
### 前序遍历 ###
|
||||
def pre_oder(root)
|
||||
def pre_order(root)
|
||||
return if root.nil?
|
||||
|
||||
# 访问优先级:根节点 -> 左子树 -> 右子树
|
||||
$res << root.val
|
||||
pre_oder(root.left)
|
||||
pre_oder(root.right)
|
||||
pre_order(root.left)
|
||||
pre_order(root.right)
|
||||
end
|
||||
|
||||
### 中序遍历 ###
|
||||
@ -47,7 +47,7 @@ if __FILE__ == $0
|
||||
|
||||
# 前序遍历
|
||||
$res = []
|
||||
pre_oder(root)
|
||||
pre_order(root)
|
||||
puts "\n前序遍历的节点打印序列 = #{$res}"
|
||||
|
||||
# 中序遍历
|
||||
|
||||
@ -51,4 +51,4 @@ const res = subsetSumI(nums, target);
|
||||
console.log(`输入数组 nums = ${JSON.stringify(nums)}, target = ${target}`);
|
||||
console.log(`所有和等于 ${target} 的子集 res = ${JSON.stringify(res)}`);
|
||||
|
||||
export { };
|
||||
export {};
|
||||
|
||||
@ -56,4 +56,4 @@ const res = subsetSumII(nums, target);
|
||||
console.log(`输入数组 nums = ${JSON.stringify(nums)}, target = ${target}`);
|
||||
console.log(`所有和等于 ${target} 的子集 res = ${JSON.stringify(res)}`);
|
||||
|
||||
export { };
|
||||
export {};
|
||||
|
||||
@ -67,4 +67,4 @@ console.log(`尾递归函数的求和结果 res = ${res}`);
|
||||
res = fib(n);
|
||||
console.log(`斐波那契数列的第 ${n} 项为 ${res}`);
|
||||
|
||||
export { };
|
||||
export {};
|
||||
|
||||
Reference in New Issue
Block a user