2在num2中没有右侧数字了 结果是-1 我的python代码: 1classSolution(object):2defnextGreaterElement(self, findNums, nums):3"""4:type findNums: List[int]5:type nums: List[int]6:rtype: List[int]7"""8res =[]9foriinfindNums:10index =nums.index(i)11index2 = -112forjinrange(index + ...
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...
1)常规解法(python3) 思路:首先对nums1中每个元素先找到在nums2中的下标,然后再看后面的元素有无大于nums1该元素的结果。运行效率很低,多个循环: 1classSolution:2defnextGreaterElement(self, nums1, nums2):3stack =[]4foriinnums1:56forjinrange(len(nums2)):7ifj==(len(nums2)-1):8stack.append(-...
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() if stk: hashTable[cur]=stk[-1] else: hashTa...
【Python-转码刷题】LeetCode 496E 下一个最大元素 Next Greater Element 1 【单调栈+字典】 新手村100题汇总:王几行xing:【Python-转码刷题】LeetCode 力扣新手村100题,及刷题顺序 利用额外的单调栈来计数。所以,这也是一个典型的用空间换时间算法。
Find all the next greater numbers for nums1's elements in the corresponding places of nu...leetcode 496. Next Greater Element I ...[LeetCode] 496. Next Greater Element I 题目内容 https://leetcode-cn.com/problems/next-greater-element-i/ 给定两个没有重复元素的数组 nums1 和 nums2 ,...
496. Next Greater Element I (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 ...
像这样的题,我一开始想都没想,针对nums1的每个元素,循环遍历找nums2中的next greater。很明显它的复杂度就是O(nm)O(nm),代码如下: 代码语言:javascript 代码运行次数:0 运行 AI代码解释 publicint[]nextGreaterElement(int[]findNums,int[]nums){int[]ans=newint[findNums.length];for(int i=0;i<findNu...
publicintnextGreaterElement(int n){String value=String.valueOf(n);char[]digits=value.toCharArray();int i=digits.length-1;//找到小于右侧任意值的第一个正整数while(i>0){if(digits[i-1]<digits[i]){break;}i--;}if(i==0){return-1;}//找到该整数右侧大于该整数的最小整数int maxIndex=i,...
Add first element to the stack. Iterate through the element of the array. If the stack is empty, add the current element to the stack. While the current element is greater than the top element of the stack. Print the top element with the next greater element as current element. Pop the...