Skip to content

Commit

Permalink
修正代码中 tab 占位混用问题
Browse files Browse the repository at this point in the history
  • Loading branch information
itcharge committed Dec 15, 2021
1 parent 99d1599 commit 0e85802
Show file tree
Hide file tree
Showing 8 changed files with 291 additions and 291 deletions.
88 changes: 44 additions & 44 deletions Contents/00.Introduction/02.Algorithm-Complexity.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@

```Python
def algorithm(n):
fact = 1
for i in range(1, n + 1):
fact *= i
return fact
fact = 1
for i in range(1, n + 1):
fact *= i
return fact
```

把上述算法中所有语句的执行次数加起来 $1 + n + n + 1 = 2n + 2$,可以用一个函数 $f(n)$ 来表达语句的执行次数:$f(n) = 2n + 2$。
Expand Down Expand Up @@ -122,10 +122,10 @@ $O(1)$ 只是常数阶时间复杂度的一种表示方式,并不是指只执

```Python
def algorithm(n):
a = 1
b = 2
res = a * b + n
return res
a = 1
b = 2
res = a * b + n
return res
```

上述代码虽然有 4 行,但时间复杂度也是 $O(1)$,而不是 $O(3)$。
Expand All @@ -136,10 +136,10 @@ def algorithm(n):

```Python
def algorithm(n):
sum = 0
for i in range(n):
sum += 1
return sum
sum = 0
for i in range(n):
sum += 1
return sum
```

上述代码中 `sum += 1` 的执行次数为 n 次,所以这段代码的时间复杂度为 $O(n)$。
Expand All @@ -151,11 +151,11 @@ def algorithm(n):

```Python
def algorithm(n):
res = 0
for i in range(n):
for j in range(n):
res += 1
return res
res = 0
for i in range(n):
for j in range(n):
res += 1
return res
```

上述代码中,`res += 1` 在两重循环中,根据时间复杂度的乘法原理,这段代码的执行次数为 $n^2$ 次,所以其时间复杂度为 $O(n^2)$。
Expand All @@ -166,9 +166,9 @@ def algorithm(n):

```Python
def algorithm(n):
if n <= 0:
return 1
return n * algorithm(n - 1)
if n <= 0:
return 1
return n * algorithm(n - 1)
```

上述代码中计算阶乘使用了递归的方法。计算 `n` 的阶乘时需要先计算出 `n - 1` 的阶乘,计算 `n - 1` 的阶乘时,需要计算出 `n - 2` 的阶乘,以此类推。在计算总的时间复杂度时需要将每一步的基本操作数相乘,即:$n * (n - 1) * (n - 2) * ... * 2 * 1 = n!$,这段代码的执行次数为 $n!$ 次,所以其时间复杂度为 $O(n!)$。
Expand All @@ -179,10 +179,10 @@ def algorithm(n):

```Python
def algorithm(n):
cnt = 1
while cnt < n:
cnt *= 2
return cnt
cnt = 1
while cnt < n:
cnt *= 2
return cnt
```

上述代码中 `cnt = 1` 的时间复杂度为 O(1) 可以忽略不算。while 循环体中 `cnt` 从 1 开始,每循环一次都乘以 2。当大于 n 时循环结束。变量 `cnt` 的取值是一个等比数列:$2^0,2^1,2^2,…,2^x$,根据 $2^x = n$,可以得出这段循环体的执行次数为 $log_2n$。所以这段代码的时间复杂度为 $O(log_2n)$。
Expand All @@ -194,13 +194,13 @@ def algorithm(n):

```Python
def algorithm(n):
cnt = 1
res = 0
while cnt < n:
cnt *= 2
for i in range(n):
res += 1
return res
cnt = 1
res = 0
while cnt < n:
cnt *= 2
for i in range(n):
res += 1
return res
```

上述代码中外层循环的时间复杂度为 $O(log_2 n)$,内层循环的时间复杂度为 $O(n)$,且两层循环相互独立,则总体时间复杂度为 $O(n log_2 n)$。
Expand All @@ -222,12 +222,12 @@ def algorithm(n):

```Python
def find(nums, val):
pos = -1
for i in range(n):
if nums[i] == val:
pos = i
break
return pos
pos = -1
for i in range(n):
if nums[i] == val:
pos = i
break
return pos
```

这段代码要实现的功能是:从一个整数数组 `nums` 中查找值为 `val` 的变量出现的位置。如果不考虑 `break` 语句,根据「2.3 时间复杂度计算」中讲的分析步骤,这个算法的时间复杂度是 $O(n)$,其中 n 代表数组的长度。
Expand Down Expand Up @@ -260,10 +260,10 @@ def find(nums, val):

```Python
def algorithm(n):
a = 1
b = 2
res = a * b + n
return res
a = 1
b = 2
res = a * b + n
return res
```

上述代码中使用 `a``b``res` 3 个局部变量,其所占空间大小并不会随着问题规模 n 的在增大而增大,所以该算法的空间复杂度为 $O(1)$。
Expand All @@ -272,9 +272,9 @@ def algorithm(n):

```Python
def algorithm(n):
if n <= 0:
return 1
return n * algorithm(n - 1)
if n <= 0:
return 1
return n * algorithm(n - 1)
```

上述代码采用了递归调用的方式。每次递归调用都占用了 1 个栈帧空间,总共调用了 n 次,所以该算法的空间复杂度为 $O(n)$。
Expand Down
6 changes: 3 additions & 3 deletions Contents/01.Array/01.Array-Basic/01.Array-Basic.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ arr = ['python', 'java', ['asp', 'php'], 'c']
```Python
# 从数组 nums 中读取下标为 i 的数据元素值
def value(nums, i):
if 0 <= i <= len(nums) - 1:
print(nums[i])
if 0 <= i <= len(nums) - 1:
print(nums[i])
arr = [0, 5, 2, 3, 7, 1, 6]
value(arr, 3)
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ class Solution:
````Python
# ...
while left < right:
# ...
return left if nums[left] == target else -1
# ...
return left if nums[left] == target else -1
````

此外,用 `left < right` 的话还有一个好处,就是退出循环的时候 `left == right` 成立,就不用判断应该返回 `left` 还是 `right` 了。
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ class Solution:
slow = 0
fast = 1
while 没有遍历完:
if 满足要求的特殊条件:
if 满足要求的特殊条件:
slow += 1
fast += 1
return 合适的值
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,14 @@
```Python
# 链节点类
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
def __init__(self, val=0, next=None):
self.val = val
self.next = next

# 链表类
class LinkedList:
def __init__(self):
self.head = None
def __init__(self):
self.head = None
```

### 2.2 建立一个线性链表
Expand Down Expand Up @@ -329,7 +329,7 @@ def removeInside(self, index):

if not cur:
return 'Error'
del_node = cur.next
cur.next = del_node.next
```
Expand Down
138 changes: 69 additions & 69 deletions Contents/03.Stack/01.Stack-Basic/01.Stack-Basic.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,42 +72,42 @@

```Python
class Stack:
# 初始化空栈
def __init__(self, size=100):
self.stack = []
self.size = size
self.top = -1
# 判断栈是否为空
def is_empty(self):
return self.top == -1
# 判断栈是否已满
def is_full(self):
return self.top + 1 == self.size
# 入栈操作
def push(self, value):
if self.is_full():
raise Exception('Stack is full')
else:
self.stack.append(value)
self.top += 1
# 出栈操作
def pop(self):
if self.is_empty():
raise Exception('Stack is empty')
else:
self.top -= 1
self.stack.pop()
# 获取栈顶元素
def peek(self):
if self.is_empty():
raise Exception('Stack is empty')
else:
return self.stack[self.top]
# 初始化空栈
def __init__(self, size=100):
self.stack = []
self.size = size
self.top = -1
# 判断栈是否为空
def is_empty(self):
return self.top == -1
# 判断栈是否已满
def is_full(self):
return self.top + 1 == self.size
# 入栈操作
def push(self, value):
if self.is_full():
raise Exception('Stack is full')
else:
self.stack.append(value)
self.top += 1
# 出栈操作
def pop(self):
if self.is_empty():
raise Exception('Stack is empty')
else:
self.top -= 1
self.stack.pop()
# 获取栈顶元素
def peek(self):
if self.is_empty():
raise Exception('Stack is empty')
else:
return self.stack[self.top]
```

### 2.3 堆栈的链式存储实现
Expand All @@ -130,40 +130,40 @@ class Stack:

```Python
class Node:
def __init__(self, value):
self.value = value
self.next = None
def __init__(self, value):
self.value = value
self.next = None
class Stack:
# 初始化空栈
def __init__(self):
self.top = None
# 判断栈是否为空
def is_empty(self):
return self.top == None
# 入栈操作
def push(self, value):
cur = Node(value)
cur.next = self.top
self.top = cur
# 出栈操作
def pop(self):
if self.is_empty():
raise Exception('Stack is empty')
else:
cur = self.top
self.top = self.top.next
del cur
# 获取栈顶元素
def peek(self):
if self.is_empty():
raise Exception('Stack is empty')
else:
return self.top.value
# 初始化空栈
def __init__(self):
self.top = None
# 判断栈是否为空
def is_empty(self):
return self.top == None
# 入栈操作
def push(self, value):
cur = Node(value)
cur.next = self.top
self.top = cur
# 出栈操作
def pop(self):
if self.is_empty():
raise Exception('Stack is empty')
else:
cur = self.top
self.top = self.top.next
del cur
# 获取栈顶元素
def peek(self):
if self.is_empty():
raise Exception('Stack is empty')
else:
return self.top.value
```

## 3. 堆栈的应用
Expand Down
Loading

0 comments on commit 0e85802

Please sign in to comment.