Add Swift language blocks to the docs.

This commit is contained in:
Yudong Jin
2023-01-08 19:41:05 +08:00
parent 3ba37dba3a
commit 73e3452838
22 changed files with 414 additions and 70 deletions

View File

@@ -210,6 +210,12 @@ $$
}
```
=== "Swift"
```swift title="binary_search.swift"
```
### “左闭右开”实现
当然,我们也可以使用“左闭右开”的表示方法,写出相同功能的二分查找代码。
@@ -374,6 +380,12 @@ $$
}
```
=== "Swift"
```swift title="binary_search.swift"
```
### 两种表示对比
对比下来,两种表示的代码写法有以下不同点:
@@ -460,6 +472,12 @@ $$
int m = i + (j - i) / 2;
```
=== "Swift"
```swift title=""
```
## 复杂度分析
**时间复杂度 $O(\log n)$ ** 其中 $n$ 为数组或链表长度;每轮排除一半的区间,因此循环轮数为 $\log_2 n$ ,使用 $O(\log n)$ 时间。

View File

@@ -95,6 +95,12 @@ comments: true
}
```
=== "Swift"
```swift title="hashing_search.swift"
```
再比如,如果我们想要给定一个目标结点值 `target` ,获取对应的链表结点对象,那么也可以使用哈希查找实现。
![hash_search_listnode](hashing_search.assets/hash_search_listnode.png)
@@ -179,6 +185,12 @@ comments: true
}
```
=== "Swift"
```swift title="hashing_search.swift"
```
## 复杂度分析
**时间复杂度:** $O(1)$ ,哈希表的查找操作使用 $O(1)$ 时间。

View File

@@ -122,6 +122,12 @@ comments: true
```
=== "Swift"
```swift title="linear_search.swift"
```
再比如,我们想要在给定一个目标结点值 `target` ,返回此结点对象,也可以在链表中进行线性查找。
=== "Java"
@@ -238,6 +244,12 @@ comments: true
}
```
=== "Swift"
```swift title="linear_search.swift"
```
## 复杂度分析
**时间复杂度 $O(n)$ ** 其中 $n$ 为数组或链表长度。