Organizing all the code blocks.

This commit is contained in:
Yudong Jin
2022-12-03 01:31:29 +08:00
parent fec56afd5f
commit 9bd5980a81
21 changed files with 2520 additions and 310 deletions

View File

@@ -102,6 +102,42 @@ $$
}
```
=== "Python"
```python title="binary_search.py"
```
=== "Go"
```go title="binary_search.go"
```
=== "JavaScript"
```js title="binary_search.js"
```
=== "TypeScript"
```typescript title="binary_search.ts"
```
=== "C"
```c title="binary_search.c"
```
=== "C#"
```csharp title="binary_search.cs"
```
### “左闭右开” 实现
当然,我们也可以使用 “左闭右开” 的表示方法,写出相同功能的二分查找代码。
@@ -150,6 +186,42 @@ $$
}
```
=== "Python"
```python title="binary_search.py"
```
=== "Go"
```go title="binary_search.go"
```
=== "JavaScript"
```js title="binary_search.js"
```
=== "TypeScript"
```typescript title="binary_search.ts"
```
=== "C"
```c title="binary_search.c"
```
=== "C#"
```csharp title="binary_search.cs"
```
### 两种表示对比
对比下来,两种表示的代码写法有以下不同点:
@@ -171,15 +243,16 @@ $$
=== "Java"
```java
```java title=""
// (i + j) 有可能超出 int 的取值范围
int m = (i + j) / 2;
// 更换为此写法则不会越界
int m = i + (j - i) / 2;
```
=== "C++"
```cpp
```cpp title=""
// (i + j) 有可能超出 int 的取值范围
int m = (i + j) / 2;
// 更换为此写法则不会越界
@@ -188,11 +261,41 @@ $$
=== "Python"
```py
```py title=""
# Python 中的数字理论上可以无限大(取决于内存)
# 因此无需考虑大数越界问题
```
=== "Go"
```go title=""
```
=== "JavaScript"
```js title=""
```
=== "TypeScript"
```typescript title=""
```
=== "C"
```c title=""
```
=== "C#"
```csharp title=""
```
## 复杂度分析
**时间复杂度 $O(\log n)$ ** 其中 $n$ 为数组或链表长度;每轮排除一半的区间,因此循环轮数为 $\log_2 n$ ,使用 $O(\log n)$ 时间。

View File

@@ -40,6 +40,42 @@ comments: true
}
```
=== "Python"
```python title="hashing_search.py"
```
=== "Go"
```go title="hashing_search.go"
```
=== "JavaScript"
```js title="hashing_search.js"
```
=== "TypeScript"
```typescript title="hashing_search.ts"
```
=== "C"
```c title="hashing_search.c"
```
=== "C#"
```csharp title="hashing_search.cs"
```
再比如,如果我们想要给定一个目标结点值 `target` ,获取对应的链表结点对象,那么也可以使用哈希查找实现。
![hash_search_listnode](hashing_search.assets/hash_search_listnode.png)
@@ -68,6 +104,42 @@ comments: true
}
```
=== "Python"
```python title="hashing_search.py"
```
=== "Go"
```go title="hashing_search.go"
```
=== "JavaScript"
```js title="hashing_search.js"
```
=== "TypeScript"
```typescript title="hashing_search.ts"
```
=== "C"
```c title="hashing_search.c"
```
=== "C#"
```csharp title="hashing_search.cs"
```
## 复杂度分析
**时间复杂度:** $O(1)$ ,哈希表的查找操作使用 $O(1)$ 时间。

View File

@@ -44,6 +44,42 @@ comments: true
}
```
=== "Python"
```python title="linear_search.py"
```
=== "Go"
```go title="linear_search.go"
```
=== "JavaScript"
```js title="linear_search.js"
```
=== "TypeScript"
```typescript title="linear_search.ts"
```
=== "C"
```c title="linear_search.c"
```
=== "C#"
```csharp title="linear_search.cs"
```
再比如,我们想要在给定一个目标结点值 `target` ,返回此结点对象,也可以在链表中进行线性查找。
=== "Java"
@@ -80,6 +116,42 @@ comments: true
}
```
=== "Python"
```python title="linear_search.py"
```
=== "Go"
```go title="linear_search.go"
```
=== "JavaScript"
```js title="linear_search.js"
```
=== "TypeScript"
```typescript title="linear_search.ts"
```
=== "C"
```c title="linear_search.c"
```
=== "C#"
```csharp title="linear_search.cs"
```
## 复杂度分析
**时间复杂度 $O(n)$ ** 其中 $n$ 为数组或链表长度。

View File

@@ -1,3 +1,7 @@
---
comments: true
---
# 小结
- 线性查找是一种最基础的查找方法,通过遍历数据结构 + 判断条件实现查找。