From 55c78be1280054615f23c9e5859d139a7f840705 Mon Sep 17 00:00:00 2001 From: dmzlingyin Date: Fri, 29 Apr 2022 23:38:34 +0800 Subject: [PATCH 1/4] =?UTF-8?q?update=20(0739.=E6=AF=8F=E6=97=A5=E6=B8=A9?= =?UTF-8?q?=E5=BA=A6.md):=20python=E4=BB=A3=E7=A0=81=E9=AB=98=E4=BA=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0739.每日温度.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/0739.每日温度.md b/problems/0739.每日温度.md index 710f5eb6..7deab0a3 100644 --- a/problems/0739.每日温度.md +++ b/problems/0739.每日温度.md @@ -233,7 +233,7 @@ class Solution { } ``` Python: -``` Python3 +```python class Solution: def dailyTemperatures(self, temperatures: List[int]) -> List[int]: answer = [0]*len(temperatures) From d0a79760b65a1078284b036be6dcf8fa8eec5076 Mon Sep 17 00:00:00 2001 From: dmzlingyin Date: Fri, 29 Apr 2022 23:41:26 +0800 Subject: [PATCH 2/4] =?UTF-8?q?update=20(0739.=E6=AF=8F=E6=97=A5=E6=B8=A9?= =?UTF-8?q?=E5=BA=A6.md):=20=E8=AF=AD=E8=A8=80=E8=A1=A8=E8=BF=B0=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0739.每日温度.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/0739.每日温度.md b/problems/0739.每日温度.md index 7deab0a3..f0f782d2 100644 --- a/problems/0739.每日温度.md +++ b/problems/0739.每日温度.md @@ -34,7 +34,7 @@ 那么单调栈的原理是什么呢?为什么时间复杂度是O(n)就可以找到每一个元素的右边第一个比它大的元素位置呢? -单调栈的本质是空间换时间,因为在遍历的过程中需要用一个栈来记录右边第一个比当前元素的元素,优点是只需要遍历一次。 +单调栈的本质是空间换时间,因为在遍历的过程中需要用一个栈来记录右边第一个比当前元素大的元素,优点是只需要遍历一次。 在使用单调栈的时候首先要明确如下几点: From 636550d44c4ac65521865cca00b1c3c8b9f7c756 Mon Sep 17 00:00:00 2001 From: dmzlingyin Date: Fri, 29 Apr 2022 23:50:31 +0800 Subject: [PATCH 3/4] =?UTF-8?q?update=20(0739.=E6=AF=8F=E6=97=A5=E6=B8=A9?= =?UTF-8?q?=E5=BA=A6.md):=20=E5=A2=9E=E5=8A=A0=E6=9C=AA=E7=B2=BE=E7=AE=80?= =?UTF-8?q?=E7=89=88=E6=9C=ACGo=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0739.每日温度.md | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/problems/0739.每日温度.md b/problems/0739.每日温度.md index f0f782d2..206bebd2 100644 --- a/problems/0739.每日温度.md +++ b/problems/0739.每日温度.md @@ -277,8 +277,36 @@ func dailyTemperatures(t []int) []int { } ``` -> 单调栈法 +> 单调栈法(未精简版本) +```go +func dailyTemperatures(temperatures []int) []int { + res := make([]int, len(temperatures)) + // 初始化栈顶元素为第一个下标索引0 + stack := []int{0} + + for i := 1; i < len(temperatures); i++ { + top := stack[len(stack)-1] + if temperatures[i] < temperatures[top] { + stack = append(stack, i) + } else if temperatures[i] == temperatures[top] { + stack = append(stack, i) + } else { + for len(stack) != 0 && temperatures[i] > temperatures[top] { + res[top] = i - top + stack = stack[:len(stack)-1] + if len(stack) != 0 { + top = stack[len(stack)-1] + } + } + stack = append(stack, i) + } + } + return res +} +``` + +> 单调栈法(精简版本) ```go // 单调递减栈 func dailyTemperatures(num []int) []int { From 49ec574821f687ea8703c61245f815bf2dc76139 Mon Sep 17 00:00:00 2001 From: dmzlingyin Date: Sat, 30 Apr 2022 11:04:17 +0800 Subject: [PATCH 4/4] =?UTF-8?q?update=20(0496.=E4=B8=8B=E4=B8=80=E4=B8=AA?= =?UTF-8?q?=E6=9B=B4=E5=A4=A7=E5=85=83=E7=B4=A0I):=20=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E6=9C=AA=E7=B2=BE=E7=AE=80Go=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0496.下一个更大元素I.md | 33 +++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/problems/0496.下一个更大元素I.md b/problems/0496.下一个更大元素I.md index f9dfa308..02339677 100644 --- a/problems/0496.下一个更大元素I.md +++ b/problems/0496.下一个更大元素I.md @@ -244,6 +244,39 @@ class Solution: ``` Go: + +> 未精简版本 +```go +func nextGreaterElement(nums1 []int, nums2 []int) []int { + res := make([]int, len(nums1)) + for i := range res { res[i] = -1 } + m := make(map[int]int, len(nums1)) + for k, v := range nums1 { m[v] = k } + + stack := []int{0} + for i := 1; i < len(nums2); i++ { + top := stack[len(stack)-1] + if nums2[i] < nums2[top] { + stack = append(stack, i) + } else if nums2[i] == nums2[top] { + stack = append(stack, i) + } else { + for len(stack) != 0 && nums2[i] > nums2[top] { + if v, ok := m[nums2[top]]; ok { + res[v] = nums2[i] + } + stack = stack[:len(stack)-1] + if len(stack) != 0 { + top = stack[len(stack)-1] + } + } + stack = append(stack, i) + } + } + return res +} +``` +> 精简版本 ```go func nextGreaterElement(nums1 []int, nums2 []int) []int { res := make([]int, len(nums1))