diff --git a/problems/0042.接雨水.md b/problems/0042.接雨水.md index 833a7613..4f0682ba 100644 --- a/problems/0042.接雨水.md +++ b/problems/0042.接雨水.md @@ -29,7 +29,7 @@ * 输出:9 -# 思路 +## 思路 接雨水问题在面试中还是常见题目的,有必要好好讲一讲。 @@ -39,7 +39,7 @@ * 动态规划 * 单调栈 -## 暴力解法 +### 暴力解法 本题暴力解法也是也是使用双指针。 @@ -137,7 +137,7 @@ public: 力扣后面修改了后台测试数据,所以以上暴力解法超时了。 -## 双指针优化 +### 双指针优化 在暴力解法中,我们可以看到只要记录左边柱子的最高高度 和 右边柱子的最高高度,就可以计算当前位置的雨水面积,这就是通过列来计算。 @@ -184,7 +184,7 @@ public: }; ``` -## 单调栈解法 +### 单调栈解法 关于单调栈的理论基础,单调栈适合解决什么问题,单调栈的工作过程,大家可以先看这题讲解 [739. 每日温度](https://programmercarl.com/0739.每日温度.html)。 @@ -194,7 +194,7 @@ public: 而接雨水这道题目,我们正需要寻找一个元素,右边最大元素以及左边最大元素,来计算雨水面积。 -### 准备工作 +#### 准备工作 那么本题使用单调栈有如下几个问题: @@ -248,7 +248,7 @@ stack st; // 存着下标,计算的时候用下标对应的柱子高度 明确了如上几点,我们再来看处理逻辑。 -### 单调栈处理逻辑 +#### 单调栈处理逻辑 以下操作过程其实和 [739. 每日温度](https://programmercarl.com/0739.每日温度.html) 也是一样的,建议先做 [739. 每日温度](https://programmercarl.com/0739.每日温度.html)。 @@ -596,7 +596,7 @@ class Solution: ``` -### Go +### Go: ```go 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(1) - -Rust +### Rust: 双指针 @@ -980,3 +979,4 @@ impl Solution { + diff --git a/problems/0084.柱状图中最大的矩形.md b/problems/0084.柱状图中最大的矩形.md index bc82a860..4d949941 100644 --- a/problems/0084.柱状图中最大的矩形.md +++ b/problems/0084.柱状图中最大的矩形.md @@ -20,7 +20,7 @@ * 1 <= heights.length <=10^5 * 0 <= heights[i] <= 10^4 -# 思路 +## 思路 本题和[42. 接雨水](https://programmercarl.com/0042.接雨水.html),是遥相呼应的两道题目,建议都要仔细做一做,原理上有很多相同的地方,但细节上又有差异,更可以加深对单调栈的理解! @@ -28,7 +28,7 @@ 我们先来看一下暴力解法的解法: -## 暴力解法 +### 暴力解法 ```CPP class Solution { @@ -55,7 +55,7 @@ public: 如上代码并不能通过leetcode,超时了,因为时间复杂度是$O(n^2)$。 -## 双指针解法 +### 双指针解法 本题双指针的写法整体思路和[42. 接雨水](https://programmercarl.com/0042.接雨水.html)是一致的,但要比[42. 接雨水](https://programmercarl.com/0042.接雨水.html)难一些。 @@ -98,7 +98,7 @@ public: }; ``` -## 单调栈 +### 单调栈 本地单调栈的解法和接雨水的题目是遥相呼应的。 @@ -169,7 +169,7 @@ public: } }; -``` +``` 细心的录友会发现,我在 height数组上后,都加了一个元素0, 为什么这么做呢? @@ -229,7 +229,7 @@ public: ## 其他语言版本 -Java: +### Java: 暴力解法: ```java @@ -335,7 +335,7 @@ class Solution { } ``` -Python3: +### Python3: ```python @@ -468,7 +468,7 @@ class Solution: ``` -Go: +### Go: > 单调栈 @@ -506,7 +506,8 @@ func largestRectangleArea(heights []int) int { ``` -JavaScript: +### JavaScript: + ```javascript //双指针 js中运行速度最快 var largestRectangleArea = function(heights) { @@ -581,7 +582,7 @@ var largestRectangleArea = function(heights) { return maxArea; }; ``` -TypeScript: +### TypeScript: > 暴力法(会超时): @@ -669,8 +670,7 @@ function largestRectangleArea(heights: number[]): number { }; ``` - -Rust +### Rust: 双指针预处理 ```rust @@ -730,3 +730,4 @@ impl Solution { + diff --git a/problems/0496.下一个更大元素I.md b/problems/0496.下一个更大元素I.md index 411a47df..6bcafafb 100644 --- a/problems/0496.下一个更大元素I.md +++ b/problems/0496.下一个更大元素I.md @@ -37,7 +37,7 @@ nums1 中数字 x 的下一个更大元素是指 x 在 nums2 中对应位 * nums1和nums2中所有整数 互不相同 * nums1 中的所有整数同样出现在 nums2 中 -# 思路 +## 思路 做本题之前,建议先做一下[739. 每日温度](https://programmercarl.com/0739.每日温度.html) @@ -191,7 +191,8 @@ public: 建议大家把情况一二三想清楚了,先写出版本一的代码,然后在其基础上在做精简! ## 其他语言版本 -Java +### Java + ```java class Solution { public int[] nextGreaterElement(int[] nums1, int[] nums2) { @@ -248,7 +249,8 @@ class Solution { } } ``` -Python3: +### Python3 + ```python class Solution: def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]: @@ -269,7 +271,7 @@ class Solution: return result ``` -Go: +### Go > 未精简版本 ```go @@ -335,7 +337,7 @@ func nextGreaterElement(nums1 []int, nums2 []int) []int { } ``` -JavaScript: +### JavaScript ```JS var nextGreaterElement = function (nums1, nums2) { @@ -358,7 +360,7 @@ var nextGreaterElement = function (nums1, nums2) { }; ``` -TypeScript: +### TypeScript ```typescript function nextGreaterElement(nums1: number[], nums2: number[]): number[] { @@ -387,7 +389,7 @@ function nextGreaterElement(nums1: number[], nums2: number[]): number[] { }; ``` -Rust +### Rust ```rust impl Solution { @@ -419,3 +421,4 @@ impl Solution { + diff --git a/problems/0503.下一个更大元素II.md b/problems/0503.下一个更大元素II.md index a090f32c..023e4d7e 100644 --- a/problems/0503.下一个更大元素II.md +++ b/problems/0503.下一个更大元素II.md @@ -22,7 +22,7 @@ * -10^9 <= nums[i] <= 10^9 -# 思路 +## 思路 做本题之前建议先做[739. 每日温度](https://programmercarl.com/0739.每日温度.html) 和 [496.下一个更大元素 I](https://programmercarl.com/0496.下一个更大元素I.html)。 @@ -138,7 +138,8 @@ public: ## 其他语言版本 -Java: +### Java: + ```Java class Solution { public int[] nextGreaterElements(int[] nums) { @@ -162,7 +163,8 @@ class Solution { } ``` -Python: +### Python: + ```python # 方法 1: class Solution: @@ -196,7 +198,8 @@ class Solution: stack.append(i) return ans ``` -Go: +### Go: + ```go func nextGreaterElements(nums []int) []int { length := len(nums) @@ -218,7 +221,7 @@ func nextGreaterElements(nums []int) []int { } ``` -JavaScript: +### JavaScript: ```JS /** @@ -242,7 +245,7 @@ var nextGreaterElements = function (nums) { return res; }; ``` -TypeScript: +### TypeScript: ```typescript function nextGreaterElements(nums: number[]): number[] { @@ -266,7 +269,8 @@ function nextGreaterElements(nums: number[]): number[] { }; ``` -Rust +### Rust: + ```rust impl Solution { pub fn next_greater_elements(nums: Vec) -> Vec { @@ -290,3 +294,4 @@ impl Solution { + diff --git a/problems/0739.每日温度.md b/problems/0739.每日温度.md index d2da3737..fc1a8063 100644 --- a/problems/0739.每日温度.md +++ b/problems/0739.每日温度.md @@ -211,8 +211,7 @@ public: ## 其他语言版本 - -Java: +### Java: ```java class Solution { @@ -270,7 +269,8 @@ class Solution { } ``` -Python: +### Python: + > 未精简版本 ```python @@ -307,7 +307,7 @@ class Solution: return answer ``` -Go: +### Go: > 暴力法 @@ -384,7 +384,7 @@ func dailyTemperatures(num []int) []int { } ``` -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 impl Solution { @@ -482,3 +482,4 @@ impl Solution { +