Skip to content

Commit

Permalink
更新代码格式
Browse files Browse the repository at this point in the history
  • Loading branch information
itcharge committed Aug 15, 2023
1 parent ee4ffcc commit 963fa66
Show file tree
Hide file tree
Showing 888 changed files with 2,018 additions and 2,018 deletions.
20 changes: 10 additions & 10 deletions Contents/00.Introduction/02.Algorithm-Complexity.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@

下面通过一个具体例子来说明一下如何计算时间复杂度。

```Python
```python
def algorithm(n):
fact = 1
for i in range(1, n + 1):
Expand Down Expand Up @@ -120,7 +120,7 @@ $\Theta$ 符号渐进地给出了一个函数的上界和下界,如果我们

$O(1)$ 只是常数阶时间复杂度的一种表示方式,并不是指只执行了一行代码。只要代码的执行时间不随着问题规模 n 的增大而增长,这样的算法时间复杂度都记为 $O(1)$。

```Python
```python
def algorithm(n):
a = 1
b = 2
Expand All @@ -134,7 +134,7 @@ def algorithm(n):

一般含有非嵌套循环,且单层循环下的语句执行次数为 n 的算法涉及线性时间复杂度。这类算法随着问题规模 n 的增大,对应计算次数呈线性增长。

```Python
```python
def algorithm(n):
sum = 0
for i in range(n):
Expand All @@ -149,7 +149,7 @@ def algorithm(n):

一般含有双层嵌套,且每层循环下的语句执行次数为 n 的算法涉及平方时间复杂度。这类算法随着问题规模 n 的增大,对应计算次数呈平方关系增长。

```Python
```python
def algorithm(n):
res = 0
for i in range(n):
Expand All @@ -164,7 +164,7 @@ def algorithm(n):

阶乘时间复杂度一般出现在与「全排列」、「旅行商问题暴力解法」相关的算法中。这类算法随着问题规模 n 的增大,对应计算次数呈阶乘关系增长。

```Python
```python
def permutations(arr, start, end):
if start == end:
print(arr)
Expand All @@ -182,7 +182,7 @@ def permutations(arr, start, end):

对数时间复杂度一般出现在「二分查找」、「分治」这种一分为二的算法中。这类算法随着问题规模 n 的增大,对应的计算次数呈对数关系增长。

```Python
```python
def algorithm(n):
cnt = 1
while cnt < n:
Expand All @@ -197,7 +197,7 @@ def algorithm(n):

线性对数一般出现在排序算法中,例如「快速排序」、「归并排序」、「堆排序」等。这类算法随着问题规模 n 的增大,对应的计算次数呈线性对数关系增长。

```Python
```python
def algorithm(n):
cnt = 1
res = 0
Expand Down Expand Up @@ -225,7 +225,7 @@ def algorithm(n):

我们通过一个例子来分析下最佳、最坏、最差时间复杂度。

```Python
```python
def find(nums, val):
pos = -1
for i in range(n):
Expand Down Expand Up @@ -263,7 +263,7 @@ def find(nums, val):

#### 3.1.1 常数 O(1)

```Python
```python
def algorithm(n):
a = 1
b = 2
Expand All @@ -275,7 +275,7 @@ def algorithm(n):

#### 3.1.2 线性 O(n)

```Python
```python
def algorithm(n):
if n <= 0:
return 1
Expand Down
4 changes: 2 additions & 2 deletions Contents/00.Introduction/03.LeetCode-Guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ LeetCode 提供了题目的搜索过滤功能。可以筛选相关题单、不

1. 思路一:暴力搜索

```Python
```python
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
size = len(nums)
Expand All @@ -127,7 +127,7 @@ class Solution:

2. 思路二:哈希表

```Python
```python
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
size = len(nums)
Expand Down
16 changes: 8 additions & 8 deletions Contents/01.Array/01.Array-Basic/01.Array-Basic.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ int[][] arr = new int[3][]{ {1,2,3}, {4,5}, {6,7,8,9}};

原生 `Python` 中其实没有数组的概念,而是使用了类似 `Java` 中的 `ArrayList` 容器类数据结构,叫做列表。通常我们把列表来作为 `Python` 中的数组使用。`Python` 中列表存储的数据类型可以不一致,数组长度也可以不一致。例如:

```Python
```python
arr = ['python', 'java', ['asp', 'php'], 'c']
```

Expand All @@ -80,7 +80,7 @@ arr = ['python', 'java', ['asp', 'php'], 'c']

示例代码如下:

```Python
```python
# 从数组 nums 中读取下标为 i 的数据元素值
def value(nums, i):
if 0 <= i <= len(nums) - 1:
Expand All @@ -96,7 +96,7 @@ value(arr, 3)

示例代码如下:

```Python
```python
# 从数组 nums 中查找元素值为 val 的数据元素第一次出现的位置
def find(nums, val):
for i in range(len(nums)):
Expand All @@ -120,7 +120,7 @@ print(find(arr, 5))

示例代码如下:

```Python
```python
arr = [0, 5, 2, 3, 7, 1, 6]
val = 4
arr.append(val)
Expand All @@ -135,7 +135,7 @@ print(arr)

示例代码如下:

```Python
```python
arr = [0, 5, 2, 3, 7, 1, 6]
i, val = 2, 4
arr.insert(i, val)
Expand All @@ -150,7 +150,7 @@ print(arr)

示例代码如下:

```Python
```python
def change(nums, i, val):
if 0 <= i <= len(nums) - 1:
nums[i] = val
Expand All @@ -173,7 +173,7 @@ print(arr)

示例代码如下:

```Python
```python
arr = [0, 5, 2, 3, 7, 1, 6]
arr.pop()
print(arr)
Expand All @@ -198,7 +198,7 @@ print(arr)

示例代码如下:

```Python
```python
arr = [0, 5, 2, 3, 7, 1, 6]
i = 3
arr.remove(5)
Expand Down
2 changes: 1 addition & 1 deletion Contents/01.Array/02.Array-Sort/01.Array-Bubble-Sort.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@

## 5. 冒泡排序代码实现

```Python
```python
class Solution:
def bubbleSort(self, arr):
# 第 i 趟排序
Expand Down
2 changes: 1 addition & 1 deletion Contents/01.Array/02.Array-Sort/02.Array-Selection-Sort.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@

## 5. 选择排序代码实现

```Python
```python
class Solution:
def selectionSort(self, arr):
for i in range(len(arr) - 1):
Expand Down
2 changes: 1 addition & 1 deletion Contents/01.Array/02.Array-Sort/03.Array-Insertion-Sort.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@

## 5. 插入排序代码实现

```Python
```python
class Solution:
def insertionSort(self, arr):
# 遍历无序序列
Expand Down
2 changes: 1 addition & 1 deletion Contents/01.Array/02.Array-Sort/04.Array-Shell-Sort.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

## 5. 希尔排序代码实现

```Python
```python
class Solution:
def shellSort(self, arr):
size = len(arr)
Expand Down
2 changes: 1 addition & 1 deletion Contents/01.Array/02.Array-Sort/05.Array-Merge-Sort.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@

## 5. 归并排序代码实现

```Python
```python
class Solution:
def merge(self, left_arr, right_arr): # 归并过程
arr = []
Expand Down
2 changes: 1 addition & 1 deletion Contents/01.Array/02.Array-Sort/06.Array-Quick-Sort.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@

## 5. 快速排序代码实现

```Python
```python
import random

class Solution:
Expand Down
2 changes: 1 addition & 1 deletion Contents/01.Array/02.Array-Sort/07.Array-Heap-Sort.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@

## 6. 堆排序代码实现

```Python
```python
class Solution:
# 调整为大顶堆
def heapify(self, arr: [int], index: int, end: int):
Expand Down
2 changes: 1 addition & 1 deletion Contents/01.Array/02.Array-Sort/08.Array-Counting-Sort.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

## 5. 计数排序代码实现

```Python
```python
class Solution:
def countingSort(self, arr):
# 计算待排序序列中最大值元素 arr_max 和最小值元素 arr_min
Expand Down
2 changes: 1 addition & 1 deletion Contents/01.Array/02.Array-Sort/09.Array-Bucket-Sort.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

## 5. 桶排序代码实现

```Python
```python
class Solution:
def insertionSort(self, arr):
# 遍历无序序列
Expand Down
2 changes: 1 addition & 1 deletion Contents/01.Array/02.Array-Sort/10.Array-Radix-Sort.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

## 5. 基数排序代码实现

```Python
```python
class Solution:
def radixSort(self, arr):
# 桶的大小为所有元素的最大位数
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@

**示例**

```Python
```python
输入: nums = [-1,0,3,5,9,12], target = 9
输出: 4
解释: 9 出现在 nums 中并且下标为 4
Expand All @@ -75,7 +75,7 @@

#### 思路 1:代码

```Python
```python
class Solution:
def search(self, nums: List[int], target: int) -> int:
left, right = 0, len(nums) - 1
Expand Down Expand Up @@ -147,7 +147,7 @@ class Solution:

可以在返回的时候需要增加一层判断,判断 `left` 所指向位置是否等于目标元素,如果是的话就返回 `left`,如果不是的话返回 `-1`。即:

````Python
````python
# ...
while left < right:
# ...
Expand Down Expand Up @@ -187,7 +187,7 @@ class Solution:

#### 代码:

```Python
```python
class Solution:
def search(self, nums: List[int], target: int) -> int:
left, right = 0, len(nums) - 1
Expand Down Expand Up @@ -229,7 +229,7 @@ class Solution:

#### 第一种代码:

```Python
```python
class Solution:
def search(self, nums: List[int], target: int) -> int:
left, right = 0, len(nums) - 1
Expand All @@ -250,7 +250,7 @@ class Solution:

#### 第二种代码:

```Python
```python
class Solution:
def search(self, nums: List[int], target: int) -> int:
left, right = 0, len(nums) - 1
Expand Down
Loading

0 comments on commit 963fa66

Please sign in to comment.