Skip to content

Commit

Permalink
1. Add the building util of Python
Browse files Browse the repository at this point in the history
for the markdown docs.
2. Update the deploy.sh
  • Loading branch information
krahets committed Feb 6, 2023
1 parent 64f251f commit ea901af
Show file tree
Hide file tree
Showing 28 changed files with 295 additions and 936 deletions.
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
hello-algo.iml

# mkdocs files
site/
.cache/
scripts/
docs/overrides/

src/
2 changes: 1 addition & 1 deletion codes/python/chapter_hashing/array_hash_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

""" 键值对 int->String """
class Entry:
def __init__(self, key: int, val: str):
def __init__(self, key, val):
self.key = key
self.val = val

Expand Down
11 changes: 3 additions & 8 deletions codes/python/chapter_tree/binary_search_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,9 @@ def insert(self, num: int) -> Optional[TreeNode]:
# 若树为空,直接提前返回
if root is None:
return None

cur = root
pre = None


# 循环查找,越过叶结点后跳出
cur, pre = root, None
while cur is not None:
# 找到重复结点,直接返回
if cur.val == num:
Expand Down Expand Up @@ -86,10 +84,8 @@ def remove(self, num: int) -> Optional[TreeNode]:
if root is None:
return None

cur = root
pre = None

# 循环查找,越过叶结点后跳出
cur, pre = root, None
while cur is not None:
# 找到待删除结点,跳出循环
if cur.val == num:
Expand All @@ -99,7 +95,6 @@ def remove(self, num: int) -> Optional[TreeNode]:
cur = cur.right
else: # 待删除结点在 cur 的左子树中
cur = cur.left

# 若无待删除结点,则直接返回
if cur is None:
return None
Expand Down
8 changes: 0 additions & 8 deletions deploy.sh

This file was deleted.

51 changes: 6 additions & 45 deletions docs/chapter_array_and_linkedlist/array.md
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,7 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
=== "Python"

```python title="array.py"
""" 随机访问元素 """
def random_access(nums):
# 在区间 [0, len(nums)-1] 中随机抽取一个数字
random_index = random.randint(0, len(nums) - 1)
# 获取并返回随机元素
random_num = nums[random_index]
return random_num
[class]{}-[func]{random_access}
```

=== "Go"
Expand Down Expand Up @@ -279,17 +273,7 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
=== "Python"

```python title="array.py"
""" 扩展数组长度 """
# 请注意,Python 的 list 是动态数组,可以直接扩展
# 为了方便学习,本函数将 list 看作是长度不可变的数组
def extend(nums, enlarge):
# 初始化一个扩展长度后的数组
res = [0] * (len(nums) + enlarge)
# 将原数组中的所有元素复制到新数组
for i in range(len(nums)):
res[i] = nums[i]
# 返回扩展后的新数组
return res
[class]{}-[func]{extend}
```

=== "Go"
Expand Down Expand Up @@ -452,19 +436,9 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
=== "Python"

```python title="array.py"
""" 在数组的索引 index 处插入元素 num """
def insert(nums, num, index):
# 把索引 index 以及之后的所有元素向后移动一位
for i in range(len(nums) - 1, index, -1):
nums[i] = nums[i - 1]
# 将 num 赋给 index 处元素
nums[index] = num
[class]{}-[func]{insert}

""" 删除索引 index 处元素 """
def remove(nums, index):
# 把索引 index 之后的所有元素向前移动一位
for i in range(index, len(nums) - 1):
nums[i] = nums[i + 1]
[class]{}-[func]{remove}
```

=== "Go"
Expand Down Expand Up @@ -648,15 +622,7 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
=== "Python"

```python title="array.py"
""" 遍历数组 """
def traverse(nums):
count = 0
# 通过索引遍历数组
for i in range(len(nums)):
count += 1
# 直接遍历数组
for num in nums:
count += 1
[class]{}-[func]{traverse}
```

=== "Go"
Expand Down Expand Up @@ -803,12 +769,7 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
=== "Python"

```python title="array.py"
""" 在数组中查找指定元素 """
def find(nums, target):
for i in range(len(nums)):
if nums[i] == target:
return i
return -1
[class]{}-[func]{find}
```

=== "Go"
Expand Down
33 changes: 4 additions & 29 deletions docs/chapter_array_and_linkedlist/linked_list.md
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -369,20 +369,9 @@ comments: true
=== "Python"

```python title="linked_list.py"
""" 在链表的结点 n0 之后插入结点 P """
def insert(n0, P):
n1 = n0.next
n0.next = P
P.next = n1
[class]{}-[func]{insert}

""" 删除链表的结点 n0 之后的首个结点 """
def remove(n0):
if not n0.next:
return
# n0 -> P -> n1
P = n0.next
n1 = P.next
n0.next = n1
[class]{}-[func]{remove}
```

=== "Go"
Expand Down Expand Up @@ -557,13 +546,7 @@ comments: true
=== "Python"

```python title="linked_list.py"
""" 访问链表中索引为 index 的结点 """
def access(head, index):
for _ in range(index):
if not head:
return None
head = head.next
return head
[class]{}-[func]{access}
```

=== "Go"
Expand Down Expand Up @@ -704,15 +687,7 @@ comments: true
=== "Python"

```python title="linked_list.py"
""" 在链表中查找值为 target 的首个结点 """
def find(head, target):
index = 0
while head:
if head.val == target:
return index
head = head.next
index += 1
return -1
[class]{}-[func]{find}
```

=== "Go"
Expand Down
63 changes: 1 addition & 62 deletions docs/chapter_array_and_linkedlist/list.md
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -912,68 +912,7 @@ comments: true
=== "Python"

```python title="my_list.py"
""" 列表类简易实现 """
class MyList:
""" 构造函数 """
def __init__(self):
self.__capacity = 10 # 列表容量
self.__nums = [0] * self.__capacity # 数组(存储列表元素)
self.__size = 0 # 列表长度(即当前元素数量)
self.__extend_ratio = 2 # 每次列表扩容的倍数

""" 获取列表长度(即当前元素数量) """
def size(self):
return self.__size
""" 获取列表容量 """
def capacity(self):
return self.__capacity
""" 访问元素 """
def get(self, index):
# 索引如果越界则抛出异常,下同
assert index >= 0 and index < self.__size, "索引越界"
return self.__nums[index]

""" 更新元素 """
def set(self, num, index):
assert index >= 0 and index < self.__size, "索引越界"
self.__nums[index] = num

""" 中间插入(尾部添加)元素 """
def add(self, num, index=-1):
assert index >= 0 and index < self.__size, "索引越界"
# 若不指定索引 index ,则向数组尾部添加元素
if index == -1:
index = self.__size
# 元素数量超出容量时,触发扩容机制
if self.__size == self.capacity():
self.extend_capacity()
# 索引 i 以及之后的元素都向后移动一位
for j in range(self.__size - 1, index - 1, -1):
self.__nums[j + 1] = self.__nums[j]
self.__nums[index] = num
# 更新元素数量
self.__size += 1

""" 删除元素 """
def remove(self, index):
assert index >= 0 and index < self.__size, "索引越界"
num = self.nums[index]
# 索引 i 之后的元素都向前移动一位
for j in range(index, self.__size - 1):
self.__nums[j] = self.__nums[j + 1]
# 更新元素数量
self.__size -= 1
# 返回被删除元素
return num

""" 列表扩容 """
def extend_capacity(self):
# 新建一个长度为 self.__size 的数组,并将原数组拷贝到新数组
self.__nums = self.__nums + [0] * self.capacity() * (self.__extend_ratio - 1)
# 更新列表容量
self.__capacity = len(self.__nums)
[class]{MyList}-[func]{}
```

=== "Go"
Expand Down
48 changes: 6 additions & 42 deletions docs/chapter_computational_complexity/space_complexity.md
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -624,18 +624,7 @@ $$
=== "Python"

```python title="space_complexity.py"
""" 常数阶 """
def constant(n):
# 常量、变量、对象占用 O(1) 空间
a = 0
nums = [0] * 10000
node = ListNode(0)
# 循环中的变量占用 O(1) 空间
for _ in range(n):
c = 0
# 循环中的函数占用 O(1) 空间
for _ in range(n):
function()
[class]{}-[func]{constant}
```

=== "Go"
Expand Down Expand Up @@ -829,14 +818,7 @@ $$
=== "Python"

```python title="space_complexity.py"
""" 线性阶 """
def linear(n):
# 长度为 n 的列表占用 O(n) 空间
nums = [0] * n
# 长度为 n 的哈希表占用 O(n) 空间
mapp = {}
for i in range(n):
mapp[i] = str(i)
[class]{}-[func]{linear}
```

=== "Go"
Expand Down Expand Up @@ -996,11 +978,7 @@ $$
=== "Python"

```python title="space_complexity.py"
""" 线性阶(递归实现) """
def linear_recur(n):
print("递归 n =", n)
if n == 1: return
linear_recur(n - 1)
[class]{}-[func]{linear_recur}
```

=== "Go"
Expand Down Expand Up @@ -1127,10 +1105,7 @@ $$
=== "Python"

```python title="space_complexity.py"
""" 平方阶 """
def quadratic(n):
# 二维列表占用 O(n^2) 空间
num_matrix = [[0] * n for _ in range(n)]
[class]{}-[func]{quadratic}
```

=== "Go"
Expand Down Expand Up @@ -1272,12 +1247,7 @@ $$
=== "Python"

```python title="space_complexity.py"
""" 平方阶(递归实现) """
def quadratic_recur(n):
if n <= 0: return 0
# 数组 nums 长度为 n, n-1, ..., 2, 1
nums = [0] * n
return quadratic_recur(n - 1)
[class]{}-[func]{quadratic_recur}
```

=== "Go"
Expand Down Expand Up @@ -1400,13 +1370,7 @@ $$
=== "Python"

```python title="space_complexity.py"
""" 指数阶(建立满二叉树) """
def build_tree(n):
if n == 0: return None
root = TreeNode(0)
root.left = build_tree(n - 1)
root.right = build_tree(n - 1)
return root
[class]{}-[func]{build_tree}
```

=== "Go"
Expand Down
20 changes: 2 additions & 18 deletions docs/chapter_computational_complexity/space_time_tradeoff.md
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,7 @@ comments: true
=== "Python"

```python title="leetcode_two_sum.py"
class SolutionBruteForce:
def twoSum(self, nums: List[int], target: int) -> List[int]:
# 两层循环,时间复杂度 O(n^2)
for i in range(len(nums) - 1):
for j in range(i + 1, len(nums)):
if nums[i] + nums[j] == target:
return i, j
return []
[class]{SolutionBruteForce}-[func]{}
```

=== "Go"
Expand Down Expand Up @@ -247,16 +240,7 @@ comments: true
=== "Python"

```python title="leetcode_two_sum.py"
class SolutionHashMap:
def twoSum(self, nums: List[int], target: int) -> List[int]:
# 辅助哈希表,空间复杂度 O(n)
dic = {}
# 单层循环,时间复杂度 O(n)
for i in range(len(nums)):
if target - nums[i] in dic:
return dic[target - nums[i]], i
dic[nums[i]] = i
return []
[class]{SolutionHashMap}-[func]{}
```

=== "Go"
Expand Down
Loading

0 comments on commit ea901af

Please sign in to comment.