1classSolution {2intmintoplus =Integer.MAX_VALUE;3publicintnextGreaterElement(intn) {4String s =String.valueOf(n);5char[] array =s.toCharArray();6perm(array,n,0,array.length-1);7if( mintoplus == Integer.MAX_VALUE )return-1;8returnn+mintoplus;9}10privatevoidperm(char[] a,intn ,...
想到了用哈希表存这个数的位置,但是没有想到可以直接用哈希表存next great,用栈存还没找到的数,没遍历一个数就考察栈中的元素小,小的话,这个数就是栈中数的next great,栈中的数肯定是下大上小。 publicint[] nextGreaterElement(int[] nums1,int[] nums2) {/*通过map建立当前元素和其next great的映射 ...
AI代码解释 classSolution:defsecondGreaterElement(self,nums:List[int])->List[int]:s=[(x,-i)fori,xinenumerate(nums)]s=sorted(s,reverse=True)cnums=[]res=[-1for_innums]forx,iins:i=-i idx=bisect.bisect_left(cnums,i)ifidx+1<len(cnums):res[i]=nums[cnums[idx+1]]bisect.insort(c...
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
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,...
Can you solve this real interview question? Next Greater Element II - Given a circular integer array nums (i.e., the next element of nums[nums.length - 1] is nums[0]), return the next greater number for every element in nums. The next greater number of
Leetcode496. Next Greater Element I 这个题,开始我是想用find和upper_bound两个函数解决,但是 upper_bound并不对。 upper_bound返回的是插入的数字的上界。 如一个数组number序列1,2,2,4.upper_bound(2)后,返回的位置是3(下标)也就是4所在的位置。
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 nums1 is the first greater number to its right in nums2. If it does not exist, output -1 for this number. Example 1: Input: nums1 = [4,1,2]...
nums1=[4,1,2] nums2=[1,3,4,2] sl=Solution() print(sl.nextGreaterElement(nums1,nums2)) 代码至此结束 此栏目预测共享自学之乐,共勉求知之友,共塑网站和谐好学的形象。 欢迎大家在评论区发表合理的意见和指正。 如果觉得该栏目对您有帮助,望不吝点赞收藏。