Merge pull request #2184 from jinbudaily/master

更新 数组章节 排版格式修复
This commit is contained in:
程序员Carl
2023-07-17 20:33:26 +08:00
committed by GitHub
6 changed files with 97 additions and 72 deletions

View File

@ -5,7 +5,7 @@
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!</strong></p>
## 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<i32>, 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) {

View File

@ -26,7 +26,7 @@
## 算法公开课
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html)[拿下螺旋矩阵LeetCode59.螺旋矩阵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 {
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -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 {
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -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<i32>) -> Vec<i32> {
@ -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 {
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -4,9 +4,9 @@
</a>
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!</strong></p>
# 数组总结篇
# 数组理论基础
## 数组理论基础
数组是非常基础的数据结构,在面试中,考察数组的题目一般在思维上都不难,主要是考察对代码的掌控能力
@ -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)

View File

@ -6,7 +6,7 @@
## 数组理论基础
# 数组理论基础
数组是非常基础的数据结构,在面试中,考察数组的题目一般在思维上都不难,主要是考察对代码的掌控能力