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
参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!
+
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
-
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/)
-
diff --git a/problems/0383.赎金信.md b/problems/0383.赎金信.md
index e74cdf71..8122240e 100644
--- a/problems/0383.赎金信.md
+++ b/problems/0383.赎金信.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.四数相加II.md b/problems/0454.四数相加II.md
index 4a16d4f4..90c37334 100644
--- a/problems/0454.四数相加II.md
+++ b/problems/0454.四数相加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
+
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 @@
-
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 {
+
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 {
+
diff --git a/problems/哈希表总结.md b/problems/哈希表总结.md
index 9ee84f99..67506363 100644
--- a/problems/哈希表总结.md
+++ b/problems/哈希表总结.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/哈希表理论基础.md b/problems/哈希表理论基础.md
index 0ab17ec0..3055875a 100644
--- a/problems/哈希表理论基础.md
+++ b/problems/哈希表理论基础.md
@@ -8,6 +8,8 @@
+# 哈希表理论基础
+
## 哈希表
首先什么是 哈希表,哈希表(英文名字为Hash table,国内也有一些算法书籍翻译为散列表,大家看到这两个名称知道都是指hash table就可以了)。
diff --git a/problems/字符串总结.md b/problems/字符串总结.md
index c12d1764..5c2f0164 100644
--- a/problems/字符串总结.md
+++ b/problems/字符串总结.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并不容易
+