mirror of
https://github.com/krahets/hello-algo.git
synced 2025-12-19 07:17:54 +08:00
1. Remove Pair class from hash coliision code.
2. Fix the comment in my_list code. 3. Add a Q&A to the summary of sorting.
This commit is contained in:
@@ -34,88 +34,66 @@
|
||||
=== "Java"
|
||||
|
||||
```java title="hash_map_chaining.java"
|
||||
[class]{Pair}-[func]{}
|
||||
|
||||
[class]{HashMapChaining}-[func]{}
|
||||
```
|
||||
|
||||
=== "C++"
|
||||
|
||||
```cpp title="hash_map_chaining.cpp"
|
||||
[class]{Pair}-[func]{}
|
||||
|
||||
[class]{HashMapChaining}-[func]{}
|
||||
```
|
||||
|
||||
=== "Python"
|
||||
|
||||
```python title="hash_map_chaining.py"
|
||||
[class]{Pair}-[func]{}
|
||||
|
||||
[class]{HashMapChaining}-[func]{}
|
||||
```
|
||||
|
||||
=== "Go"
|
||||
|
||||
```go title="hash_map_chaining.go"
|
||||
[class]{pair}-[func]{}
|
||||
|
||||
[class]{hashMapChaining}-[func]{}
|
||||
```
|
||||
|
||||
=== "JavaScript"
|
||||
|
||||
```javascript title="hash_map_chaining.js"
|
||||
[class]{Pair}-[func]{}
|
||||
|
||||
[class]{HashMapChaining}-[func]{}
|
||||
```
|
||||
|
||||
=== "TypeScript"
|
||||
|
||||
```typescript title="hash_map_chaining.ts"
|
||||
[class]{Pair}-[func]{}
|
||||
|
||||
[class]{HashMapChaining}-[func]{}
|
||||
```
|
||||
|
||||
=== "C"
|
||||
|
||||
```c title="hash_map_chaining.c"
|
||||
[class]{pair}-[func]{}
|
||||
|
||||
[class]{hashMapChaining}-[func]{}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
|
||||
```csharp title="hash_map_chaining.cs"
|
||||
[class]{Pair}-[func]{}
|
||||
|
||||
[class]{HashMapChaining}-[func]{}
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
|
||||
```swift title="hash_map_chaining.swift"
|
||||
[class]{Pair}-[func]{}
|
||||
|
||||
[class]{HashMapChaining}-[func]{}
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
```zig title="hash_map_chaining.zig"
|
||||
[class]{Pair}-[func]{}
|
||||
|
||||
[class]{HashMapChaining}-[func]{}
|
||||
```
|
||||
|
||||
=== "Dart"
|
||||
|
||||
```dart title="hash_map_chaining.dart"
|
||||
[class]{Pair}-[func]{}
|
||||
|
||||
[class]{HashMapChaining}-[func]{}
|
||||
```
|
||||
|
||||
@@ -150,88 +128,66 @@
|
||||
=== "Java"
|
||||
|
||||
```java title="hash_map_open_addressing.java"
|
||||
[class]{Pair}-[func]{}
|
||||
|
||||
[class]{HashMapOpenAddressing}-[func]{}
|
||||
```
|
||||
|
||||
=== "C++"
|
||||
|
||||
```cpp title="hash_map_open_addressing.cpp"
|
||||
[class]{Pair}-[func]{}
|
||||
|
||||
[class]{HashMapOpenAddressing}-[func]{}
|
||||
```
|
||||
|
||||
=== "Python"
|
||||
|
||||
```python title="hash_map_open_addressing.py"
|
||||
[class]{Pair}-[func]{}
|
||||
|
||||
[class]{HashMapOpenAddressing}-[func]{}
|
||||
```
|
||||
|
||||
=== "Go"
|
||||
|
||||
```go title="hash_map_open_addressing.go"
|
||||
[class]{pair}-[func]{}
|
||||
|
||||
[class]{hashMapOpenAddressing}-[func]{}
|
||||
```
|
||||
|
||||
=== "JavaScript"
|
||||
|
||||
```javascript title="hash_map_open_addressing.js"
|
||||
[class]{Pair}-[func]{}
|
||||
|
||||
[class]{HashMapOpenAddressing}-[func]{}
|
||||
```
|
||||
|
||||
=== "TypeScript"
|
||||
|
||||
```typescript title="hash_map_open_addressing.ts"
|
||||
[class]{Pair}-[func]{}
|
||||
|
||||
[class]{HashMapOpenAddressing}-[func]{}
|
||||
```
|
||||
|
||||
=== "C"
|
||||
|
||||
```c title="hash_map_open_addressing.c"
|
||||
[class]{pair}-[func]{}
|
||||
|
||||
[class]{hashMapOpenAddressing}-[func]{}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
|
||||
```csharp title="hash_map_open_addressing.cs"
|
||||
[class]{Pair}-[func]{}
|
||||
|
||||
[class]{HashMapOpenAddressing}-[func]{}
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
|
||||
```swift title="hash_map_open_addressing.swift"
|
||||
[class]{Pair}-[func]{}
|
||||
|
||||
[class]{HashMapOpenAddressing}-[func]{}
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
```zig title="hash_map_open_addressing.zig"
|
||||
[class]{Pair}-[func]{}
|
||||
|
||||
[class]{HashMapOpenAddressing}-[func]{}
|
||||
```
|
||||
|
||||
=== "Dart"
|
||||
|
||||
```dart title="hash_map_open_addressing.dart"
|
||||
[class]{Pair}-[func]{}
|
||||
|
||||
[class]{HashMapOpenAddressing}-[func]{}
|
||||
```
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
|
||||
给定一个长度为 $n$ 的有序数组 `nums` ,数组可能包含重复元素。请查找并返回元素 `target` 在数组中首次出现的索引。若数组中不包含该元素,则返回 $-1$ 。
|
||||
|
||||
## 简单方法
|
||||
## 线性方法
|
||||
|
||||
为了查找数组中最左边的 `target` ,我们可以分为两步:
|
||||
|
||||
@@ -15,11 +15,11 @@
|
||||
|
||||

|
||||
|
||||
这个方法虽然有效,但由于包含线性查找,**其时间复杂度可能会劣化至 $O(n)$** 。
|
||||
这个方法虽然有效,但由于包含线性查找,时间复杂度为 $O(n)$ ,当存在很多重复的 `target` 时效率较低。
|
||||
|
||||
## 二分方法
|
||||
|
||||
实际上,我们可以仅通过二分查找解决以上问题。整体算法流程不变,先计算中点索引 $m$ ,再判断 `target` 和 `nums[m]` 大小关系:
|
||||
考虑仅使用二分查找解决该问题。整体算法流程不变,先计算中点索引 $m$ ,再判断 `target` 和 `nums[m]` 大小关系:
|
||||
|
||||
- 当 `nums[m] < target` 或 `nums[m] > target` 时,说明还没有找到 `target` ,因此采取与上节代码相同的缩小区间操作,**从而使指针 $i$ 和 $j$ 向 `target` 靠近**。
|
||||
- 当 `nums[m] == target` 时,说明“小于 `target` 的元素”在区间 $[i, m - 1]$ 中,因此采用 $j = m - 1$ 来缩小区间,**从而使指针 $j$ 向小于 `target` 的元素靠近**。
|
||||
|
||||
@@ -38,6 +38,10 @@
|
||||
|
||||
回顾原始的快速排序,我们有可能会连续地递归长度较大的数组,最差情况下为 $n, n - 1, n - 2, ..., 2, 1$ ,从而递归深度为 $n$ 。尾递归优化可以避免这种情况的出现。
|
||||
|
||||
!!! question "当数组中所有元素都相等时,快速排序的时间复杂度是 $O(n^2)$ 吗?该如何处理这种退化情况?"
|
||||
|
||||
是的。这种情况可以考虑通过哨兵划分将数组划分为三个部分:小于、等于、大于基准数。仅向下递归小于和大于的两部分。在该方法下,输入元素全部相等的数组,仅一轮哨兵划分即可完成排序。
|
||||
|
||||
!!! question "桶排序的最差时间复杂度为什么是 $O(n^2)$ ?"
|
||||
|
||||
最差情况下,所有元素被分至同一个桶中。如果我们采用一个 $O(n^2)$ 算法来排序这些元素,则时间复杂度为 $O(n^2)$ 。
|
||||
|
||||
Reference in New Issue
Block a user