) {
}
}
```
-C:
+### C:
```C
// 翻转字符串中指定范围的字符
@@ -972,3 +974,4 @@ char * reverseWords(char * s){
+
diff --git "a/problems/0202.345円277円253円344円271円220円346円225円260円.md" "b/problems/0202.345円277円253円344円271円220円346円225円260円.md"
index 7fe8cd8d75..4a77e2b67c 100644
--- "a/problems/0202.345円277円253円344円271円220円346円225円260円.md"
+++ "b/problems/0202.345円277円253円344円271円220円346円225円260円.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 {
+
diff --git "a/problems/0242.346円234円211円346円225円210円347円232円204円345円255円227円346円257円215円345円274円202円344円275円215円350円257円215円.md" "b/problems/0242.346円234円211円346円225円210円347円232円204円345円255円227円346円257円215円345円274円202円344円275円215円350円257円215円.md"
index 4ea43947e8..f47d8b05b6 100644
--- "a/problems/0242.346円234円211円346円225円210円347円232円204円345円255円227円346円257円215円345円274円202円344円275円215円350円257円215円.md"
+++ "b/problems/0242.346円234円211円346円225円210円347円232円204円345円255円227円346円257円215円345円274円202円344円275円215円350円257円215円.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/)
+
diff --git "a/problems/0344.345円217円215円350円275円254円345円255円227円347円254円246円344円270円262円.md" "b/problems/0344.345円217円215円350円275円254円345円255円227円347円254円246円344円270円262円.md"
index 1c74f9aa46..8a4fed4574 100644
--- "a/problems/0344.345円217円215円350円275円254円345円255円227円347円254円246円344円270円262円.md"
+++ "b/problems/0344.345円217円215円350円275円254円345円255円227円347円254円246円344円270円262円.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) {
@@ -332,7 +337,8 @@ impl Solution {
}
```
-C:
+### C:
+
```c
void reverseString(char* s, int sSize){
int left = 0;
@@ -347,7 +353,8 @@ void reverseString(char* s, int sSize){
}
```
-C#:
+### C#:
+
```csharp
public class Solution
{
@@ -361,8 +368,8 @@ public class Solution
}
```
+### PHP:
-PHP:
```php
// 双指针
// 一:
@@ -392,7 +399,8 @@ function reverse(&$s, $start, $end) {
}
```
-Scala:
+### Scala:
+
```scala
object Solution {
def reverseString(s: Array[Char]): Unit = {
@@ -411,4 +419,3 @@ object Solution {
-
diff --git "a/problems/0349.344円270円244円344円270円252円346円225円260円347円273円204円347円232円204円344円272円244円351円233円206円.md" "b/problems/0349.344円270円244円344円270円252円346円225円260円347円273円204円347円232円204円344円272円244円351円233円206円.md"
index c2f6ef46ba..8daf5a35ba 100644
--- "a/problems/0349.344円270円244円344円270円252円346円225円260円347円273円204円347円232円204円344円272円244円351円233円206円.md"
+++ "b/problems/0349.344円270円244円344円270円252円346円225円260円347円273円204円347円232円204円344円272円244円351円233円206円.md"
@@ -10,7 +10,7 @@
> 如果哈希值比较少、特别分散、跨度非常大,使用数组就造成空间的极大浪费!
-## 349. 两个数组的交集
+# 349. 两个数组的交集
[力扣题目链接](https://leetcode.cn/problems/intersection-of-two-arrays/)
@@ -22,9 +22,11 @@
输出结果中的每个元素一定是唯一的。
我们可以不考虑输出结果的顺序。
-## 思路
+## 算法公开课
+
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html)::[学透哈希表,set使用有技巧!Leetcode:349. 两个数组的交集](https://www.bilibili.com/video/BV1ba411S7wu),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
-关于本题,我录制了讲解视频:[学透哈希表,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/)
-
diff --git "a/problems/0383.350円265円216円351円207円221円344円277円241円.md" "b/problems/0383.350円265円216円351円207円221円344円277円241円.md"
index e74cdf71fd..8122240ecc 100644
--- "a/problems/0383.350円265円216円351円207円221円344円277円241円.md"
+++ "b/problems/0383.350円265円216円351円207円221円344円277円241円.md"
@@ -34,7 +34,7 @@ canConstruct("aa", "aab") -> true
* 第二点 "你可以假设两个字符串均只含有小写字母。" *说明只有小写字母*,这一点很重要
-## 暴力解法
+### 暴力解法
那么第一个思路其实就是暴力枚举了,两层for循环,不断去寻找,代码如下:
@@ -66,7 +66,7 @@ public:
这里时间复杂度是比较高的,而且里面还有一个字符串删除也就是erase的操作,也是费时的,当然这段代码也可以过这道题。
-## 哈希解法
+### 哈希解法
因为题目所只有小写字母,那可以采用空间换取时间的哈希策略, 用一个长度为26的数组还记录magazine里字母出现的次数。
@@ -112,8 +112,8 @@ public:
## 其他语言版本
+### Java:
-Java:
```Java
class Solution {
public boolean canConstruct(String ransomNote, String magazine) {
@@ -146,8 +146,9 @@ class Solution {
```
-Python:
+### Python:
(版本一)使用数组
+
```python
class Solution:
def canConstruct(self, ransomNote: str, magazine: str) -> bool:
@@ -213,7 +214,7 @@ class Solution:
return all(ransomNote.count(c) <= magazine.count(c) for c in set(ransomNote)) ``` -Go: +### Go: ```go func canConstruct(ransomNote string, magazine string) bool { @@ -231,7 +232,7 @@ func canConstruct(ransomNote string, magazine string) bool { } ``` -javaScript: +### JavaScript: ```js /** @@ -254,7 +255,7 @@ var canConstruct = function(ransomNote, magazine) { }; ``` -TypeScript: +### TypeScript: ```typescript function canConstruct(ransomNote: string, magazine: string): boolean { @@ -275,8 +276,8 @@ function canConstruct(ransomNote: string, magazine: string): boolean { }; ``` +### PHP: -PHP: ```php class Solution { /** @@ -301,7 +302,8 @@ class Solution { } ``` -Swift: +### Swift: + ```swift func canConstruct(_ ransomNote: String, _ magazine: String) -> Bool {
var record = Array(repeating: 0, count: 26);
@@ -324,7 +326,8 @@ func canConstruct(_ ransomNote: String, _ magazine: String) -> Bool {
}
```
-Rust:
+### Rust:
+
```rust
impl Solution {
pub fn can_construct(ransom_note: String, magazine: String) -> bool {
@@ -347,7 +350,7 @@ impl Solution {
}
```
-Scala:
+### Scala:
版本一: 使用数组作为哈希表
```scala
@@ -411,8 +414,8 @@ object Solution {
}
```
+### C#:
-C#:
```csharp
public bool CanConstruct(string ransomNote, string magazine) {
if(ransomNote.Length> magazine.Length) return false;
@@ -434,3 +437,4 @@ public bool CanConstruct(string ransomNote, string magazine) {
+
diff --git "a/problems/0454.345円233円233円346円225円260円347円233円270円345円212円240円II.md" "b/problems/0454.345円233円233円346円225円260円347円233円270円345円212円240円II.md"
index 4a16d4f4a7..90c3733449 100644
--- "a/problems/0454.345円233円233円346円225円260円347円233円270円345円212円240円II.md"
+++ "b/problems/0454.345円233円233円346円225円260円347円233円270円345円212円240円II.md"
@@ -34,10 +34,12 @@
1. (0, 0, 0, 1) -> A[0] + B[0] + C[0] + D[1] = 1 + (-2) + (-1) + 2 = 0
2. (1, 1, 0, 0) -> A[1] + B[1] + C[0] + D[0] = 2 + (-1) + (-1) + 0 = 0
+## 算法公开课
-# 思路
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[学透哈希表,map使用有技巧!LeetCode:454.四数相加II](https://www.bilibili.com/video/BV1Md4y1Q7Yh),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
-本题视频讲解:[学透哈希表,map使用有技巧!LeetCode:454.四数相加II](https://www.bilibili.com/video/BV1Md4y1Q7Yh),结合视频在看本题解,事半功倍。
+
+## 思路
本题咋眼一看好像和[0015.三数之和](https://programmercarl.com/0015.三数之和.html),[0018.四数之和](https://programmercarl.com/0018.四数之和.html)差不多,其实差很多。
@@ -92,8 +94,8 @@ public:
## 其他语言版本
+### Java:
-Java:
```Java
class Solution {
public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
@@ -117,8 +119,9 @@ class Solution {
}
```
-Python:
+### Python:
(版本一) 使用字典
+
```python
class Solution(object):
def fourSumCount(self, nums1, nums2, nums3, nums4):
@@ -179,7 +182,7 @@ class Solution:
return cnt
```
-Go:
+### Go:
```go
func fourSumCount(nums1 []int, nums2 []int, nums3 []int, nums4 []int) int {
@@ -201,7 +204,7 @@ func fourSumCount(nums1 []int, nums2 []int, nums3 []int, nums4 []int) int {
}
```
-javaScript:
+### JavaScript:
```js
/**
@@ -233,7 +236,7 @@ var fourSumCount = function(nums1, nums2, nums3, nums4) {
};
```
-TypeScript:
+### TypeScript:
```typescript
function fourSumCount(nums1: number[], nums2: number[], nums3: number[], nums4: number[]): number {
@@ -258,7 +261,7 @@ function fourSumCount(nums1: number[], nums2: number[], nums3: number[], nums4:
};
```
-PHP:
+### PHP:
```php
class Solution {
@@ -291,8 +294,8 @@ class Solution {
}
```
+### Swift:
-Swift:
```swift
func fourSumCount(_ nums1: [Int], _ nums2: [Int], _ nums3: [Int], _ nums4: [Int]) -> Int {
// ab和: ab和出现次数
@@ -316,7 +319,8 @@ func fourSumCount(_ nums1: [Int], _ nums2: [Int], _ nums3: [Int], _ nums4: [Int]
}
```
-Rust:
+### Rust:
+
```rust
use std::collections::HashMap;
impl Solution {
@@ -342,8 +346,8 @@ impl Solution {
}
```
+### Scala:
-Scala:
```scala
object Solution {
// 导包
@@ -380,7 +384,8 @@ object Solution {
}
```
-C#:
+### C#:
+
```csharp
public int FourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
Dictionary dic = new Dictionary();
@@ -411,3 +416,4 @@ public int FourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
+
diff --git "a/problems/0459.351円207円215円345円244円215円347円232円204円345円255円220円345円255円227円347円254円246円344円270円262円.md" "b/problems/0459.351円207円215円345円244円215円347円232円204円345円255円220円345円255円227円347円254円246円344円270円262円.md"
index e26d04ad87..f99102ab45 100644
--- "a/problems/0459.351円207円215円345円244円215円347円232円204円345円255円220円345円255円227円347円254円246円344円270円262円.md"
+++ "b/problems/0459.351円207円215円345円244円215円347円232円204円345円255円220円345円255円227円347円254円246円344円270円262円.md"
@@ -5,8 +5,6 @@
参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!
-
-
> KMP算法还能干这个
# 459.重复的子字符串
@@ -29,9 +27,11 @@
* 输出: True
* 解释: 可由子字符串 "abc" 重复四次构成。 (或者子字符串 "abcabc" 重复两次构成。)
-# 思路
+## 算法公开课
+
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[字符串这么玩,可有点难度! | LeetCode:459.重复的子字符串](https://www.bilibili.com/video/BV1cg41127fw),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
-针对本题,我录制了视频讲解:[字符串这么玩,可有点难度! | LeetCode:459.重复的子字符串](https://www.bilibili.com/video/BV1cg41127fw),结合本题解一起看,事半功倍!
+## 思路
暴力的解法, 就是一个for循环获取 子串的终止位置, 然后判断子串是否能重复构成字符串,又嵌套一个for循环,所以是O(n^2)的时间复杂度。
@@ -44,7 +44,7 @@
主要讲一讲移动匹配 和 KMP两种方法。
-## 移动匹配
+### 移动匹配
当一个字符串s:abcabc,内部由重复的子串组成,那么这个字符串的结构一定是这样的:
@@ -80,9 +80,9 @@ public:
如果我们做过 [28.实现strStr](https://programmercarl.com/0028.实现strStr.html) 题目的话,其实就知道,**实现一个 高效的算法来判断 一个字符串中是否出现另一个字符串是很复杂的**,这里就涉及到了KMP算法。
-## KMP
+### KMP
-### 为什么会使用KMP
+#### 为什么会使用KMP
以下使用KMP方式讲解,强烈建议大家先把以下两个视频看了,理解KMP算法,再来看下面讲解,否则会很懵。
* [视频讲解版:帮你把KMP算法学个通透!(理论篇)](https://www.bilibili.com/video/BV1PD4y1o7nd/)
@@ -105,7 +105,7 @@ KMP算法中next数组为什么遇到字符不匹配的时候可以找到上一

-### 如何找到最小重复子串
+#### 如何找到最小重复子串
这里有同学就问了,为啥一定是开头的ab呢。 其实最关键还是要理解 最长相等前后缀,如图:
@@ -123,7 +123,7 @@ KMP算法中next数组为什么遇到字符不匹配的时候可以找到上一
正是因为 最长相等前后缀的规则,当一个字符串由重复子串组成的,最长相等前后缀不包含的子串就是最小重复子串。
-### 简单推理
+#### 简单推理
这里再给出一个数学推导,就容易理解很多。
@@ -229,7 +229,7 @@ public:
## 其他语言版本
-Java:
+### Java:
```java
class Solution {
@@ -261,8 +261,7 @@ class Solution {
}
```
-
-Python:
+### Python:
(版本一) 前缀表 减一
```python
@@ -346,8 +345,7 @@ class Solution:
return False
```
-
-Go:
+### Go:
这里使用了前缀表统一减一的实现方式
@@ -405,7 +403,7 @@ func repeatedSubstringPattern(s string) bool {
}
```
-JavaScript版本
+### JavaScript:
> 前缀表统一减一
@@ -479,7 +477,7 @@ var repeatedSubstringPattern = function (s) {
};
```
-TypeScript:
+### TypeScript:
> 前缀表统一减一
@@ -539,8 +537,7 @@ function repeatedSubstringPattern(s: string): boolean {
};
```
-
-Swift:
+### Swift:
> 前缀表统一减一
```swift
@@ -623,7 +620,7 @@ Swift:
}
```
-Rust:
+### Rust:
>前缀表统一不减一
```Rust
diff --git "a/problems/0541.345円217円215円350円275円254円345円255円227円347円254円246円344円270円262円II.md" "b/problems/0541.345円217円215円350円275円254円345円255円227円347円254円246円344円270円262円II.md"
index 179395b3ac..80e662f9dd 100644
--- "a/problems/0541.345円217円215円350円275円254円345円255円227円347円254円246円344円270円262円II.md"
+++ "b/problems/0541.345円217円215円350円275円254円345円255円227円347円254円246円344円270円262円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 {
-
diff --git "a/problems/345円211円221円346円214円207円Offer05.346円233円277円346円215円242円347円251円272円346円240円274円.md" "b/problems/345円211円221円346円214円207円Offer05.346円233円277円346円215円242円347円251円272円346円240円274円.md"
index dbad781ef3..fed08a5346 100644
--- "a/problems/345円211円221円346円214円207円Offer05.346円233円277円346円215円242円347円251円272円346円240円274円.md"
+++ "b/problems/345円211円221円346円214円207円Offer05.346円233円277円346円215円242円347円251円272円346円240円274円.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 {
+
diff --git "a/problems/345円211円221円346円214円207円Offer58-II.345円267円246円346円227円213円350円275円254円345円255円227円347円254円246円344円270円262円.md" "b/problems/345円211円221円346円214円207円Offer58-II.345円267円246円346円227円213円350円275円254円345円255円227円347円254円246円344円270円262円.md"
index 6cd8845609..008b7915c3 100644
--- "a/problems/345円211円221円346円214円207円Offer58-II.345円267円246円346円227円213円350円275円254円345円255円227円347円254円246円344円270円262円.md"
+++ "b/problems/345円211円221円346円214円207円Offer58-II.345円267円246円346円227円213円350円275円254円345円255円227円347円254円246円344円270円262円.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 {
+
diff --git "a/problems/345円223円210円345円270円214円350円241円250円346円200円273円347円273円223円.md" "b/problems/345円223円210円345円270円214円350円241円250円346円200円273円347円273円223円.md"
index 9ee84f991b..6750636305 100644
--- "a/problems/345円223円210円345円270円214円350円241円250円346円200円273円347円273円223円.md"
+++ "b/problems/345円223円210円345円270円214円350円241円250円346円200円273円347円273円223円.md"
@@ -7,8 +7,10 @@
> 哈希表总结篇如约而至
+# 哈希表总结篇
-# 哈希表理论基础
+
+## 哈希表理论基础
在[关于哈希表,你该了解这些!](https://programmercarl.com/哈希表理论基础.html)中,我们介绍了哈希表的基础理论知识,不同于枯燥的讲解,这里介绍了都是对刷题有帮助的理论知识点。
@@ -32,9 +34,9 @@
**只有对这些数据结构的底层实现很熟悉,才能灵活使用,否则很容易写出效率低下的程序**。
-# 哈希表经典题目
+## 哈希表经典题目
-## 数组作为哈希表
+### 数组作为哈希表
一些应用场景就是为数组量身定做的。
@@ -51,7 +53,7 @@
**上面两道题目用map确实可以,但使用map的空间消耗要比数组大一些,因为map要维护红黑树或者符号表,而且还要做哈希函数的运算。所以数组更加简单直接有效!**
-## set作为哈希表
+### set作为哈希表
在[349. 两个数组的交集](https://programmercarl.com/0349.两个数组的交集.html)中我们给出了什么时候用数组就不行了,需要用set。
@@ -75,7 +77,7 @@ std::set和std::multiset底层实现都是红黑树,std::unordered_set的底
在[202.快乐数](https://programmercarl.com/0202.快乐数.html)中,我们再次使用了unordered_set来判断一个数是否重复出现过。
-## map作为哈希表
+### map作为哈希表
在[1.两数之和](https://programmercarl.com/0001.两数之和.html)中map正式登场。
@@ -110,7 +112,7 @@ std::unordered_map 底层实现为哈希,std::map 和std::multimap 的底层
所以18. 四数之和,15.三数之和都推荐使用双指针法!
-# 总结
+## 总结
对于哈希表的知识相信很多同学都知道,但是没有成体系。
@@ -123,9 +125,8 @@ std::unordered_map 底层实现为哈希,std::map 和std::multimap 的底层
-
-
+
diff --git "a/problems/345円223円210円345円270円214円350円241円250円347円220円206円350円256円272円345円237円272円347円241円200円.md" "b/problems/345円223円210円345円270円214円350円241円250円347円220円206円350円256円272円345円237円272円347円241円200円.md"
index 0ab17ec02b..3055875a11 100644
--- "a/problems/345円223円210円345円270円214円350円241円250円347円220円206円350円256円272円345円237円272円347円241円200円.md"
+++ "b/problems/345円223円210円345円270円214円350円241円250円347円220円206円350円256円272円345円237円272円347円241円200円.md"
@@ -8,6 +8,8 @@
+# 哈希表理论基础
+
## 哈希表
首先什么是 哈希表,哈希表(英文名字为Hash table,国内也有一些算法书籍翻译为散列表,大家看到这两个名称知道都是指hash table就可以了)。
diff --git "a/problems/345円255円227円347円254円246円344円270円262円346円200円273円347円273円223円.md" "b/problems/345円255円227円347円254円246円344円270円262円346円200円273円347円273円223円.md"
index c12d1764ad..5c2f016471 100644
--- "a/problems/345円255円227円347円254円246円344円270円262円346円200円273円347円273円223円.md"
+++ "b/problems/345円255円227円347円254円246円344円270円262円346円200円273円347円273円223円.md"
@@ -11,7 +11,7 @@
那么这次我们来做一个总结。
-# 什么是字符串
+## 什么是字符串
字符串是若干字符组成的有限序列,也可以理解为是一个字符数组,但是很多语言对字符串做了特殊的规定,接下来我来说一说C/C++中的字符串。
@@ -42,7 +42,7 @@ for (int i = 0; i < a.size(); i++) { 所以想处理字符串,我们还是会定义一个string类型。 -# 要不要使用库函数 +## 要不要使用库函数 在文章[344.反转字符串](https://programmercarl.com/0344.反转字符串.html)中强调了**打基础的时候,不要太迷恋于库函数。** @@ -52,7 +52,7 @@ for (int i = 0; i < a.size(); i++) { **如果库函数仅仅是 解题过程中的一小部分,并且你已经很清楚这个库函数的内部实现原理的话,可以考虑使用库函数。** -# 双指针法 +## 双指针法 在[344.反转字符串](https://programmercarl.com/0344.反转字符串.html) ,我们使用双指针法实现了反转字符串的操作,**双指针法在数组,链表和字符串中很常用。** @@ -67,7 +67,7 @@ for (int i = 0; i < a.size(); i++) { 一些同学会使用for循环里调用库函数erase来移除元素,这其实是O(n^2)的操作,因为erase就是O(n)的操作,所以这也是典型的不知道库函数的时间复杂度,上来就用的案例了。 -# 反转系列 +## 反转系列 在反转上还可以在加一些玩法,其实考察的是对代码的掌控能力。 @@ -87,7 +87,7 @@ for (int i = 0; i < a.size(); i++) { 在[字符串:反转个字符串还有这个用处?](https://programmercarl.com/剑指Offer58-II.左旋转字符串.html)中,我们通过**先局部反转再整体反转**达到了左旋的效果。 -# KMP +## KMP KMP的主要思想是**当出现字符串不匹配时,可以知道一部分之前已经匹配的文本内容,可以利用这些信息避免从头再去做匹配了。** @@ -110,7 +110,7 @@ KMP的精髓所在就是前缀表,在[KMP精讲](https://programmercarl.com/00 其中主要**理解j=next[x]这一步最为关键!** -# 总结 +## 总结 字符串类类型的题目,往往想法比较简单,但是实现起来并不容易,复杂的字符串题目非常考验对代码的掌控能力。 @@ -128,3 +128,4 @@ KMP算法是字符串查找最重要的算法,但彻底理解KMP并不容易
+