Skip to content

Commit

Permalink
算法基础
Browse files Browse the repository at this point in the history
  • Loading branch information
carla-cn committed Nov 7, 2023
1 parent 3b6af84 commit 82c33b4
Show file tree
Hide file tree
Showing 8 changed files with 1,088 additions and 1,417 deletions.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
"clong",
"Fprintln",
"imgs",
"logn",
"nlogn",
"Println",
"Sincos",
"strconv",
"struct",
"Trunc",
"typeof",
"unshift",
"vuepress"
]
}
8 changes: 6 additions & 2 deletions docs/.vuepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,13 @@ const Navbar = [
{
text: "通用",
children: [
// {
// text: "计算机网络",
// link: "/general/network"
// },
{
text: "计算机网络",
link: "/general/network"
text: "数据结构与算法",
link: "/general/algorithm"
}
]
}
Expand Down
88 changes: 88 additions & 0 deletions docs/general/algorithm/01.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
---
title: 数据结构与算法
prev:
text: 目录
link: ./README.md
---

## 基础

### JS 语法

#### 数组

创建一个长度确定、同时每一个元素的值也都确定的数组

```js
const arr = new Array(10).fill(0);
```

增加/删除元素的三种方法

- push / pop
- unshift / shift
- splice

:::warning 注意

1. 从性能上看,for 循环遍历起来是最快的
2. 当你给 fill 传递一个入参时,如果这个入参的类型是引用类型,那么 fill 在填充坑位时填充的其实就是入参的引用
3. 在算法题目中,见到“矩阵”时,要立刻反射出它说的是二维数组

:::

### 常见数据结构

- 数组
- 访问效率较高,而插入/删除效率较低
-
- 只用 pop 和 push 完成增删的“数组”
- 队列
- 只用 push 和 shift 完成增删的“数组”
- 链表
- ![handler](./imgs/linked-list.png)
- 插入/删除效率较高,而访问效率较低
- 树(二叉树)

### 二叉树的递归遍历

```js
const preorder = (root) => {
if (!root) return;
console.log("当前遍历的节点是:", root.val);
preorder(root.left);
preorder(root.right);
};

const inorder = (root) => {
if (!root) return;
inorder(root.left);
console.log("当前遍历的节点是:", root.val);
inorder(root.right);
};

const postorder = (root) => {
if (!root) return;
postorder(root.left);
postorder(root.right);
console.log("当前遍历的节点是:", root.val);
};
```

### 评价算法能力

时间复杂度(常见的如下):反映算法对应的执行总次数的一个变化趋势

- 常数时间 `O(1)`
- 对数时间 `O(logn)`
- 线性时间 `O(n)`
- 线性对数时间 `O(nlogn)`
- 二次时间 `O(n^2)`
- 三次时间 `O(n^3)`
- 指数时间 `O(2^n)`

空间复杂度(常见的如下):对一个算法在运行过程中临时占用存储空间大小的量度,反映内存增长的趋势

- O(1)
- O(n)
- O(n^2)
8 changes: 8 additions & 0 deletions docs/general/algorithm/02.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
title: 数据结构与算法
prev:
text: 基础
link: ./01.md
---

##
Binary file added docs/general/algorithm/imgs/linked-list.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions docs/general/algorithm/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
sidebar: false
---

## 数据结构与算法

[第 1 章 基础](./01.md)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
---
lang: zh-CN
title: 计算机网络
---
---
lang: zh-CN
title: 计算机网络
---
Loading

0 comments on commit 82c33b4

Please sign in to comment.