From 47b6ed62d5952aa329729afba250dcd39f1da8e7 Mon Sep 17 00:00:00 2001 From: CyC2018 <1029579233@qq.com> Date: Fri, 8 Jun 2018 16:13:27 +0800 Subject: [PATCH] auto commit --- notes/Leetcode 题解.md | 52 +++++++++++++++--------------------------- 1 file changed, 19 insertions(+), 33 deletions(-) diff --git a/notes/Leetcode 题解.md b/notes/Leetcode 题解.md index 73395093..92123a2d 100644 --- a/notes/Leetcode 题解.md +++ b/notes/Leetcode 题解.md @@ -3992,48 +3992,34 @@ public int[] dailyTemperatures(int[] temperatures) { } ``` -**在另一个数组中比当前元素大的下一个元素** - -[496. Next Greater Element I (Easy)](https://leetcode.com/problems/next-greater-element-i/description/) - -```html -Input: nums1 = [4,1,2], nums2 = [1,3,4,2]. -Output: [-1,3,-1] -``` - -```java -public int[] nextGreaterElement(int[] nums1, int[] nums2) { - Map map = new HashMap<>(); - Stack stack = new Stack<>(); - for (int num : nums2) { - while (!stack.isEmpty() && num > stack.peek()) { - map.put(stack.pop(), num); - } - stack.add(num); - } - int[] ret = new int[nums1.length]; - for (int i = 0; i < nums1.length; i++) { - if (map.containsKey(nums1[i])) ret[i] = map.get(nums1[i]); - else ret[i] = -1; - } - return ret; -} -``` - **循环数组中比当前元素大的下一个元素** [503. Next Greater Element II (Medium)](https://leetcode.com/problems/next-greater-element-ii/description/) +```text +Input: [1,2,1] +Output: [2,-1,2] +Explanation: The first 1's next greater number is 2; +The number 2 can't find next greater number; +The second 1's next greater number needs to search circularly, which is also 2. +``` + +与 739. Daily Temperatures (Medium) 不同的是,数组是循环数组,并且最后要求的不是距离而是下一个元素。 + ```java public int[] nextGreaterElements(int[] nums) { - int n = nums.length, next[] = new int[n]; + int n = nums.length; + int[] next = new int[n]; Arrays.fill(next, -1); - Stack stack = new Stack<>(); + Stack pre = new Stack<>(); for (int i = 0; i < n * 2; i++) { int num = nums[i % n]; - while (!stack.isEmpty() && nums[stack.peek()] < num) - next[stack.pop()] = num; - if (i < n) stack.push(i); + while (!pre.isEmpty() && nums[pre.peek()] < num) { + next[pre.pop()] = num; + } + if (i < n){ + pre.push(i); + } } return next; }