496. 下一个更大元素 I(简单题)

mac2026-06-12  16

题目描述: 给定两个没有重复元素的数组 nums1 和 nums2 ,其中nums1 是 nums2 的子集。找到 nums1 中每个元素在 nums2 中的下一个比其大的值。

nums1 中数字 x 的下一个更大元素是指 x 在 nums2 中对应位置的右边的第一个比 x 大的元素。如果不存在,对应位置输出-1。

示例 1:

输入: nums1 = [4,1,2], nums2 = [1,3,4,2]. 输出: [-1,3,-1] 解释: 对于num1中的数字4,你无法在第二个数组中找到下一个更大的数字,因此输出 -1。 对于num1中的数字1,第二个数组中数字1右边的下一个较大数字是 3。 对于num1中的数字2,第二个数组中没有下一个更大的数字,因此输出 -1。

示例 2:

输入: nums1 = [2,4], nums2 = [1,2,3,4]. 输出: [3,-1] 解释: 对于num1中的数字2,第二个数组中的下一个较大数字是3。 对于num1中的数字4,第二个数组中没有下一个更大的数字,因此输出 -1。

注意:

nums1和nums2中所有元素是唯一的。 nums1和nums2 的数组大小都不超过1000。

来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/next-greater-element-i 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 暴力破解:

class Solution { public int[] nextGreaterElement(int[] nums1, int[] nums2) { for (int i = 0; i < nums1.length; i++) { int temp = Integer.MAX_VALUE; boolean judge = true; for (int j = 0; j < nums2.length; j++) { if(nums2[j] == nums1[i]) temp = nums2[j]; if(nums2[j] > temp) { nums1[i] = nums2[j]; judge = false; break; } } if(judge){ nums1[i] = -1; } } return nums1; } }

单调栈解法,来源题解:

class Solution { public int[] nextGreaterElement(int[] nums1, int[] nums2) { int[] temp = new int[nums2.length]; Stack<Integer> st = new Stack<>(); for (int i = nums2.length - 1; i >= 0; i--) { while (!st.empty() && nums2[i] < st.peek()) { st.pop(); } temp[i] = st.empty() ? -1 : st.peek(); st.push(nums2[i]); } for (int i = 0; i < nums1.length; i++) { for (int j = 0; j < nums2.length; j++) { if (nums1[i] == nums2[j]) { nums1[i] = temp[j]; break; } } } return nums1; } }

单调栈+哈希表:

class Solution { public int[] nextGreaterElement(int[] nums1, int[] nums2) { int[] temp = new int[nums2.length]; Stack<Integer> st = new Stack<>(); HashMap<Integer,Integer> map = new HashMap<>(); for (int i = nums2.length - 1; i >= 0; i--) { while (!st.empty() && nums2[i] > st.peek()) { st.pop(); } temp[i] = st.empty() ? -1 : st.peek(); st.push(nums2[i]); map.put(nums2[i],temp[i]); } for (int i = 0; i < nums1.length; i++) { nums1[i] = map.get(nums1[i]); } return nums1; } }
最新回复(0)