From fdbc7442ac2b685414544f0af79d64c40da05d0f Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Mon, 17 Jul 2023 15:11:55 +0800 Subject: [PATCH 1/6] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20027.=E7=A7=BB=E9=99=A4?= =?UTF-8?q?=E5=85=83=E7=B4=A0=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/0027.移除元素.md | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/problems/0027.移除元素.md b/problems/0027.移除元素.md index 3d43a199..ce9eccf0 100644 --- a/problems/0027.移除元素.md +++ b/problems/0027.移除元素.md @@ -5,7 +5,7 @@

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

-## 27. 移除元素 +# 27. 移除元素 [力扣题目链接](https://leetcode.cn/problems/remove-element/) @@ -159,8 +159,8 @@ public: ## 其他语言版本 +### Java: -Java: ```java class Solution { public int removeElement(int[] nums, int val) { @@ -197,7 +197,7 @@ class Solution { } ``` -Python: +### Python: ``` python 3 @@ -233,8 +233,8 @@ class Solution: ``` +### Go: -Go: ```go func removeElement(nums []int, val int) int { length:=len(nums) @@ -275,7 +275,8 @@ func removeElement(nums []int, val int) int { } ``` -JavaScript: +### JavaScript: + ```javascript //时间复杂度:O(n) //空间复杂度:O(1) @@ -290,7 +291,7 @@ var removeElement = (nums, val) => { }; ``` -TypeScript: +### TypeScript: ```typescript function removeElement(nums: number[], val: number): number { @@ -305,7 +306,7 @@ function removeElement(nums: number[], val: number): number { }; ``` -Ruby: +### Ruby: ```ruby def remove_element(nums, val) @@ -319,7 +320,8 @@ def remove_element(nums, val) i end ``` -Rust: +### Rust: + ```rust impl Solution { pub fn remove_element(nums: &mut Vec, val: i32) -> i32 { @@ -335,7 +337,7 @@ impl Solution { } ``` -Swift: +### Swift: ```swift func removeElement(_ nums: inout [Int], _ val: Int) -> Int { @@ -351,7 +353,8 @@ func removeElement(_ nums: inout [Int], _ val: Int) -> Int { } ``` -PHP: +### PHP: + ```php class Solution { /** @@ -375,7 +378,8 @@ class Solution { } ``` -C: +### C: + ```c int removeElement(int* nums, int numsSize, int val){ int slow = 0; @@ -391,7 +395,8 @@ int removeElement(int* nums, int numsSize, int val){ } ``` -Kotlin: +### Kotlin: + ```kotlin fun removeElement(nums: IntArray, `val`: Int): Int { var slowIndex = 0 // 初始化慢指针 @@ -402,7 +407,8 @@ fun removeElement(nums: IntArray, `val`: Int): Int { } ``` -Scala: +### Scala: + ```scala object Solution { def removeElement(nums: Array[Int], `val`: Int): Int = { @@ -418,7 +424,8 @@ object Solution { } ``` -C#: +### C#: + ```csharp public class Solution { public int RemoveElement(int[] nums, int val) { From ec101bebb24b9af7910c02b979b198c238ae12b1 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Mon, 17 Jul 2023 15:21:00 +0800 Subject: [PATCH 2/6] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200977.=E6=9C=89?= =?UTF-8?q?=E5=BA=8F=E6=95=B0=E7=BB=84=E7=9A=84=E5=B9=B3=E6=96=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0977.有序数组的平方.md | 42 +++++++++++++++----------- 1 file changed, 25 insertions(+), 17 deletions(-) diff --git a/problems/0977.有序数组的平方.md b/problems/0977.有序数组的平方.md index de06c419..4bee585b 100644 --- a/problems/0977.有序数组的平方.md +++ b/problems/0977.有序数组的平方.md @@ -21,14 +21,14 @@ * 输入:nums = [-7,-3,2,3,11] * 输出:[4,9,9,49,121] -# 算法公开课 +## 算法公开课 **[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[双指针法经典题目!LeetCode:977.有序数组的平方](https://www.bilibili.com/video/BV1QB4y1D7ep),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 -# 思路 +## 思路 -## 暴力排序 +### 暴力排序 最直观的想法,莫过于:每个数平方之后,排个序,美滋滋,代码如下: @@ -47,7 +47,7 @@ public: 这个时间复杂度是 O(n + nlogn), 可以说是O(nlogn)的时间复杂度,但为了和下面双指针法算法时间复杂度有鲜明对比,我记为 O(n + nlog n)。 -## 双指针法 +### 双指针法 数组其实是有序的, 只不过负数平方之后可能成为最大数了。 @@ -99,7 +99,8 @@ public: ## 其他语言版本 -Java: +### Java: + ```Java class Solution { public int[] sortedSquares(int[] nums) { @@ -141,7 +142,8 @@ class Solution { } ``` -Python: +### Python: + ```Python (版本一)双指针法 class Solution: @@ -176,7 +178,8 @@ class Solution: return sorted(x*x for x in nums) ``` -Go: +### Go: + ```Go func sortedSquares(nums []int) []int { n := len(nums) @@ -196,7 +199,8 @@ func sortedSquares(nums []int) []int { return ans } ``` -Rust +### Rust: + ```rust impl Solution { pub fn sorted_squares(nums: Vec) -> Vec { @@ -217,7 +221,8 @@ impl Solution { } } ``` -Javascript: +### Javascript: + ```Javascript /** * @param {number[]} nums @@ -242,7 +247,7 @@ var sortedSquares = function(nums) { }; ``` -Typescript: +### Typescript: 双指针法: @@ -277,7 +282,7 @@ function sortedSquares(nums: number[]): number[] { }; ``` -Swift: +### Swift: ```swift func sortedSquares(_ nums: [Int]) -> [Int] { @@ -305,7 +310,7 @@ func sortedSquares(_ nums: [Int]) -> [Int] { } ``` -Ruby: +### Ruby: ```ruby def sorted_squares(nums) @@ -323,8 +328,8 @@ def sorted_squares(nums) end ``` +### C: -C: ```c int* sortedSquares(int* nums, int numsSize, int* returnSize){ //返回的数组大小就是原数组大小 @@ -357,7 +362,8 @@ int* sortedSquares(int* nums, int numsSize, int* returnSize){ } ``` -PHP: +### PHP: + ```php class Solution { /** @@ -386,7 +392,7 @@ class Solution { } ``` -Kotlin: +### Kotlin: 双指针法 ```kotlin @@ -437,7 +443,7 @@ class Solution { } ``` -Scala: +### Scala: 双指针: ```scala @@ -473,7 +479,8 @@ object Solution { } ``` -C#: +### C#: + ```csharp public class Solution { public int[] SortedSquares(int[] nums) { @@ -504,3 +511,4 @@ public class Solution { + From 76c84efca0bcfab9e889bf97458dd6b6423e0f05 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Mon, 17 Jul 2023 15:32:24 +0800 Subject: [PATCH 3/6] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200209.=20=E9=95=BF?= =?UTF-8?q?=E5=BA=A6=E6=9C=80=E5=B0=8F=E7=9A=84=E5=AD=90=E6=95=B0=E7=BB=84?= =?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/0209.长度最小的子数组.md | 41 +++++++++++++---------- 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/problems/0209.长度最小的子数组.md b/problems/0209.长度最小的子数组.md index 82d551ea..4b1d0e96 100644 --- a/problems/0209.长度最小的子数组.md +++ b/problems/0209.长度最小的子数组.md @@ -23,14 +23,14 @@ * 1 <= nums.length <= 10^5 * 1 <= nums[i] <= 10^5 -# 算法公开课 +## 算法公开课 **[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[拿下滑动窗口! | LeetCode 209 长度最小的子数组](https://www.bilibili.com/video/BV1tZ4y1q7XE),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 -# 思路 +## 思路 -## 暴力解法 +### 暴力解法 这道题目暴力解法当然是 两个for循环,然后不断的寻找符合条件的子序列,时间复杂度很明显是O(n^2)。 @@ -64,7 +64,7 @@ public: 后面力扣更新了数据,暴力解法已经超时了。 -## 滑动窗口 +### 滑动窗口 接下来就开始介绍数组操作中另一个重要的方法:**滑动窗口**。 @@ -151,8 +151,8 @@ public: ## 其他语言版本 +### Java: -Java: ```java class Solution { @@ -173,7 +173,7 @@ class Solution { } ``` -Python: +### Python: ```python (版本一)滑动窗口法 @@ -216,7 +216,8 @@ class Solution: return min_len if min_len != float('inf') else 0 ``` -Go: +### Go: + ```go func minSubArrayLen(target int, nums []int) int { i := 0 @@ -242,8 +243,7 @@ func minSubArrayLen(target int, nums []int) int { } ``` - -JavaScript: +### JavaScript: ```js var minSubArrayLen = function(target, nums) { @@ -266,7 +266,7 @@ var minSubArrayLen = function(target, nums) { }; ``` -Typescript: +### Typescript: ```typescript function minSubArrayLen(target: number, nums: number[]): number { @@ -288,7 +288,7 @@ function minSubArrayLen(target: number, nums: number[]): number { }; ``` -Swift: +### Swift: ```swift func minSubArrayLen(_ target: Int, _ nums: [Int]) -> Int { @@ -309,7 +309,7 @@ func minSubArrayLen(_ target: Int, _ nums: [Int]) -> Int { } ``` -Rust: +### Rust: ```rust impl Solution { @@ -336,7 +336,8 @@ impl Solution { } ``` -PHP: +### PHP: + ```php // 双指针 - 滑动窗口 class Solution { @@ -365,7 +366,7 @@ class Solution { } ``` -Ruby: +### Ruby: ```ruby def min_sub_array_len(target, nums) @@ -383,8 +384,9 @@ def min_sub_array_len(target, nums) end ``` -C: +### C: 暴力解法: + ```c int minSubArrayLen(int target, int* nums, int numsSize){ //初始化最小长度为INT_MAX @@ -433,7 +435,8 @@ int minSubArrayLen(int target, int* nums, int numsSize){ } ``` -Kotlin: +### Kotlin: + ```kotlin class Solution { fun minSubArrayLen(target: Int, nums: IntArray): Int { @@ -485,7 +488,7 @@ class Solution { } } ``` -Scala: +### Scala: 滑动窗口: ```scala @@ -533,7 +536,8 @@ object Solution { } } ``` -C#: +### C#: + ```csharp public class Solution { public int MinSubArrayLen(int s, int[] nums) { @@ -559,3 +563,4 @@ public class Solution { + From 7a30d500cb5f328c3e4c952f2f9ae0ecb3382b45 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Mon, 17 Jul 2023 15:47:48 +0800 Subject: [PATCH 4/6] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20059.=E8=9E=BA=E6=97=8B?= =?UTF-8?q?=E7=9F=A9=E9=98=B5II=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/0059.螺旋矩阵II.md | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/problems/0059.螺旋矩阵II.md b/problems/0059.螺旋矩阵II.md index fd40f3fc..f03fcdad 100644 --- a/problems/0059.螺旋矩阵II.md +++ b/problems/0059.螺旋矩阵II.md @@ -26,7 +26,7 @@ ## 算法公开课 **[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[拿下螺旋矩阵!LeetCode:59.螺旋矩阵II](https://www.bilibili.com/video/BV1SL4y1N7mV),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 - + ## 思路 这道题目可以说在面试中出现频率较高的题目,**本题并不涉及到什么算法,就是模拟过程,但却十分考察对代码的掌控能力。** @@ -125,15 +125,15 @@ public: ## 类似题目 -* 54.螺旋矩阵 -* 剑指Offer 29.顺时针打印矩阵 +* [54.螺旋矩阵](https://leetcode.cn/problems/spiral-matrix/) +* [剑指Offer 29.顺时针打印矩阵](https://leetcode.cn/problems/shun-shi-zhen-da-yin-ju-zhen-lcof/) ## 其他语言版本 -Java: +### Java: ```Java class Solution { @@ -176,7 +176,7 @@ class Solution { } ``` -python3: +### python3: ```python class Solution: @@ -207,7 +207,7 @@ class Solution: return nums ``` -javaScript +### JavaScript: ```javascript @@ -259,7 +259,7 @@ var generateMatrix = function(n) { ``` -TypeScript: +### TypeScript: ```typescript function generateMatrix(n: number): number[][] { @@ -304,7 +304,7 @@ function generateMatrix(n: number): number[][] { }; ``` -Go: +### Go: ```go package main @@ -397,7 +397,7 @@ func generateMatrix(n int) [][]int { } ``` -Swift: +### Swift: ```swift func generateMatrix(_ n: Int) -> [[Int]] { @@ -453,7 +453,7 @@ func generateMatrix(_ n: Int) -> [[Int]] { } ``` -Rust: +### Rust: ```rust impl Solution { @@ -506,7 +506,8 @@ impl Solution { } ``` -PHP: +### PHP: + ```php class Solution { /** @@ -548,7 +549,8 @@ class Solution { } ``` -C: +### C: + ```c int** generateMatrix(int n, int* returnSize, int** returnColumnSizes){ //初始化返回的结果数组的大小 @@ -607,7 +609,8 @@ int** generateMatrix(int n, int* returnSize, int** returnColumnSizes){ return ans; } ``` -Scala: +### Scala: + ```scala object Solution { def generateMatrix(n: Int): Array[Array[Int]] = { @@ -659,7 +662,8 @@ object Solution { } } ``` -C#: +### C#: + ```csharp public class Solution { public int[][] GenerateMatrix(int n) { @@ -688,3 +692,4 @@ public class Solution { + From 053632c31d05cae6391b29db2ec76f1945eb2cfe Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Mon, 17 Jul 2023 15:51:53 +0800 Subject: [PATCH 5/6] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E6=95=B0=E7=BB=84?= =?UTF-8?q?=E6=80=BB=E7=BB=93=E7=AF=87=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/数组总结篇.md | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/problems/数组总结篇.md b/problems/数组总结篇.md index ef962187..7550ce02 100644 --- a/problems/数组总结篇.md +++ b/problems/数组总结篇.md @@ -4,9 +4,9 @@

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

+# 数组总结篇 - -# 数组理论基础 +## 数组理论基础 数组是非常基础的数据结构,在面试中,考察数组的题目一般在思维上都不难,主要是考察对代码的掌控能力 @@ -51,7 +51,7 @@ 所以**Java的二维数组在内存中不是 `3*4` 的连续地址空间,而是四条连续的地址空间组成!** -# 数组的经典题目 +## 数组的经典题目 在面试中,数组是必考的基础数据结构。 @@ -59,7 +59,7 @@ 我们之前一共讲解了四道经典数组题目,每一道题目都代表一个类型,一种思想。 -## 二分法 +### 二分法 [数组:每次遇到二分法,都是一看就会,一写就废](https://programmercarl.com/0704.二分查找.html) @@ -75,7 +75,7 @@ **二分法是算法面试中的常考题,建议通过这道题目,锻炼自己手撕二分的能力**。 -## 双指针法 +### 双指针法 * [数组:就移除个元素很难么?](https://programmercarl.com/0027.移除元素.html) @@ -91,7 +91,7 @@ 双指针法(快慢指针法)在数组和链表的操作中是非常常见的,很多考察数组和链表操作的面试题,都使用双指针法。 -## 滑动窗口 +### 滑动窗口 * [数组:滑动窗口拯救了你](https://programmercarl.com/0209.长度最小的子数组.html) @@ -107,7 +107,7 @@ 如果没有接触过这一类的方法,很难想到类似的解题思路,滑动窗口方法还是很巧妙的。 -## 模拟行为 +### 模拟行为 * [数组:这个循环可以转懵很多人!](https://programmercarl.com/0059.螺旋矩阵II.html) @@ -118,7 +118,7 @@ 相信大家有遇到过这种情况: 感觉题目的边界调节超多,一波接着一波的判断,找边界,拆了东墙补西墙,好不容易运行通过了,代码写的十分冗余,毫无章法,其实**真正解决题目的代码都是简洁的,或者有原则性的**,大家可以在这道题目中体会到这一点。 -# 总结 +## 总结 ![](https://code-thinking-1253855093.file.myqcloud.com/pics/数组总结.png) From a3cc34ef7413e7e80420674bd261e4de393f152c Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Mon, 17 Jul 2023 15:53:46 +0800 Subject: [PATCH 6/6] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E6=95=B0=E7=BB=84?= =?UTF-8?q?=E7=90=86=E8=AE=BA=E5=9F=BA=E7=A1=80=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/数组理论基础.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/数组理论基础.md b/problems/数组理论基础.md index 67b7b20d..d104c883 100644 --- a/problems/数组理论基础.md +++ b/problems/数组理论基础.md @@ -6,7 +6,7 @@ -## 数组理论基础 +# 数组理论基础 数组是非常基础的数据结构,在面试中,考察数组的题目一般在思维上都不难,主要是考察对代码的掌控能力