From 6f7ffaa08755564c6a5714445e8861386fe3869f Mon Sep 17 00:00:00 2001 From: caozheng0401 <1546376420@qq.com> Date: Tue, 10 Aug 2021 15:31:10 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20496.=E4=B8=8B=E4=B8=80?= =?UTF-8?q?=E4=B8=AA=E6=9B=B4=E5=A4=A7=E5=85=83=E7=B4=A0=20Java=E6=96=B9?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0496.下一个更大元素I.md | 30 +++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/problems/0496.下一个更大元素I.md b/problems/0496.下一个更大元素I.md index 0404e434..566a09b1 100644 --- a/problems/0496.下一个更大元素I.md +++ b/problems/0496.下一个更大元素I.md @@ -186,7 +186,37 @@ public: 建议大家把情况一二三想清楚了,先写出版本一的代码,然后在其基础上在做精简! ## 其他语言版本 +Java +```java +class Solution { + public int[] nextGreaterElement(int[] nums1, int[] nums2) { + Stack temp = new Stack<>(); + int[] res = new int[nums1.length]; + Arrays.fill(res,-1); + HashMap hashMap = new HashMap<>(); + for (int i = 0 ; i< nums1.length ; i++){ + hashMap.put(nums1[i],i); + } + temp.add(0); + for (int i = 1; i < nums2.length; i++) { + if (nums2[i] <= nums2[temp.peek()]) { + temp.add(i); + } else { + while (!temp.isEmpty() && nums2[temp.peek()] < nums2[i]) { + if (hashMap.containsKey(nums2[temp.peek()])){ + Integer index = hashMap.get(nums2[temp.peek()]); + res[index] = nums2[i]; + } + temp.pop(); + } + temp.add(i); + } + } + return res; + } +} +``` Python: ```python3 class Solution: From 5f6d5a0731968ee55db47b61c1d5654751df10a0 Mon Sep 17 00:00:00 2001 From: caozheng0401 <1546376420@qq.com> Date: Tue, 10 Aug 2021 15:52:27 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20503.=E4=B8=8B=E4=B8=80?= =?UTF-8?q?=E4=B8=AA=E6=9B=B4=E5=A4=A7=E5=85=83=E7=B4=A0II=20Java=E6=96=B9?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0503.下一个更大元素II.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/problems/0503.下一个更大元素II.md b/problems/0503.下一个更大元素II.md index 34ade48e..6e351f27 100644 --- a/problems/0503.下一个更大元素II.md +++ b/problems/0503.下一个更大元素II.md @@ -95,7 +95,23 @@ public: ## 其他语言版本 Java: +```java +class Solution { + public int[] nextGreaterElements(int[] nums) { + int[] res = new int[nums.length]; + Arrays.fill(res, -1); + Stack temp = new Stack<>(); + for (int i = 1; i < nums.length * 2; i++) { + while (!temp.isEmpty() && nums[temp.peek()] < nums[i % nums.length]) { + res[temp.pop()] = nums[i % nums.length]; + } + temp.add(i % nums.length); + } + return res; + } +} +``` Python: ```python3 class Solution: