From 607e4ebd49c67b6ddd4fcdd91b248e74ed0ed75e Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Wed, 19 Jul 2023 14:18:19 +0800 Subject: [PATCH 01/18] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E5=93=88=E5=B8=8C?= =?UTF-8?q?=E8=A1=A8=E7=90=86=E8=AE=BA=E5=9F=BA=E7=A1=80=20=E6=8E=92?= =?UTF-8?q?=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/哈希表理论基础.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/problems/哈希表理论基础.md b/problems/哈希表理论基础.md index 0ab17ec0..3055875a 100644 --- a/problems/哈希表理论基础.md +++ b/problems/哈希表理论基础.md @@ -8,6 +8,8 @@ +# 哈希表理论基础 + ## 哈希表 首先什么是 哈希表,哈希表(英文名字为Hash table,国内也有一些算法书籍翻译为散列表,大家看到这两个名称知道都是指hash table就可以了)。 From c628aa65d438aaf4933f2b029ae71c93e3cfce63 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Wed, 19 Jul 2023 14:25:34 +0800 Subject: [PATCH 02/18] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200242.=E6=9C=89?= =?UTF-8?q?=E6=95=88=E7=9A=84=E5=AD=97=E6=AF=8D=E5=BC=82=E4=BD=8D=E8=AF=8D?= =?UTF-8?q?=20=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0242.有效的字母异位词.md | 38 ++++++++++++----------- 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/problems/0242.有效的字母异位词.md b/problems/0242.有效的字母异位词.md index 4ea43947..f47d8b05 100644 --- a/problems/0242.有效的字母异位词.md +++ b/problems/0242.有效的字母异位词.md @@ -7,7 +7,7 @@ > 数组就是简单的哈希表,但是数组的大小可不是无限开辟的 -## 242.有效的字母异位词 +# 242.有效的字母异位词 [力扣题目链接](https://leetcode.cn/problems/valid-anagram/) @@ -21,13 +21,14 @@ 输入: s = "rat", t = "car" 输出: false - **说明:** 你可以假设字符串只包含小写字母。 -## 思路 +## 算法公开课 -本题B站视频讲解版:[学透哈希表,数组使用有技巧!Leetcode:242.有效的字母异位词](https://www.bilibili.com/video/BV1YG411p7BA) +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[学透哈希表,数组使用有技巧!Leetcode:242.有效的字母异位词](https://www.bilibili.com/video/BV1YG411p7BA),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 + +## 思路 先看暴力的解法,两层for循环,同时还要记录字符是否重复出现,很明显时间复杂度是 O(n^2)。 @@ -88,12 +89,10 @@ public: * 时间复杂度: O(n) * 空间复杂度: O(1) - - ## 其他语言版本 +### Java: -Java: ```java /** * 242. 有效的字母异位词 字典解法 @@ -121,7 +120,7 @@ class Solution { } ``` -Python: +### Python: ```python class Solution: @@ -165,7 +164,7 @@ class Solution(object): return a_count == b_count ``` -Go: +### Go: ```go func isAnagram(s string, t string) bool { @@ -182,7 +181,7 @@ func isAnagram(s string, t string) bool { } ``` -javaScript: +### JavaScript: ```js /** @@ -218,7 +217,7 @@ var isAnagram = function(s, t) { }; ``` -TypeScript: +### TypeScript: ```typescript function isAnagram(s: string, t: string): boolean { @@ -233,7 +232,7 @@ function isAnagram(s: string, t: string): boolean { }; ``` -Swift: +### Swift: ```Swift func isAnagram(_ s: String, _ t: String) -> Bool { @@ -257,7 +256,8 @@ func isAnagram(_ s: String, _ t: String) -> Bool { } ``` -PHP: +### PHP: + ```php class Solution { /** @@ -292,7 +292,8 @@ class Solution { } ``` -Rust: +### Rust: + ```rust impl Solution { pub fn is_anagram(s: String, t: String) -> bool { @@ -312,8 +313,8 @@ impl Solution { } ``` +### Scala: -Scala: ```scala object Solution { def isAnagram(s: String, t: String): Boolean = { @@ -337,8 +338,8 @@ object Solution { } ``` +### C#: -C#: ```csharp public bool IsAnagram(string s, string t) { int sl=s.Length,tl=t.Length; @@ -360,11 +361,12 @@ C#: ## 相关题目 * [383.赎金信](https://programmercarl.com/0383.%E8%B5%8E%E9%87%91%E4%BF%A1.html) -* 49.字母异位词分组 -* 438.找到字符串中所有字母异位词 +* [49.字母异位词分组](https://leetcode.cn/problems/group-anagrams/) +* [438.找到字符串中所有字母异位词](https://leetcode.cn/problems/find-all-anagrams-in-a-string/)
+
From 19dfa98e4f936f4b5f73886e6c1503c07f913bf8 Mon Sep 17 00:00:00 2001
From: jinbudaily <18336218010@163.com>
Date: Wed, 19 Jul 2023 14:30:12 +0800
Subject: [PATCH 03/18] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200349.=E4=B8=A4?=
=?UTF-8?q?=E4=B8=AA=E6=95=B0=E7=BB=84=E7=9A=84=E4=BA=A4=E9=9B=86=20?=
=?UTF-8?q?=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0349.两个数组的交集.md | 39 +++++++++++++++-----------
1 file changed, 22 insertions(+), 17 deletions(-)
diff --git a/problems/0349.两个数组的交集.md b/problems/0349.两个数组的交集.md
index c2f6ef46..8daf5a35 100644
--- a/problems/0349.两个数组的交集.md
+++ b/problems/0349.两个数组的交集.md
@@ -10,7 +10,7 @@
> 如果哈希值比较少、特别分散、跨度非常大,使用数组就造成空间的极大浪费!
-## 349. 两个数组的交集
+# 349. 两个数组的交集
[力扣题目链接](https://leetcode.cn/problems/intersection-of-two-arrays/)
@@ -22,9 +22,11 @@
输出结果中的每个元素一定是唯一的。
我们可以不考虑输出结果的顺序。
-## 思路
+## 算法公开课
-关于本题,我录制了讲解视频:[学透哈希表,set使用有技巧!Leetcode:349. 两个数组的交集](https://www.bilibili.com/video/BV1ba411S7wu),看视频配合题解,事半功倍。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html)::[学透哈希表,set使用有技巧!Leetcode:349. 两个数组的交集](https://www.bilibili.com/video/BV1ba411S7wu),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
+
+## 思路
这道题目,主要要学会使用一种哈希数据结构:unordered_set,这个数据结构可以解决很多类似的问题。
@@ -118,8 +120,7 @@ public:
## 其他语言版本
-
-Java:
+### Java:
```Java
import java.util.HashSet;
@@ -159,8 +160,9 @@ class Solution {
}
```
-Python3:
+### Python3:
(版本一) 使用字典和集合
+
```python
class Solution:
def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
@@ -206,7 +208,8 @@ class Solution:
```
-Go:
+### Go:
+
```go
func intersection(nums1 []int, nums2 []int) []int {
set:=make(map[int]struct{},0) // 用map模拟set
@@ -227,7 +230,7 @@ func intersection(nums1 []int, nums2 []int) []int {
}
```
-javaScript:
+### JavaScript:
```js
/**
@@ -255,7 +258,7 @@ var intersection = function(nums1, nums2) {
};
```
-TypeScript:
+### TypeScript:
版本一(正常解法):
@@ -280,7 +283,7 @@ function intersection(nums1: number[], nums2: number[]): number[] {
};
```
-Swift:
+### Swift:
```swift
func intersection(_ nums1: [Int], _ nums2: [Int]) -> [Int] {
@@ -298,7 +301,8 @@ func intersection(_ nums1: [Int], _ nums2: [Int]) -> [Int] {
}
```
-PHP:
+### PHP:
+
```php
class Solution {
/**
@@ -327,7 +331,8 @@ class Solution {
}
```
-Rust:
+### Rust:
+
```rust
use std::collections::HashSet;
impl Solution {
@@ -363,7 +368,8 @@ impl Solution {
}
```
-C:
+### C:
+
```C
int* intersection1(int* nums1, int nums1Size, int* nums2, int nums2Size, int* returnSize){
@@ -394,7 +400,7 @@ int* intersection1(int* nums1, int nums1Size, int* nums2, int nums2Size, int* re
}
```
-Scala:
+### Scala:
正常解法:
```scala
@@ -439,8 +445,8 @@ object Solution {
```
+### C#:
-C#:
```csharp
public int[] Intersection(int[] nums1, int[] nums2) {
if(nums1==null||nums1.Length==0||nums2==null||nums1.Length==0)
@@ -461,11 +467,10 @@ C#:
```
## 相关题目
-* 350.两个数组的交集 II
+* [350.两个数组的交集 II](https://leetcode.cn/problems/intersection-of-two-arrays-ii/)
-
From 764b3e9e51608baf17183a5e36cab0888a6e46f9 Mon Sep 17 00:00:00 2001
From: jinbudaily <18336218010@163.com>
Date: Wed, 19 Jul 2023 14:34:27 +0800
Subject: [PATCH 04/18] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200202.=E5=BF=AB?=
=?UTF-8?q?=E4=B9=90=E6=95=B0=20=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F?=
=?UTF-8?q?=E4=BF=AE=E5=A4=8D?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0202.快乐数.md | 42 ++++++++++++++++++++++----------------
1 file changed, 24 insertions(+), 18 deletions(-)
diff --git a/problems/0202.快乐数.md b/problems/0202.快乐数.md
index 7fe8cd8d..4a77e2b6 100644
--- a/problems/0202.快乐数.md
+++ b/problems/0202.快乐数.md
@@ -28,7 +28,7 @@
6^2 + 8^2 = 100
1^2 + 0^2 + 0^2 = 1
-# 思路
+## 思路
这道题目看上去貌似一道数学问题,其实并不是!
@@ -80,10 +80,10 @@ public:
-# 其他语言版本
+## 其他语言版本
+### Java:
-Java:
```java
class Solution {
public boolean isHappy(int n) {
@@ -107,8 +107,9 @@ class Solution {
}
```
-Python:
+### Python:
(版本一)使用集合
+
```python
class Solution:
def isHappy(self, n: int) -> bool:
@@ -131,7 +132,7 @@ class Solution:
n, r = divmod(n, 10)
new_num += r ** 2
return new_num
- ```
+```
(版本二)使用集合
```python
class Solution:
@@ -146,7 +147,7 @@ class Solution:
if new_num==1: return True
else: n = new_num
return False
-```
+ ```
(版本三)使用数组
```python
class Solution:
@@ -161,7 +162,7 @@ class Solution:
if new_num==1: return True
else: n = new_num
return False
-```
+ ```
(版本四)使用快慢指针
```python
class Solution:
@@ -180,7 +181,7 @@ class Solution:
n, r = divmod(n, 10)
new_num += r ** 2
return new_num
-```
+ ```
(版本五)使用集合+精简
```python
class Solution:
@@ -192,7 +193,7 @@ class Solution:
return False
seen.add(n)
return True
-```
+ ```
(版本六)使用数组+精简
```python
class Solution:
@@ -204,8 +205,9 @@ class Solution:
return False
seen.append(n)
return True
-```
-Go:
+ ```
+### Go:
+
```go
func isHappy(n int) bool {
m := make(map[int]bool)
@@ -225,7 +227,7 @@ func getSum(n int) int {
}
```
-javaScript:
+### JavaScript:
```js
var isHappy = function (n) {
@@ -303,7 +305,7 @@ var isHappy = function(n) {
};
```
-TypeScript:
+### TypeScript:
```typescript
function isHappy(n: number): boolean {
@@ -322,7 +324,7 @@ function isHappy(n: number): boolean {
};
```
-Swift:
+### Swift:
```swift
// number 每个位置上的数字的平方和
@@ -355,7 +357,8 @@ func isHappy(_ n: Int) -> Bool {
}
```
-PHP:
+### PHP:
+
```php
class Solution {
/**
@@ -386,7 +389,8 @@ class Solution {
}
```
-Rust:
+### Rust:
+
```Rust
use std::collections::HashSet;
impl Solution {
@@ -416,7 +420,8 @@ impl Solution {
}
```
-C:
+### C:
+
```C
typedef struct HashNodeTag {
int key; /* num */
@@ -473,8 +478,8 @@ object Solution {
}
```
+### C#:
-C#:
```csharp
public class Solution {
private int getSum(int n) {
@@ -500,3 +505,4 @@ public class Solution {
+
From 23950d1c3e8541420eaca3f55f0896a84d3ba007 Mon Sep 17 00:00:00 2001
From: jinbudaily <18336218010@163.com>
Date: Wed, 19 Jul 2023 14:43:14 +0800
Subject: [PATCH 05/18] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20001.=E4=B8=A4?=
=?UTF-8?q?=E6=95=B0=E4=B9=8B=E5=92=8C=20=E6=8E=92=E7=89=88=E6=A0=BC?=
=?UTF-8?q?=E5=BC=8F=E4=BF=AE=E5=A4=8D?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0001.两数之和.md | 41 ++++++++++++++++++++---------------
1 file changed, 23 insertions(+), 18 deletions(-)
diff --git a/problems/0001.两数之和.md b/problems/0001.两数之和.md
index ca62e3ed..e3fb0fb5 100644
--- a/problems/0001.两数之和.md
+++ b/problems/0001.两数之和.md
@@ -5,7 +5,7 @@
参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!
-## 1. 两数之和 +# 1. 两数之和 [力扣题目链接](https://leetcode.cn/problems/two-sum/) @@ -21,11 +21,13 @@ 所以返回 [0, 1] +## 算法公开课 + +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[梦开始的地方,Leetcode:1.两数之和](https://www.bilibili.com/video/BV1aT41177mK),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 + ## 思路 -建议看一下我录的这期视频:[梦开始的地方,Leetcode:1.两数之和](https://www.bilibili.com/video/BV1aT41177mK),结合本题解来学习,事半功倍。 - 很明显暴力的解法是两层for循环查找,时间复杂度是O(n^2)。 建议大家做这道题目之前,先做一下这两道 @@ -128,8 +130,8 @@ public: ## 其他语言版本 +### Java: -Java: ```java public int[] twoSum(int[] nums, int target) { int[] res = new int[2]; @@ -150,8 +152,9 @@ public int[] twoSum(int[] nums, int target) { } ``` -Python: +### Python: (版本一) 使用字典 + ```python class Solution: def twoSum(self, nums: List[int], target: int) -> List[int]: @@ -211,7 +214,7 @@ class Solution: return [i,j] ``` -Go: +### Go: ```go // 暴力解法 @@ -242,7 +245,7 @@ func twoSum(nums []int, target int) []int { } ``` -Rust +### Rust: ```rust use std::collections::HashMap; @@ -263,9 +266,7 @@ impl Solution { } } ``` -Rust - -``` +```rust use std::collections::HashMap; impl Solution { @@ -284,7 +285,7 @@ impl Solution { } ``` -Javascript +### Javascript: ```javascript var twoSum = function (nums, target) { @@ -299,7 +300,7 @@ var twoSum = function (nums, target) { }; ``` -TypeScript: +### TypeScript: ```typescript function twoSum(nums: number[], target: number): number[] { @@ -317,7 +318,7 @@ function twoSum(nums: number[], target: number): number[] { }; ``` -php +### php: ```php function twoSum(array $nums, int $target): array @@ -337,7 +338,8 @@ function twoSum(array $nums, int $target): array } ``` -Swift: +### Swift: + ```swift func twoSum(_ nums: [Int], _ target: Int) -> [Int] { // 值: 下标 @@ -353,8 +355,8 @@ func twoSum(_ nums: [Int], _ target: Int) -> [Int] { } ``` +### Scala: -Scala: ```scala object Solution { // 导入包 @@ -377,7 +379,8 @@ object Solution { } ``` -C#: +### C#: + ```csharp public class Solution { public int[] TwoSum(int[] nums, int target) { @@ -396,7 +399,8 @@ public class Solution { } ``` -Dart: +### Dart: + ```dart List
参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!
+
From 00ef6084c6ea18933e7a63923aad71addee70c6e Mon Sep 17 00:00:00 2001
From: jinbudaily <18336218010@163.com>
Date: Wed, 19 Jul 2023 15:32:11 +0800
Subject: [PATCH 11/18] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200344.=E5=8F=8D?=
=?UTF-8?q?=E8=BD=AC=E5=AD=97=E7=AC=A6=E4=B8=B2=20=E6=8E=92=E7=89=88?=
=?UTF-8?q?=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0344.反转字符串.md | 35 +++++++++++++++++++-------------
1 file changed, 21 insertions(+), 14 deletions(-)
diff --git a/problems/0344.反转字符串.md b/problems/0344.反转字符串.md
index 1c74f9aa..8a4fed45 100644
--- a/problems/0344.反转字符串.md
+++ b/problems/0344.反转字符串.md
@@ -26,10 +26,12 @@
输入:["H","a","n","n","a","h"]
输出:["h","a","n","n","a","H"]
+## 算法公开课
-# 思路
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[字符串基础操作! | LeetCode:344.反转字符串](https://www.bilibili.com/video/BV1fV4y17748),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
-针对本题,我录制了视频讲解:[字符串基础操作! | LeetCode:344.反转字符串](https://www.bilibili.com/video/BV1fV4y17748),结合本题解一起看,事半功倍!
+
+## 思路
先说一说题外话:
@@ -138,8 +140,8 @@ public:
## 其他语言版本
+### Java:
-Java:
```Java
class Solution {
public void reverseString(char[] s) {
@@ -173,8 +175,9 @@ class Solution {
```
-Python:
+### Python:
(版本一) 双指针
+
```python
class Solution:
def reverseString(self, s: List[str]) -> None:
@@ -247,7 +250,8 @@ class Solution:
s[:] = [s[i] for i in range(len(s) - 1, -1, -1)]
```
-Go:
+### Go:
+
```Go
func reverseString(s []byte) {
left := 0
@@ -260,7 +264,7 @@ func reverseString(s []byte) {
}
```
-javaScript:
+### JavaScript:
```js
/**
@@ -278,7 +282,7 @@ var reverse = function(s) {
};
```
-TypeScript:
+### TypeScript:
```typescript
/**
@@ -299,7 +303,7 @@ function reverseString(s: string[]): void {
};
```
-Swift:
+### Swift:
```swift
// 双指针 - 元组
@@ -316,7 +320,8 @@ func reverseString(_ s: inout [Character]) {
```
-Rust:
+### Rust:
+
```Rust
impl Solution {
pub fn reverse_string(s: &mut Vec
-
From 5cb250100b49e1b8109ddf01ba2109b257094b47 Mon Sep 17 00:00:00 2001
From: jinbudaily <18336218010@163.com>
Date: Wed, 19 Jul 2023 15:37:57 +0800
Subject: [PATCH 12/18] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200541.=E5=8F=8D?=
=?UTF-8?q?=E8=BD=AC=E5=AD=97=E7=AC=A6=E4=B8=B2=20=E6=8E=92=E7=89=88?=
=?UTF-8?q?=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0541.反转字符串II.md | 33 +++++++++++++++---------------
1 file changed, 17 insertions(+), 16 deletions(-)
diff --git a/problems/0541.反转字符串II.md b/problems/0541.反转字符串II.md
index 179395b3..80e662f9 100644
--- a/problems/0541.反转字符串II.md
+++ b/problems/0541.反转字符串II.md
@@ -23,9 +23,11 @@
输入: s = "abcdefg", k = 2
输出: "bacdfeg"
-# 思路
+## 算法公开课
-针对本题,我录制了视频讲解:[字符串操作进阶! | LeetCode:541. 反转字符串II](https://www.bilibili.com/video/BV1dT411j7NN),结合本题解一起看,事半功倍!
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[字符串操作进阶! | LeetCode:541. 反转字符串II](https://www.bilibili.com/video/BV1dT411j7NN),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
+
+## 思路
这道题目其实也是模拟,实现题目中规定的反转规则就可以了。
@@ -42,8 +44,6 @@
那么这里具体反转的逻辑我们要不要使用库函数呢,其实用不用都可以,使用reverse来实现反转也没毛病,毕竟不是解题关键部分。
-# C++代码
-
使用C++库函数reverse的版本如下:
```CPP
@@ -129,7 +129,7 @@ public:
## 其他语言版本
-C:
+### C:
```c
char * reverseStr(char * s, int k){
@@ -152,7 +152,7 @@ char * reverseStr(char * s, int k){
}
```
-Java:
+### Java:
```Java
//解法一
@@ -256,7 +256,8 @@ class Solution {
}
}
```
-Python:
+### Python:
+
```python
class Solution:
def reverseStr(self, s: str, k: int) -> str:
@@ -281,7 +282,7 @@ class Solution:
return ''.join(res)
```
-Python3 (v2):
+### Python3 (v2):
```python
class Solution:
@@ -296,7 +297,7 @@ class Solution:
return s
```
-Go:
+### Go:
```go
func reverseStr(s string, k int) string {
@@ -325,7 +326,7 @@ func reverse(b []byte) {
}
```
-javaScript:
+### JavaScript:
```js
@@ -346,7 +347,7 @@ var reverseStr = function(s, k) {
```
-TypeScript:
+### TypeScript:
```typescript
function reverseStr(s: string, k: number): string {
@@ -368,7 +369,7 @@ function reverseStr(s: string, k: number): string {
};
```
-Swift:
+### Swift:
```swift
func reverseStr(_ s: String, _ k: Int) -> String {
@@ -388,7 +389,8 @@ func reverseStr(_ s: String, _ k: Int) -> String {
}
```
-C#:
+### C#:
+
```csharp
public class Solution
{
@@ -403,7 +405,7 @@ public class Solution
}
}
```
-Scala:
+### Scala:
版本一: (正常解法)
```scala
@@ -469,7 +471,7 @@ object Solution {
}
```
-Rust:
+### Rust:
```Rust
impl Solution {
@@ -503,4 +505,3 @@ impl Solution {
-
From 963eb8fc8e58094388bf6c0f27be7563d7a488b9 Mon Sep 17 00:00:00 2001
From: jinbudaily <18336218010@163.com>
Date: Wed, 19 Jul 2023 15:41:59 +0800
Subject: [PATCH 13/18] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E5=89=91=E6=8C=87Of?=
=?UTF-8?q?fer05.=E6=9B=BF=E6=8D=A2=E7=A9=BA=E6=A0=BC=20=E6=8E=92=E7=89=88?=
=?UTF-8?q?=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/剑指Offer05.替换空格.md | 29 ++++++++++++++------------
1 file changed, 16 insertions(+), 13 deletions(-)
diff --git a/problems/剑指Offer05.替换空格.md b/problems/剑指Offer05.替换空格.md
index dbad781e..fed08a53 100644
--- a/problems/剑指Offer05.替换空格.md
+++ b/problems/剑指Offer05.替换空格.md
@@ -15,7 +15,7 @@
输入:s = "We are happy."
输出:"We%20are%20happy."
-# 思路
+## 思路
如果想把这道题目做到极致,就不要只用额外的辅助空间了!
@@ -86,7 +86,7 @@ public:
* [142.环形链表II](https://programmercarl.com/0142.环形链表II.html)
* [344.反转字符串](https://programmercarl.com/0344.反转字符串.html)
-# 拓展
+## 拓展
这里也给大家拓展一下字符串和数组有什么差别,
@@ -121,7 +121,8 @@ for (int i = 0; i < a.size(); i++) {
## 其他语言版本
-C:
+### C:
+
```C
char* replaceSpace(char* s){
//统计空格数量
@@ -152,8 +153,8 @@ char* replaceSpace(char* s){
}
```
+### Java:
-Java:
```Java
//使用一个新的对象,复制 str,复制的过程对其判断,是空格则替换,否则直接复制,类似于数组复制
public static String replaceSpace(String s) {
@@ -211,8 +212,8 @@ public String replaceSpace(String s) {
}
```
+### Go:
-Go:
```go
// 遍历添加
func replaceSpace(s string) string {
@@ -264,9 +265,10 @@ func replaceSpace(s string) string {
+### python:
+
+因为字符串是不可变类型,所以操作字符串需要将其转换为列表,因此空间复杂度不可能为O(1)
-python:
-#### 因为字符串是不可变类型,所以操作字符串需要将其转换为列表,因此空间复杂度不可能为O(1)
(版本一)转换成列表,并且添加相匹配的空间,然后进行填充
```python
class Solution:
@@ -328,7 +330,7 @@ class Solution:
def replaceSpace(self, s: str) -> str:
return s.replace(' ', '%20')
```
-javaScript:
+### JavaScript:
```js
/**
@@ -366,7 +368,7 @@ javaScript:
};
```
-TypeScript:
+### TypeScript:
```typescript
function replaceSpace(s: string): string {
@@ -393,7 +395,7 @@ function replaceSpace(s: string): string {
};
```
-Swift:
+### Swift:
```swift
func replaceSpace(_ s: String) -> String {
@@ -434,7 +436,7 @@ func replaceSpace(_ s: String) -> String {
}
```
-Scala:
+### Scala:
方式一: 双指针
```scala
@@ -491,8 +493,8 @@ object Solution {
}
```
+### PHP:
-PHP:
```php
function replaceSpace($s){
$sLen = strlen($s);
@@ -527,7 +529,7 @@ function spaceLen($s){
}
```
-Rust
+### Rust:
```Rust
impl Solution {
@@ -563,3 +565,4 @@ impl Solution {
+
From e70ca923440e656d4c62761fcff2ff40e9c17b32 Mon Sep 17 00:00:00 2001
From: jinbudaily <18336218010@163.com>
Date: Wed, 19 Jul 2023 15:47:12 +0800
Subject: [PATCH 14/18] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200151.=E5=8F=8D?=
=?UTF-8?q?=E8=BD=AC=E5=AD=97=E7=AC=A6=E4=B8=B2=E4=B8=AD=E7=9A=84=E5=8D=95?=
=?UTF-8?q?=E8=AF=8D=20=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE?=
=?UTF-8?q?=E5=A4=8D?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0151.翻转字符串里的单词.md | 29 +++++++++++---------
1 file changed, 16 insertions(+), 13 deletions(-)
diff --git a/problems/0151.翻转字符串里的单词.md b/problems/0151.翻转字符串里的单词.md
index 6dd3cd49..19ccb725 100644
--- a/problems/0151.翻转字符串里的单词.md
+++ b/problems/0151.翻转字符串里的单词.md
@@ -28,10 +28,11 @@
输出: "example good a"
解释: 如果两个单词间有多余的空格,将反转后单词间的空格减少到只含一个。
+## 算法公开课
-# 思路
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[字符串复杂操作拿捏了! | LeetCode:151.翻转字符串里的单词](https://www.bilibili.com/video/BV1uT41177fX),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
-针对本题,我录制了视频讲解:[字符串复杂操作拿捏了! | LeetCode:151.翻转字符串里的单词](https://www.bilibili.com/video/BV1uT41177fX),结合本题解一起看,事半功倍!
+## 思路
**这道题目可以说是综合考察了字符串的多种操作。**
@@ -204,8 +205,7 @@ public:
## 其他语言版本
-
-Java:
+### Java:
```Java
class Solution {
@@ -433,9 +433,10 @@ class Solution {
}
```
-python:
+### python:
(版本一)先删除空白,然后整个反转,最后单词反转。
**因为字符串是不可变类型,所以反转单词的时候,需要将其转换成列表,然后通过join函数再将其转换成列表,所以空间复杂度不是O(1)**
+
```Python
class Solution:
def reverseWords(self, s: str) -> str:
@@ -467,7 +468,7 @@ class Solution:
return " ".join(words)
```
-Go:
+### Go:
版本一:
@@ -571,7 +572,8 @@ func reverse(b *[]byte, left, right int) {
-javaScript:
+### JavaScript:
+
```js
/**
* @param {string} s
@@ -630,7 +632,7 @@ function reverse(strArr, start, end) {
}
```
-TypeScript:
+### TypeScript:
```typescript
function reverseWords(s: string): string {
@@ -689,7 +691,7 @@ function reverseWords(s: string): string {
};
```
-Swift:
+### Swift:
```swift
func reverseWords(_ s: String) -> String {
@@ -766,7 +768,7 @@ func reverseWord(_ s: inout [Character]) {
}
```
-Scala:
+### Scala:
```scala
object Solution {
@@ -824,8 +826,8 @@ object Solution {
}
```
+### PHP:
-PHP:
```php
function reverseWords($s) {
$this->removeExtraSpaces($s);
@@ -872,7 +874,7 @@ function reverseString(&$s, $start, $end) {
return ;
}
```
-Rust:
+### Rust:
```Rust
// 根据C++版本二思路进行实现
@@ -924,7 +926,7 @@ pub fn remove_extra_spaces(s: &mut Vec
+
From 370a4d1c05b1204c652c00bcb90702947df11795 Mon Sep 17 00:00:00 2001
From: jinbudaily <18336218010@163.com>
Date: Wed, 19 Jul 2023 15:50:44 +0800
Subject: [PATCH 15/18] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E5=89=91=E6=8C=87Of?=
=?UTF-8?q?fer58-II.=E5=B7=A6=E6=97=8B=E8=BD=AC=E5=AD=97=E7=AC=A6=E4=B8=B2?=
=?UTF-8?q?=20=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../剑指Offer58-II.左旋转字符串.md | 26 +++++++++----------
1 file changed, 13 insertions(+), 13 deletions(-)
diff --git a/problems/剑指Offer58-II.左旋转字符串.md b/problems/剑指Offer58-II.左旋转字符串.md
index 6cd88456..008b7915 100644
--- a/problems/剑指Offer58-II.左旋转字符串.md
+++ b/problems/剑指Offer58-II.左旋转字符串.md
@@ -24,7 +24,7 @@
限制:
1 <= k < s.length <= 10000
-# 思路
+## 思路
为了让本题更有意义,提升一下本题难度:**不能申请额外空间,只能在本串上操作**。
@@ -71,7 +71,7 @@ public:
是不是发现这代码也太简单了,哈哈。
-# 总结
+## 总结
此时我们已经反转好多次字符串了,来一起回顾一下吧。
@@ -86,7 +86,7 @@ public:
好了,反转字符串一共就介绍到这里,相信大家此时对反转字符串的常见操作已经很了解了。
-# 题外话
+## 题外话
一些同学热衷于使用substr,来做这道题。
其实使用substr 和 反转 时间复杂度是一样的 ,都是O(n),但是使用substr申请了额外空间,所以空间复杂度是O(n),而反转方法的空间复杂度是O(1)。
@@ -96,7 +96,8 @@ public:
## 其他语言版本
-Java:
+### Java:
+
```java
class Solution {
public String reverseLeftWords(String s, int n) {
@@ -141,7 +142,7 @@ class Solution {
}
```
-python:
+### python:
(版本一)使用切片
```python
@@ -211,7 +212,7 @@ class Solution:
```
-Go:
+### Go:
```go
func reverseLeftWords(s string, n int) string {
@@ -234,8 +235,7 @@ func reverse(b []byte, left, right int){
}
```
-
-JavaScript:
+### JavaScript:
```javascript
var reverseLeftWords = function(s, n) {
@@ -279,7 +279,7 @@ var reverseLeftWords = function (s, n) {
};
```
-TypeScript:
+### TypeScript:
```typescript
function reverseLeftWords(s: string, n: number): string {
@@ -311,7 +311,7 @@ function reverseLeftWords(s: string, n: number): string {
};
```
-Swift:
+### Swift:
```swift
func reverseLeftWords(_ s: String, _ n: Int) -> String {
@@ -358,8 +358,7 @@ function reverse(&$s, $start, $end) {
}
```
-
-Scala:
+### Scala:
```scala
object Solution {
@@ -388,7 +387,7 @@ object Solution {
}
```
-Rust:
+### Rust:
```Rust
impl Solution {
@@ -419,3 +418,4 @@ impl Solution {
+
From 5acebcc6f70f37d675f03356ecfc2f8f43b5949b Mon Sep 17 00:00:00 2001
From: jinbudaily <18336218010@163.com>
Date: Wed, 19 Jul 2023 16:00:41 +0800
Subject: [PATCH 16/18] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200028.=E5=AE=9E?=
=?UTF-8?q?=E7=8E=B0strStr=20=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE?=
=?UTF-8?q?=E5=A4=8D?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0028.实现strStr.md | 51 +++++++++++++++++------------------
1 file changed, 25 insertions(+), 26 deletions(-)
diff --git a/problems/0028.实现strStr.md b/problems/0028.实现strStr.md
index ac984d1a..53b57fd5 100644
--- a/problems/0028.实现strStr.md
+++ b/problems/0028.实现strStr.md
@@ -28,7 +28,7 @@
对于本题而言,当 needle 是空字符串时我们应当返回 0 。这与C语言的 strstr() 以及 Java的 indexOf() 定义相符。
-# 思路
+## 思路
本题是KMP 经典题目。
@@ -60,13 +60,13 @@ KMP的经典思想就是:**当出现字符串不匹配时,可以记录一部
读完本篇可以顺便把leetcode上28.实现strStr()题目做了。
-# 什么是KMP
+### 什么是KMP
说到KMP,先说一下KMP这个名字是怎么来的,为什么叫做KMP呢。
因为是由这三位学者发明的:Knuth,Morris和Pratt,所以取了三位学者名字的首字母。所以叫做KMP
-# KMP有什么用
+### KMP有什么用
KMP主要应用在字符串匹配上。
@@ -84,7 +84,7 @@ KMP的主要思想是**当出现字符串不匹配时,可以知道一部分之
下面Carl就带大家把KMP的精髓,next数组弄清楚。
-# 什么是前缀表
+### 什么是前缀表
写过KMP的同学,一定都写过next数组,那么这个next数组究竟是个啥呢?
@@ -122,7 +122,7 @@ next数组就是一个前缀表(prefix table)。
那么什么是前缀表:**记录下标i之前(包括i)的字符串中,有多大长度的相同前缀后缀。**
-# 最长公共前后缀?
+### 最长公共前后缀
文章中字符串的**前缀是指不包含最后一个字符的所有以第一个字符开头的连续子串**。
@@ -144,7 +144,7 @@ next数组就是一个前缀表(prefix table)。
等等.....。
-# 为什么一定要用前缀表
+### 为什么一定要用前缀表
这就是前缀表,那为啥就能告诉我们 上次匹配的位置,并跳过去呢?
@@ -163,7 +163,7 @@ next数组就是一个前缀表(prefix table)。
**很多介绍KMP的文章或者视频并没有把为什么要用前缀表?这个问题说清楚,而是直接默认使用前缀表。**
-# 如何计算前缀表
+### 如何计算前缀表
接下来就要说一说怎么计算前缀表。
@@ -205,7 +205,7 @@ next数组就是一个前缀表(prefix table)。
最后就在文本串中找到了和模式串匹配的子串了。
-# 前缀表与next数组
+### 前缀表与next数组
很多KMP算法的时间都是使用next数组来做回退操作,那么next数组与前缀表有什么关系呢?
@@ -217,7 +217,7 @@ next数组就可以是前缀表,但是很多实现都是把前缀表统一减
后面我会提供两种不同的实现代码,大家就明白了。
-# 使用next数组来匹配
+### 使用next数组来匹配
**以下我们以前缀表统一减一之后的next数组来做演示**。
@@ -229,7 +229,7 @@ next数组就可以是前缀表,但是很多实现都是把前缀表统一减

-# 时间复杂度分析
+### 时间复杂度分析
其中n为文本串长度,m为模式串长度,因为在匹配的过程中,根据前缀表不断调整匹配的位置,可以看出匹配的过程是O(n),之前还要单独生成next数组,时间复杂度是O(m)。所以整个KMP算法的时间复杂度是O(n+m)的。
@@ -239,7 +239,7 @@ next数组就可以是前缀表,但是很多实现都是把前缀表统一减
都知道使用KMP算法,一定要构造next数组。
-# 构造next数组
+### 构造next数组
我们定义一个函数getNext来构建next数组,函数参数为指向next数组的指针,和一个字符串。 代码如下:
@@ -338,7 +338,7 @@ void getNext(int* next, const string& s){
得到了next数组之后,就要用这个来做匹配了。
-# 使用next数组来做匹配
+### 使用next数组来做匹配
在文本串s里 找是否出现过模式串t。
@@ -403,7 +403,7 @@ for (int i = 0; i < s.size(); i++) { // 注意i就从0开始
此时所有逻辑的代码都已经写出来了,力扣 28.实现strStr 题目的整体代码如下:
-# 前缀表统一减一 C++代码实现
+### 前缀表统一减一 C++代码实现
```CPP
class Solution {
@@ -447,7 +447,7 @@ public:
* 时间复杂度: O(n + m)
* 空间复杂度: O(m), 只需要保存字符串needle的前缀表
-# 前缀表(不减一)C++实现
+### 前缀表(不减一)C++实现
那么前缀表就不减一了,也不右移的,到底行不行呢?
@@ -546,7 +546,7 @@ public:
* 空间复杂度: O(m)
-# 总结
+## 总结
我们介绍了什么是KMP,KMP可以解决什么问题,然后分析KMP算法里的next数组,知道了next数组就是前缀表,再分析为什么要是前缀表而不是什么其他表。
@@ -563,8 +563,7 @@ public:
## 其他语言版本
-
-Java:
+### Java:
```Java
class Solution {
@@ -691,8 +690,9 @@ class Solution {
}
```
-Python3:
+### Python3:
(版本一)前缀表(减一)
+
```python
class Solution:
def getNext(self, next, s):
@@ -781,9 +781,9 @@ class Solution:
def strStr(self, haystack: str, needle: str) -> int:
return haystack.find(needle)
-```
+```
-Go:
+### Go:
```go
// 方法一:前缀表使用减1实现
@@ -871,7 +871,7 @@ func strStr(haystack string, needle string) int {
}
```
-JavaScript版本
+### JavaScript:
> 前缀表统一减一
@@ -959,7 +959,7 @@ var strStr = function (haystack, needle) {
};
```
-TypeScript版本:
+### TypeScript:
> 前缀表统一减一
@@ -1036,7 +1036,7 @@ function strStr(haystack: string, needle: string): number {
}
```
-Swift 版本
+### Swift:
> 前缀表统一减一
@@ -1196,7 +1196,7 @@ func strStr(_ haystack: String, _ needle: String) -> Int {
```
-PHP:
+### PHP:
> 前缀表统一减一
```php
@@ -1272,7 +1272,7 @@ function getNext(&$next, $s){
}
```
-Rust:
+### Rust:
> 前缀表统一不减一
```Rust
@@ -1362,4 +1362,3 @@ impl Solution {
-
From 202dd382d29eb34f311d5f35f001255b2b770402 Mon Sep 17 00:00:00 2001
From: jinbudaily <18336218010@163.com>
Date: Wed, 19 Jul 2023 16:06:12 +0800
Subject: [PATCH 17/18] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200459.=E9=87=8D?=
=?UTF-8?q?=E5=A4=8D=E7=9A=84=E5=AD=97=E7=AC=A6=E4=B8=B2=20=E6=8E=92?=
=?UTF-8?q?=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0459.重复的子字符串.md | 35 ++++++++++++--------------
1 file changed, 16 insertions(+), 19 deletions(-)
diff --git a/problems/0459.重复的子字符串.md b/problems/0459.重复的子字符串.md
index e26d04ad..f99102ab 100644
--- a/problems/0459.重复的子字符串.md
+++ b/problems/0459.重复的子字符串.md
@@ -5,8 +5,6 @@
+