Skip to content

Commit 5b6d020

Browse files
committed
feat: update solutions to lc problems
1 parent dcbd15e commit 5b6d020

File tree

63 files changed

+190
-104
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+190
-104
lines changed

basic/sorting/BubbleSort/README.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -234,14 +234,14 @@ print(arr)
234234

235235
## 算法分析
236236

237-
空间复杂度 O(1)、时间复杂度 O(n²)
237+
空间复杂度 $O(1)$、时间复杂度 $O(n^2)$
238238

239239
分情况讨论:
240240

241-
1. 给定的数组按照顺序已经排好:只需要进行 `n-1` 次比较,两两交换次数为 0,时间复杂度为 O(n),这是最好的情况。
242-
2. 给定的数组按照逆序排列:需要进行 `n*(n-1)/2` 次比较,时间复杂度为 O(n²),这是最坏的情况。
243-
3. 给定的数组杂乱无章。在这种情况下,平均时间复杂度 O(n²)
241+
1. 给定的数组按照顺序已经排好:只需要进行 $n-1$ 次比较,两两交换次数为 0,时间复杂度为 $O(n)$,这是最好的情况。
242+
2. 给定的数组按照逆序排列:需要进行 $\frac{n\times (n-1)}{2}$ 次比较,时间复杂度为 $O(n^2)$,这是最坏的情况。
243+
3. 给定的数组杂乱无章。在这种情况下,平均时间复杂度 $O(n^2)$
244244

245-
因此,时间复杂度是 O(n²),这是一种稳定的排序算法。
245+
因此,时间复杂度是 $O(n^2)$,这是一种稳定的排序算法。
246246

247247
> 稳定是指,两个相等的数,在排序过后,相对位置保持不变。

basic/sorting/CountingSort/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -69,5 +69,5 @@ func CountingSort(nums []int, min, max int) {
6969

7070
## 算法分析
7171

72-
- 时间复杂度 `O(n+k)`, 其中 `n` 为排序数组长度,`k` 为排序数组中数值的取值范围,当 `k < n` 时,时间复杂度为 `O(n)`
73-
- 空间复杂度 `O(n+k)`, 其中 `n` 为排序数组长度,`k` 为排序数组中数值的取值范围,当 `k < n` 时,空间复杂度为 `O(n)`
72+
- 时间复杂度 $O(n+k)$, 其中 $n$ 为排序数组长度,而 $k$ 为排序数组中数值的取值范围,当 $k\lt n$ 时,时间复杂度为 $O(n)$。
73+
- 空间复杂度 $O(n+k)$, 其中 $n$ 为排序数组长度,而 $k$ 为排序数组中数值的取值范围,当 $k\lt n$ 时,空间复杂度为 $O(n)$。

basic/sorting/InsertionSort/README.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -214,12 +214,12 @@ print(insertion_sort(array))
214214

215215
## 算法分析
216216

217-
空间复杂度 O(1),时间复杂度 O(n²)
217+
空间复杂度 $O(1)$,时间复杂度 $O(n^2)$
218218

219219
分情况讨论:
220220

221-
1. 给定的数组按照顺序排好序:只需要进行 n-1 次比较,两两交换次数为 0,时间复杂度为 O(n),这是最好的情况。
222-
1. 给定的数组按照逆序排列:需要进行 `n*(n-1)/2` 次比较,时间复杂度为 O(n²),这是最坏的情况。
223-
1. 给定的数组杂乱无章:在这种情况下,平均时间复杂度是 O(n²)
221+
1. 给定的数组按照顺序排好序:只需要进行 $n-1$ 次比较,两两交换次数为 0,时间复杂度为 $O(n)$,这是最好的情况。
222+
1. 给定的数组按照逆序排列:需要进行 $\frac{n\times (n-1)}{2}$ 次比较,时间复杂度为 $O(n^2)$,这是最坏的情况。
223+
1. 给定的数组杂乱无章:在这种情况下,平均时间复杂度是 $O(n^2)$
224224

225-
因此,时间复杂度是 O(n²),这也是一种稳定的排序算法。
225+
因此,时间复杂度是 $O(n^2)$,这也是一种稳定的排序算法。

basic/sorting/SelectionSort/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ print(arr)
217217

218218
## 算法分析
219219

220-
空间复杂度 O(1),时间复杂度 O(n²)
220+
空间复杂度 $O(1)$,时间复杂度 $O(n^2)$
221221

222222
那选择排序是稳定的排序算法吗?
223223

basic/sorting/ShellSort/README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,9 @@ fn main() {
127127

128128
时间复杂度:
129129

130-
希尔排序的时间性能取决于所取“增量”序列的函数,这涉及到一些数学上尚未解决的难题。但是有人通过大量的实验,给出了较好的结果:当 n 较大时,比较和移动的次数约在 `n^1.25``(1.6n)^1.25` 之间。所以可以这样简单记忆:
130+
希尔排序的时间性能取决于所取“增量”序列的函数,这涉及到一些数学上尚未解决的难题。但是有人通过大量的实验,给出了较好的结果:当 $n$ 较大时,比较和移动的次数约在 $n^{1.25}$${1.6n}^{1.25}$ 之间。所以可以这样简单记忆:
131131

132-
-n 较小时,希尔排序和插入排序相差不大,都为 左右。
133-
-n 很大时,时间增长幅度逐渐放缓,平均复杂度是 nlogn
132+
-$n$ 较小时,希尔排序和插入排序相差不大,都为 $n^2$ 左右。
133+
-$n$ 很大时,时间增长幅度逐渐放缓,平均复杂度是 $O(n\log n)$
134134

135-
空间复杂度:O(1)。
135+
空间复杂度:$O(1)$

lcci/01.02.Check Permutation/README.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,13 @@
3030

3131
<!-- 这里可写通用的实现逻辑 -->
3232

33-
**方法一:**
33+
**方法一:哈希表**
3434

35-
使用哈希表作为字符计数器`O(n)` 时间内解决
35+
使用哈希表作为字符计数器。
3636

37-
**方法二:**
37+
时间复杂度 $O(n)$。
38+
39+
**方法二:排序**
3840

3941
按照字符编码重新排序字符串,再检查两者一致性。
4042

lcci/08.06.Hanota/README.md

+15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# [面试题 08.06. 汉诺塔问题](https://leetcode.cn/problems/hanota-lcci)
2+
23
[English Version](/lcci/08.06.Hanota/README_EN.md)
4+
35
## 题目描述
6+
47
<!-- 这里写题目描述 -->
58
<p>在经典汉诺塔问题中,有 3 根柱子及 N 个不同大小的穿孔圆盘,盘子可以滑入任意一根柱子。一开始,所有盘子自上而下按升序依次套在第一根柱子上(即每一个盘子只能放在更大的盘子上面)。移动圆盘时受到以下限制:<br>
69
(1) 每次只能移动一个盘子;<br>
@@ -20,21 +23,33 @@
2023
<ol>
2124
<li>A中盘子的数目不大于14个。</li>
2225
</ol>
26+
2327
## 解法
28+
2429
<!-- 这里可写通用的实现逻辑 -->
30+
2531
<!-- tabs:start -->
32+
2633
### **Python3**
34+
2735
<!-- 这里可写当前语言的特殊实现逻辑 -->
36+
2837
```python
2938

3039
```
40+
3141
### **Java**
42+
3243
<!-- 这里可写当前语言的特殊实现逻辑 -->
44+
3345
```java
3446

3547
```
48+
3649
### **...**
50+
3751
```
3852
3953
```
54+
4055
<!-- tabs:end -->

lcci/08.06.Hanota/README_EN.md

+12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# [08.06. Hanota](https://leetcode.cn/problems/hanota-lcci)
2+
23
[中文文档](/lcci/08.06.Hanota/README.md)
4+
35
## Description
6+
47
<p>In the classic problem of the Towers of Hanoi, you have 3 towers and N disks of different sizes which can slide onto any tower. The puzzle starts with disks sorted in ascending order of size from top to bottom (i.e., each disk sits on top of an even larger one). You have the following constraints:</p>
58
<p>(1) Only one disk can be moved at a time.<br />
69
(2) A disk is slid off the top of one tower onto another tower.<br />
@@ -26,18 +29,27 @@
2629
<ol>
2730
<li><code>A.length &lt;= 14</code></li>
2831
</ol>
32+
2933
## Solutions
34+
3035
<!-- tabs:start -->
36+
3137
### **Python3**
38+
3239
```python
3340

3441
```
42+
3543
### **Java**
44+
3645
```java
3746

3847
```
48+
3949
### **...**
50+
4051
```
4152
4253
```
54+
4355
<!-- tabs:end -->

lcci/08.08.Permutation II/README.md

+17
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# [面试题 08.08. 有重复字符串的排列组合](https://leetcode.cn/problems/permutation-ii-lcci)
2+
23
[English Version](/lcci/08.08.Permutation%20II/README_EN.md)
4+
35
## 题目描述
6+
47
<!-- 这里写题目描述 -->
58
<p>有重复字符串的排列组合。编写一种方法,计算某字符串的所有排列组合。</p>
69
<p><strong>示例1:</strong></p>
@@ -16,20 +19,31 @@
1619
<li>字符都是英文字母。</li>
1720
<li>字符串长度在[1, 9]之间。</li>
1821
</ol>
22+
1923
## 解法
24+
2025
<!-- 这里可写通用的实现逻辑 -->
26+
2127
<!-- tabs:start -->
28+
2229
### **Python3**
30+
2331
<!-- 这里可写当前语言的特殊实现逻辑 -->
32+
2433
```python
2534

2635
```
36+
2737
### **Java**
38+
2839
<!-- 这里可写当前语言的特殊实现逻辑 -->
40+
2941
```java
3042

3143
```
44+
3245
### **JavaScript**
46+
3347
```js
3448
/**
3549
* @param {string} S
@@ -66,8 +80,11 @@ function dfs(arr, depth, prev, record, res) {
6680
}
6781
}
6882
```
83+
6984
### **...**
85+
7086
```
7187
7288
```
89+
7390
<!-- tabs:end -->

lcci/08.08.Permutation II/README_EN.md

+14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# [08.08. Permutation II](https://leetcode.cn/problems/permutation-ii-lcci)
2+
23
[中文文档](/lcci/08.08.Permutation%20II/README.md)
4+
35
## Description
6+
47
<p>Write a method to compute all permutations of a string whose charac&shy; ters are not necessarily unique. The list of permutations should not have duplicates.</p>
58
<p><strong>Example1:</strong></p>
69
<pre>
@@ -23,17 +26,25 @@
2326
<li>All characters are English letters.</li>
2427
<li><code>1 &lt;= S.length &lt;= 9</code></li>
2528
</ol>
29+
2630
## Solutions
31+
2732
<!-- tabs:start -->
33+
2834
### **Python3**
35+
2936
```python
3037

3138
```
39+
3240
### **Java**
41+
3342
```java
3443

3544
```
45+
3646
### **JavaScript**
47+
3748
```js
3849
/**
3950
* @param {string} S
@@ -70,8 +81,11 @@ function dfs(arr, depth, prev, record, res) {
7081
}
7182
}
7283
```
84+
7385
### **...**
86+
7487
```
7588
7689
```
90+
7791
<!-- tabs:end -->

lcci/08.11.Coin/README.md

+17-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# [面试题 08.11. 硬币](https://leetcode.cn/problems/coin-lcci)
2+
23
[English Version](/lcci/08.11.Coin/README_EN.md)
4+
35
## 题目描述
6+
47
<!-- 这里写题目描述 -->
58
<p>硬币。给定数量不限的硬币,币值为25分、10分、5分和1分,编写代码计算n分有几种表示法。(结果可能会很大,你需要将结果模上1000000007)</p>
69
<p> <strong>示例1:</strong></p>
@@ -27,21 +30,31 @@
2730
<ul>
2831
<li>0 &lt;= n (总金额) &lt;= 1000000</li>
2932
</ul>
33+
3034
## 解法
31-
完全背包问题
35+
3236
<!-- 这里可写通用的实现逻辑 -->
37+
38+
**方法一:动态规划**
39+
3340
<!-- tabs:start -->
3441
### **Python3**
42+
3543
<!-- 这里可写当前语言的特殊实现逻辑 -->
44+
3645
```python
3746

3847
```
3948
### **Java**
49+
4050
<!-- 这里可写当前语言的特殊实现逻辑 -->
51+
4152
```java
4253

4354
```
55+
4456
### **TypeScript**
57+
4558
```ts
4659
function waysToChange(n: number): number {
4760
const MOD = 10 ** 9 + 7;
@@ -56,8 +69,11 @@ function waysToChange(n: number): number {
5669
return dp.pop() % MOD;
5770
}
5871
```
72+
5973
### **...**
74+
6075
```
6176
6277
```
78+
6379
<!-- tabs:end -->

lcci/08.11.Coin/README_EN.md

+14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# [08.11. Coin](https://leetcode.cn/problems/coin-lcci)
2+
23
[中文文档](/lcci/08.11.Coin/README.md)
4+
35
## Description
6+
47
<p>Given an infinite number of quarters (25 cents), dimes (10 cents), nickels (5 cents), and pennies (1 cent), write code to calculate the number of ways of representing n cents.&nbsp;(The result may be large, so you should return it modulo 1000000007)</p>
58
<p><strong>Example1:</strong></p>
69
<pre>
@@ -39,17 +42,25 @@
3942
<ul>
4043
<li>0 &lt;= n&nbsp;&lt;= 1000000</li>
4144
</ul>
45+
4246
## Solutions
47+
4348
<!-- tabs:start -->
49+
4450
### **Python3**
51+
4552
```python
4653

4754
```
55+
4856
### **Java**
57+
4958
```java
5059

5160
```
61+
5262
### **TypeScript**
63+
5364
```ts
5465
function waysToChange(n: number): number {
5566
const MOD = 10 ** 9 + 7;
@@ -64,8 +75,11 @@ function waysToChange(n: number): number {
6475
return dp.pop() % MOD;
6576
}
6677
```
78+
6779
### **...**
80+
6881
```
6982
7083
```
84+
7185
<!-- tabs:end -->

lcci/10.10.Rank from Stream/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@
3333

3434
<!-- 这里可写通用的实现逻辑 -->
3535

36-
树状数组
36+
**方法一:树状数组**
3737

3838
树状数组,也称作“二叉索引树”(Binary Indexed Tree)或 Fenwick 树。 它可以高效地实现如下两个操作:
3939

4040
1. **单点更新** `update(x, delta)`: 把序列 x 位置的数加上一个值 delta;
4141
1. **前缀和查询** `query(x)`:查询序列 `[1,...x]` 区间的区间和,即位置 x 的前缀和。
4242

43-
这两个操作的时间复杂度均为 `O(log n)`
43+
这两个操作的时间复杂度均为 $O(\log n)$
4444

4545
树状数组最基本的功能就是求比某点 x 小的点的个数(这里的比较是抽象的概念,可以是数的大小、坐标的大小、质量的大小等等)。
4646

lcci/17.12.BiNode/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333

3434
中序遍历过程中改变指针指向。
3535

36-
时间复杂度 O(n)。
36+
时间复杂度 $O(n)$
3737

3838
[897. 递增顺序查找树](/solution/0800-0899/0897.Increasing%20Order%20Search%20Tree/README.md)
3939

0 commit comments

Comments
 (0)