For number 4 in the first array, you cannot find the next greater number for it in the second array, so output -1. For number 1 in the first array, the next greater number for it in the second array is 3. For number 2 in the first array, there is no next greater number for it...
Can you solve this real interview question? Next Greater Element I - The next greater element of some element x in an array is the first greater element that is to the right of x in the same array. You are given two distinct 0-indexed integer arrays num
publicint[] nextGreaterElement(int[] findNums,int[] nums) {int[] result =newint[findNums.length];intn=0;for(inti=0; i < findNums.length; i++) {for(intj=0; j < nums.length; j++) {if(nums[j] == findNums[i]) {booleanisFind=false;for(intk=j +1; k < nums.length; k++...
[LeetCode] 496. Next Greater Element I 题目内容 https://leetcode-cn.com/problems/next-greater-element-i/ 给定两个没有重复元素的数组 nums1 和 nums2 ,其中nums1 是 nums2 的子集。找到 nums1 中每个元素在 nums2 中的下一个比其大的值。 ......
Output: [-1,3,-1] 2、代码实现 public class Solution { public int[] nextGreaterElement(int[] findNums, int[] nums) { if (findNums == null || nums == null) { return null; } int findLength = findNums.length; int numsLength = nums.length; ...
## LeetCode 496fromtypingimportListclassSolution:defnextGreaterElement(self,nums1:List[int],nums2:List[int])->List[int]:res={}stack=[]foriinnums2[::-1]:## 从 num2 最右边的元素开始遍历whilestackandi>=stack[-1]:## 如果栈非空,且该元素比栈顶(列表最右边)元素大stack.pop()## 清空这个...
class Solution { public int[] nextGreaterElement(int[] nums1, int[] nums2) { Map<Integer, Integer> indexs = new HashMap<>(); int[] result = new int[nums1.length]; for (int i = 0; i < nums2.length; i++) indexs.put(nums2[i], i); ...
class Solution(object): def nextGreaterElement(self,nums1,nums2): stk=[] #单调栈 hashTable={} #哈希表 for i in range(len(nums2)-1,-1,-1): #反向遍历 print(i) cur=nums2[i] while stk!=[] and stk[-1]<=cur: #关键 stk.pop() ...
简介:LeetCode之Next Greater Element I 1、题目 You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of nums2. Find all the next greater numbers for nums1's elements in the corresponding places of nums2.The Next Greater Number of a number x in...
https://discuss.leetcode.com/topic/77916/java-10-lines-linear-time-complexity-o-n-with-explanation 栈stack维护nums的递减子集,记nums的当前元素为n,栈顶元素为top 重复弹出栈顶,直到stack为空,或者top大于n为止 将所有被弹出元素的next greater element置为n Python代码: class Solution(object): def next...