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 twoSum(List nums, int target) { var tmp = []; @@ -411,7 +415,8 @@ List twoSum(List nums, int target) { } ``` -C: +### C: + ```c diff --git a/problems/0015.三数之和.md b/problems/0015.三数之和.md index 9cca779b..4951c90c 100644 --- a/problems/0015.三数之和.md +++ b/problems/0015.三数之和.md @@ -26,14 +26,15 @@ [-1, -1, 2] ] +## 算法公开课 -# 思路 - -针对本题,我录制了视频讲解:[梦破碎的地方!| LeetCode:15.三数之和](https://www.bilibili.com/video/BV1GW4y127qo),结合本题解一起看,事半功倍! +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[梦破碎的地方!| LeetCode:15.三数之和](https://www.bilibili.com/video/BV1GW4y127qo),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 **注意[0, 0, 0, 0] 这组数据** -## 哈希解法 +## 思路 + +### 哈希解法 两层for循环就可以确定 a 和b 的数值了,可以使用哈希法来确定 0-(a+b) 是否在 数组里出现过,其实这个思路是正确的,但是我们有一个非常棘手的问题,就是题目中说的不可以包含重复的三元组。 @@ -87,7 +88,7 @@ public: * 空间复杂度: O(n),额外的 set 开销 -## 双指针 +### 双指针 **其实这道题目使用哈希法并不十分合适**,因为在去重的操作中有很多细节需要注意,在面试中很难直接写出没有bug的代码。 @@ -166,9 +167,9 @@ public: * 空间复杂度: O(1) -## 去重逻辑的思考 +### 去重逻辑的思考 -### a的去重 +#### a的去重 说道去重,其实主要考虑三个数的去重。 a, b ,c, 对应的就是 nums[i],nums[left],nums[right] @@ -188,7 +189,7 @@ a 如果重复了怎么办,a是nums里遍历的元素,那么应该直接跳 if (nums[i] == nums[i + 1]) { // 去重操作 continue; } -``` +``` 那就我们就把 三元组中出现重复元素的情况直接pass掉了。 例如{-1, -1 ,2} 这组数据,当遍历到第一个-1 的时候,判断 下一个也是-1,那这组数据就pass了。 @@ -208,7 +209,7 @@ if (i > 0 && nums[i] == nums[i - 1]) { 这是一个非常细节的思考过程。 -### b与c的去重 +#### b与c的去重 很多同学写本题的时候,去重的逻辑多加了 对right 和left 的去重:(代码中注释部分) @@ -225,7 +226,7 @@ while (right > left) { } else { } } -``` +``` 但细想一下,这种去重其实对提升程序运行效率是没有帮助的。 @@ -238,7 +239,7 @@ while (right > left) { 所以这种去重 是可以不加的。 仅仅是 把去重的逻辑提前了而已。 -# 思考题 +## 思考题 既然三数之和可以使用双指针法,我们之前讲过的[1.两数之和](https://programmercarl.com/0001.两数之和.html),可不可以使用双指针法呢? @@ -254,8 +255,8 @@ while (right > left) { ## 其他语言版本 +### Java: -Java: ```Java class Solution { public List> threeSum(int[] nums) { @@ -297,8 +298,9 @@ class Solution { } ``` -Python: +### Python: (版本一) 双指针 + ```Python class Solution: def threeSum(self, nums: List[int]) -> List[List[int]]: @@ -366,7 +368,7 @@ class Solution: return result ``` -Go: +### Go: ```Go func threeSum(nums []int) [][]int { @@ -407,7 +409,7 @@ func threeSum(nums []int) [][]int { } ``` -javaScript: +### JavaScript: ```js var threeSum = function(nums) { @@ -512,7 +514,7 @@ var threeSum = function (nums) { }; ``` -TypeScript: +### TypeScript: ```typescript function threeSum(nums: number[]): number[][] { @@ -553,7 +555,8 @@ function threeSum(nums: number[]): number[][] { }; ``` -ruby: +### Ruby: + ```ruby def is_valid(strs) symbol_map = {')' => '(', '}' => '{', ']' => '['} @@ -571,8 +574,8 @@ def is_valid(strs) end ``` +### PHP: -PHP: ```php class Solution { /** @@ -613,7 +616,8 @@ class Solution { } ``` -Swift: +### Swift: + ```swift // 双指针法 func threeSum(_ nums: [Int]) -> [[Int]] { @@ -654,7 +658,8 @@ func threeSum(_ nums: [Int]) -> [[Int]] { } ``` -Rust: +### Rust: + ```Rust // 哈希解法 use std::collections::HashSet; @@ -718,7 +723,8 @@ impl Solution { } ``` -C: +### C: + ```C //qsort辅助cmp函数 int cmp(const void* ptr1, const void* ptr2) { @@ -792,7 +798,8 @@ int** threeSum(int* nums, int numsSize, int* returnSize, int** returnColumnSizes } ``` -C#: +### C#: + ```csharp public class Solution { @@ -850,7 +857,8 @@ public class Solution } } ``` -Scala: +### Scala: + ```scala object Solution { // 导包 @@ -898,3 +906,4 @@ object Solution { + diff --git a/problems/0018.四数之和.md b/problems/0018.四数之和.md index a4d41d9b..28c20b7a 100644 --- a/problems/0018.四数之和.md +++ b/problems/0018.四数之和.md @@ -27,9 +27,11 @@ [-2, 0, 0, 2] ] -# 思路 +## 算法公开课 -针对本题,我录制了视频讲解:[难在去重和剪枝!| LeetCode:18. 四数之和](https://www.bilibili.com/video/BV1DS4y147US),结合本题解一起看,事半功倍! +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[难在去重和剪枝!| LeetCode:18. 四数之和](https://www.bilibili.com/video/BV1DS4y147US),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 + +## 思路 四数之和,和[15.三数之和](https://programmercarl.com/0015.三数之和.html)是一个思路,都是使用双指针法, 基本解法就是在[15.三数之和](https://programmercarl.com/0015.三数之和.html) 的基础上再套一层for循环。 @@ -141,22 +143,16 @@ if (nums[k] + nums[i] > target && nums[k] + nums[i] >= 0) { if (nums[k] + nums[i] > target && nums[i] >= 0) { break; } -``` +``` 因为只要 nums[k] + nums[i] > target,那么 nums[i] 后面的数都是正数的话,就一定 不符合条件了。 不过这种剪枝 其实有点 小绕,大家能够理解 文章给的完整代码的剪枝 就够了。 - - - - - - ## 其他语言版本 +### Java: -Java: ```Java class Solution { public List> fourSum(int[] nums, int target) { @@ -206,8 +202,9 @@ class Solution { } ``` -Python: +### Python: (版本一) 双指针 + ```python class Solution: def fourSum(self, nums: List[int], target: int) -> List[List[int]]: @@ -273,7 +270,8 @@ class Solution(object): ``` -Go: +### Go: + ```go func fourSum(nums []int, target int) [][]int { if len(nums) < 4 { @@ -323,7 +321,7 @@ func fourSum(nums []int, target int) [][]int { } ``` -javaScript: +### JavaScript: ```js /** @@ -359,7 +357,7 @@ var fourSum = function(nums, target) { }; ``` -TypeScript: +### TypeScript: ```typescript function fourSum(nums: number[], target: number): number[][] { @@ -400,7 +398,7 @@ function fourSum(nums: number[], target: number): number[][] { }; ``` -PHP: +### PHP: ```php class Solution { @@ -445,7 +443,8 @@ class Solution { } ``` -Swift: +### Swift: + ```swift func fourSum(_ nums: [Int], _ target: Int) -> [[Int]] { var res = [[Int]]() @@ -493,7 +492,8 @@ func fourSum(_ nums: [Int], _ target: Int) -> [[Int]] { } ``` -C#: +### C#: + ```csharp public class Solution { @@ -555,7 +555,8 @@ public class Solution } ``` -Rust: +### Rust: + ```Rust use std::cmp::Ordering; impl Solution { @@ -603,7 +604,8 @@ impl Solution { } ``` -Scala: +### Scala: + ```scala object Solution { // 导包 @@ -651,3 +653,4 @@ object Solution { + 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数组就可以是前缀表,但是很多实现都是把前缀表统一减 ![KMP精讲4](https://code-thinking.cdn.bcebos.com/gifs/KMP%E7%B2%BE%E8%AE%B24.gif) -# 时间复杂度分析 +### 时间复杂度分析 其中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 { - 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) { } } ``` -C: +### C: ```C // 翻转字符串中指定范围的字符 @@ -972,3 +974,4 @@ char * reverseWords(char * s){ + 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 { + 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/)

+ 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) { @@ -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.两个数组的交集.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 dic = new Dictionary(); @@ -411,3 +416,4 @@ public int FourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) { + 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 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

- - > KMP算法还能干这个 # 459.重复的子字符串 @@ -29,9 +27,11 @@ * 输出: True * 解释: 可由子字符串 "abc" 重复四次构成。 (或者子字符串 "abcabc" 重复两次构成。) -# 思路 +## 算法公开课 -针对本题,我录制了视频讲解:[字符串这么玩,可有点难度! | LeetCode:459.重复的子字符串](https://www.bilibili.com/video/BV1cg41127fw),结合本题解一起看,事半功倍! +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[字符串这么玩,可有点难度! | 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数组为什么遇到字符不匹配的时候可以找到上一 ![图三](https://code-thinking-1253855093.file.myqcloud.com/pics/20220728205249.png) -### 如何找到最小重复子串 +#### 如何找到最小重复子串 这里有同学就问了,为啥一定是开头的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.反转字符串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 { - 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并不容易 +