public class Solution { public int NextGreaterElement(int n) { char[] nums = n.ToString().ToCharArray(); int i = nums.Length - 2; while (i >= 0 && nums[i] >= nums[i + 1]) { i--; } if (i < 0) { return -1; } int j = nums.Length - 1; while (j >= 0 && nums...
想到了用哈希表存这个数的位置,但是没有想到可以直接用哈希表存next great,用栈存还没找到的数,没遍历一个数就考察栈中的元素小,小的话,这个数就是栈中数的next great,栈中的数肯定是下大上小。 publicint[] nextGreaterElement(int[] nums1,int[] nums2) {/*通过map建立当前元素和其next great的映射 ...
使用两层循环,先找到nums1的元素在nums2中的对应元素,然后从nums2中找到的元素的下一位往右开始判断,找到比当前元素大的元素,找得到就将其作为结果数组的元素添加进去,否则就将-1添加进结果数组。 publicint[]nextGreaterElement(int[] nums1,int[] nums2){int[] result =newint[nums1.length];for(inti=0;...
publicclassSolution{publicint[]nextGreaterElement(int[]findNums,int[]nums){Stack<Integer>stack=newStack<>();HashMap<Integer,Integer>map=newHashMap<>();int[]res=newint[findNums.length];for(inti=0;i<nums.length;i++){while(!stack.empty()&&nums[i]>stack.peek())map.put(stack.pop(),num...
LeetCode题目:503. Next Greater Element II Given acircular array (the next element of the last element is the first element of the array),print the Next Greater Number for every element. The Next Greater Number of a number x is the first greater number to its traversing-order next in the...
503. Next Greater Element II # 题目 # Given a circular array (the next element of the last element is the first element of the array), print the Next Greater Number for every element. The Next Greater Number of a number x is the first greater number to
int* nextGreaterElement(int* nums1, int nums1Size, int* nums2, int nums2Size, int* returnSize){ Hash *hash = InitHash(); int *res = (int*)malloc(nums1Size*sizeof(int)); int i, j;for (i = 0; i < nums2Size; i++) { ...
[leetcode] 503. Next Greater Element II Description Given a circular array (the next element of the last element is the first element of the array), print the Next Greater Number for every element. The Next Greater Number of a number x is the first greater number to its traversing-order...
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,...
LeetCode笔记:503. Next Greater Element II 问题: Given a circular array (the next element of the last element is the first element of the array), print the Next Greater Number for every element. The Next Greater Number of a number x is the first greater number to its traversing-order ...