mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
更新 单调栈系列题目 排版格式修复
This commit is contained in:
@ -29,7 +29,7 @@
|
|||||||
* 输出:9
|
* 输出:9
|
||||||
|
|
||||||
|
|
||||||
# 思路
|
## 思路
|
||||||
|
|
||||||
接雨水问题在面试中还是常见题目的,有必要好好讲一讲。
|
接雨水问题在面试中还是常见题目的,有必要好好讲一讲。
|
||||||
|
|
||||||
@ -39,7 +39,7 @@
|
|||||||
* 动态规划
|
* 动态规划
|
||||||
* 单调栈
|
* 单调栈
|
||||||
|
|
||||||
## 暴力解法
|
### 暴力解法
|
||||||
|
|
||||||
本题暴力解法也是也是使用双指针。
|
本题暴力解法也是也是使用双指针。
|
||||||
|
|
||||||
@ -137,7 +137,7 @@ public:
|
|||||||
|
|
||||||
力扣后面修改了后台测试数据,所以以上暴力解法超时了。
|
力扣后面修改了后台测试数据,所以以上暴力解法超时了。
|
||||||
|
|
||||||
## 双指针优化
|
### 双指针优化
|
||||||
|
|
||||||
|
|
||||||
在暴力解法中,我们可以看到只要记录左边柱子的最高高度 和 右边柱子的最高高度,就可以计算当前位置的雨水面积,这就是通过列来计算。
|
在暴力解法中,我们可以看到只要记录左边柱子的最高高度 和 右边柱子的最高高度,就可以计算当前位置的雨水面积,这就是通过列来计算。
|
||||||
@ -184,7 +184,7 @@ public:
|
|||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
## 单调栈解法
|
### 单调栈解法
|
||||||
|
|
||||||
关于单调栈的理论基础,单调栈适合解决什么问题,单调栈的工作过程,大家可以先看这题讲解 [739. 每日温度](https://programmercarl.com/0739.每日温度.html)。
|
关于单调栈的理论基础,单调栈适合解决什么问题,单调栈的工作过程,大家可以先看这题讲解 [739. 每日温度](https://programmercarl.com/0739.每日温度.html)。
|
||||||
|
|
||||||
@ -194,7 +194,7 @@ public:
|
|||||||
|
|
||||||
而接雨水这道题目,我们正需要寻找一个元素,右边最大元素以及左边最大元素,来计算雨水面积。
|
而接雨水这道题目,我们正需要寻找一个元素,右边最大元素以及左边最大元素,来计算雨水面积。
|
||||||
|
|
||||||
### 准备工作
|
#### 准备工作
|
||||||
|
|
||||||
那么本题使用单调栈有如下几个问题:
|
那么本题使用单调栈有如下几个问题:
|
||||||
|
|
||||||
@ -248,7 +248,7 @@ stack<int> st; // 存着下标,计算的时候用下标对应的柱子高度
|
|||||||
|
|
||||||
明确了如上几点,我们再来看处理逻辑。
|
明确了如上几点,我们再来看处理逻辑。
|
||||||
|
|
||||||
### 单调栈处理逻辑
|
#### 单调栈处理逻辑
|
||||||
|
|
||||||
以下操作过程其实和 [739. 每日温度](https://programmercarl.com/0739.每日温度.html) 也是一样的,建议先做 [739. 每日温度](https://programmercarl.com/0739.每日温度.html)。
|
以下操作过程其实和 [739. 每日温度](https://programmercarl.com/0739.每日温度.html) 也是一样的,建议先做 [739. 每日温度](https://programmercarl.com/0739.每日温度.html)。
|
||||||
|
|
||||||
@ -596,7 +596,7 @@ class Solution:
|
|||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
### Go
|
### Go:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func trap(height []int) int {
|
func trap(height []int) int {
|
||||||
@ -802,7 +802,7 @@ var trap = function(height) {
|
|||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
### TypeScript
|
### TypeScript:
|
||||||
|
|
||||||
暴力解法:
|
暴力解法:
|
||||||
|
|
||||||
@ -925,8 +925,7 @@ int trap(int* height, int heightSize) {
|
|||||||
* 时间复杂度 O(n)
|
* 时间复杂度 O(n)
|
||||||
* 空间复杂度 O(1)
|
* 空间复杂度 O(1)
|
||||||
|
|
||||||
|
### Rust:
|
||||||
Rust
|
|
||||||
|
|
||||||
双指针
|
双指针
|
||||||
|
|
||||||
@ -980,3 +979,4 @@ impl Solution {
|
|||||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||||
</a>
|
</a>
|
||||||
|
|
||||||
|
@ -20,7 +20,7 @@
|
|||||||
* 1 <= heights.length <=10^5
|
* 1 <= heights.length <=10^5
|
||||||
* 0 <= heights[i] <= 10^4
|
* 0 <= heights[i] <= 10^4
|
||||||
|
|
||||||
# 思路
|
## 思路
|
||||||
|
|
||||||
本题和[42. 接雨水](https://programmercarl.com/0042.接雨水.html),是遥相呼应的两道题目,建议都要仔细做一做,原理上有很多相同的地方,但细节上又有差异,更可以加深对单调栈的理解!
|
本题和[42. 接雨水](https://programmercarl.com/0042.接雨水.html),是遥相呼应的两道题目,建议都要仔细做一做,原理上有很多相同的地方,但细节上又有差异,更可以加深对单调栈的理解!
|
||||||
|
|
||||||
@ -28,7 +28,7 @@
|
|||||||
|
|
||||||
我们先来看一下暴力解法的解法:
|
我们先来看一下暴力解法的解法:
|
||||||
|
|
||||||
## 暴力解法
|
### 暴力解法
|
||||||
|
|
||||||
```CPP
|
```CPP
|
||||||
class Solution {
|
class Solution {
|
||||||
@ -55,7 +55,7 @@ public:
|
|||||||
|
|
||||||
如上代码并不能通过leetcode,超时了,因为时间复杂度是$O(n^2)$。
|
如上代码并不能通过leetcode,超时了,因为时间复杂度是$O(n^2)$。
|
||||||
|
|
||||||
## 双指针解法
|
### 双指针解法
|
||||||
|
|
||||||
本题双指针的写法整体思路和[42. 接雨水](https://programmercarl.com/0042.接雨水.html)是一致的,但要比[42. 接雨水](https://programmercarl.com/0042.接雨水.html)难一些。
|
本题双指针的写法整体思路和[42. 接雨水](https://programmercarl.com/0042.接雨水.html)是一致的,但要比[42. 接雨水](https://programmercarl.com/0042.接雨水.html)难一些。
|
||||||
|
|
||||||
@ -98,7 +98,7 @@ public:
|
|||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
## 单调栈
|
### 单调栈
|
||||||
|
|
||||||
本地单调栈的解法和接雨水的题目是遥相呼应的。
|
本地单调栈的解法和接雨水的题目是遥相呼应的。
|
||||||
|
|
||||||
@ -229,7 +229,7 @@ public:
|
|||||||
|
|
||||||
## 其他语言版本
|
## 其他语言版本
|
||||||
|
|
||||||
Java:
|
### Java:
|
||||||
|
|
||||||
暴力解法:
|
暴力解法:
|
||||||
```java
|
```java
|
||||||
@ -335,7 +335,7 @@ class Solution {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Python3:
|
### Python3:
|
||||||
|
|
||||||
```python
|
```python
|
||||||
|
|
||||||
@ -468,7 +468,7 @@ class Solution:
|
|||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
Go:
|
### Go:
|
||||||
|
|
||||||
> 单调栈
|
> 单调栈
|
||||||
|
|
||||||
@ -506,7 +506,8 @@ func largestRectangleArea(heights []int) int {
|
|||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
JavaScript:
|
### JavaScript:
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
//双指针 js中运行速度最快
|
//双指针 js中运行速度最快
|
||||||
var largestRectangleArea = function(heights) {
|
var largestRectangleArea = function(heights) {
|
||||||
@ -581,7 +582,7 @@ var largestRectangleArea = function(heights) {
|
|||||||
return maxArea;
|
return maxArea;
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
TypeScript:
|
### TypeScript:
|
||||||
|
|
||||||
> 暴力法(会超时):
|
> 暴力法(会超时):
|
||||||
|
|
||||||
@ -669,8 +670,7 @@ function largestRectangleArea(heights: number[]): number {
|
|||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Rust:
|
||||||
Rust
|
|
||||||
|
|
||||||
双指针预处理
|
双指针预处理
|
||||||
```rust
|
```rust
|
||||||
@ -730,3 +730,4 @@ impl Solution {
|
|||||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||||
</a>
|
</a>
|
||||||
|
|
||||||
|
@ -37,7 +37,7 @@ nums1 中数字 x 的下一个更大元素是指 x 在 nums2 中对应位
|
|||||||
* nums1和nums2中所有整数 互不相同
|
* nums1和nums2中所有整数 互不相同
|
||||||
* nums1 中的所有整数同样出现在 nums2 中
|
* nums1 中的所有整数同样出现在 nums2 中
|
||||||
|
|
||||||
# 思路
|
## 思路
|
||||||
|
|
||||||
做本题之前,建议先做一下[739. 每日温度](https://programmercarl.com/0739.每日温度.html)
|
做本题之前,建议先做一下[739. 每日温度](https://programmercarl.com/0739.每日温度.html)
|
||||||
|
|
||||||
@ -191,7 +191,8 @@ public:
|
|||||||
建议大家把情况一二三想清楚了,先写出版本一的代码,然后在其基础上在做精简!
|
建议大家把情况一二三想清楚了,先写出版本一的代码,然后在其基础上在做精简!
|
||||||
|
|
||||||
## 其他语言版本
|
## 其他语言版本
|
||||||
Java
|
### Java
|
||||||
|
|
||||||
```java
|
```java
|
||||||
class Solution {
|
class Solution {
|
||||||
public int[] nextGreaterElement(int[] nums1, int[] nums2) {
|
public int[] nextGreaterElement(int[] nums1, int[] nums2) {
|
||||||
@ -248,7 +249,8 @@ class Solution {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
Python3:
|
### Python3
|
||||||
|
|
||||||
```python
|
```python
|
||||||
class Solution:
|
class Solution:
|
||||||
def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]:
|
def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]:
|
||||||
@ -269,7 +271,7 @@ class Solution:
|
|||||||
return result
|
return result
|
||||||
```
|
```
|
||||||
|
|
||||||
Go:
|
### Go
|
||||||
|
|
||||||
> 未精简版本
|
> 未精简版本
|
||||||
```go
|
```go
|
||||||
@ -335,7 +337,7 @@ func nextGreaterElement(nums1 []int, nums2 []int) []int {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
JavaScript:
|
### JavaScript
|
||||||
|
|
||||||
```JS
|
```JS
|
||||||
var nextGreaterElement = function (nums1, nums2) {
|
var nextGreaterElement = function (nums1, nums2) {
|
||||||
@ -358,7 +360,7 @@ var nextGreaterElement = function (nums1, nums2) {
|
|||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
TypeScript:
|
### TypeScript
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
function nextGreaterElement(nums1: number[], nums2: number[]): number[] {
|
function nextGreaterElement(nums1: number[], nums2: number[]): number[] {
|
||||||
@ -387,7 +389,7 @@ function nextGreaterElement(nums1: number[], nums2: number[]): number[] {
|
|||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
Rust
|
### Rust
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
impl Solution {
|
impl Solution {
|
||||||
@ -419,3 +421,4 @@ impl Solution {
|
|||||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||||
</a>
|
</a>
|
||||||
|
|
||||||
|
@ -22,7 +22,7 @@
|
|||||||
* -10^9 <= nums[i] <= 10^9
|
* -10^9 <= nums[i] <= 10^9
|
||||||
|
|
||||||
|
|
||||||
# 思路
|
## 思路
|
||||||
|
|
||||||
做本题之前建议先做[739. 每日温度](https://programmercarl.com/0739.每日温度.html) 和 [496.下一个更大元素 I](https://programmercarl.com/0496.下一个更大元素I.html)。
|
做本题之前建议先做[739. 每日温度](https://programmercarl.com/0739.每日温度.html) 和 [496.下一个更大元素 I](https://programmercarl.com/0496.下一个更大元素I.html)。
|
||||||
|
|
||||||
@ -138,7 +138,8 @@ public:
|
|||||||
|
|
||||||
## 其他语言版本
|
## 其他语言版本
|
||||||
|
|
||||||
Java:
|
### Java:
|
||||||
|
|
||||||
```Java
|
```Java
|
||||||
class Solution {
|
class Solution {
|
||||||
public int[] nextGreaterElements(int[] nums) {
|
public int[] nextGreaterElements(int[] nums) {
|
||||||
@ -162,7 +163,8 @@ class Solution {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Python:
|
### Python:
|
||||||
|
|
||||||
```python
|
```python
|
||||||
# 方法 1:
|
# 方法 1:
|
||||||
class Solution:
|
class Solution:
|
||||||
@ -196,7 +198,8 @@ class Solution:
|
|||||||
stack.append(i)
|
stack.append(i)
|
||||||
return ans
|
return ans
|
||||||
```
|
```
|
||||||
Go:
|
### Go:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func nextGreaterElements(nums []int) []int {
|
func nextGreaterElements(nums []int) []int {
|
||||||
length := len(nums)
|
length := len(nums)
|
||||||
@ -218,7 +221,7 @@ func nextGreaterElements(nums []int) []int {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
JavaScript:
|
### JavaScript:
|
||||||
|
|
||||||
```JS
|
```JS
|
||||||
/**
|
/**
|
||||||
@ -242,7 +245,7 @@ var nextGreaterElements = function (nums) {
|
|||||||
return res;
|
return res;
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
TypeScript:
|
### TypeScript:
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
function nextGreaterElements(nums: number[]): number[] {
|
function nextGreaterElements(nums: number[]): number[] {
|
||||||
@ -266,7 +269,8 @@ function nextGreaterElements(nums: number[]): number[] {
|
|||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
Rust
|
### Rust:
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
impl Solution {
|
impl Solution {
|
||||||
pub fn next_greater_elements(nums: Vec<i32>) -> Vec<i32> {
|
pub fn next_greater_elements(nums: Vec<i32>) -> Vec<i32> {
|
||||||
@ -290,3 +294,4 @@ impl Solution {
|
|||||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||||
</a>
|
</a>
|
||||||
|
|
||||||
|
@ -211,8 +211,7 @@ public:
|
|||||||
|
|
||||||
## 其他语言版本
|
## 其他语言版本
|
||||||
|
|
||||||
|
### Java:
|
||||||
Java:
|
|
||||||
|
|
||||||
```java
|
```java
|
||||||
class Solution {
|
class Solution {
|
||||||
@ -270,7 +269,8 @@ class Solution {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Python:
|
### Python:
|
||||||
|
|
||||||
> 未精简版本
|
> 未精简版本
|
||||||
|
|
||||||
```python
|
```python
|
||||||
@ -307,7 +307,7 @@ class Solution:
|
|||||||
return answer
|
return answer
|
||||||
```
|
```
|
||||||
|
|
||||||
Go:
|
### Go:
|
||||||
|
|
||||||
> 暴力法
|
> 暴力法
|
||||||
|
|
||||||
@ -384,7 +384,7 @@ func dailyTemperatures(num []int) []int {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
JavaScript:
|
### JavaScript:
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
// 版本一
|
// 版本一
|
||||||
@ -429,7 +429,7 @@ var dailyTemperatures = function(temperatures) {
|
|||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
TypeScript:
|
### TypeScript:
|
||||||
|
|
||||||
> 精简版:
|
> 精简版:
|
||||||
|
|
||||||
@ -455,7 +455,7 @@ function dailyTemperatures(temperatures: number[]): number[] {
|
|||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
Rust:
|
### Rust:
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
impl Solution {
|
impl Solution {
|
||||||
@ -482,3 +482,4 @@ impl Solution {
|
|||||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||||
</a>
|
</a>
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user