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
[LeetCode] 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 next in the arr...
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 next in the array, which means you could search ci...
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 next in the array, which means you could search ci...
For number 2 in the first array, there is no next greater number for it in the second array, so output -1. My solution: class Solution { public int[] nextGreaterElement(int[] nums1, int[] nums2) { int[] res = new int[nums1.length]; ...
public class Solution { public int nextGreaterElement(int n) { char[] a = ("" + n).toCharArray(); int i = a.length - 2; while (i >= 0 && a[i + 1] <= a[i]) { i--; // 从右往左,找到第一个符合条件的位置,这个位置之后的排列都是降序的。 } if (i < 0) return -1...
return ans if ans < 2 ** 31 else -1 C++ class Solution { public: int nextGreaterElement(int n) { auto nums = to_string(n); int i = (int) nums.length() - 2; while (i >= 0 && nums[i] >= nums[i + 1]) { i--; ...
importjava.util.Arrays;publicclassSolution{publicint[]nextGreaterElement(int[]nums1,int[]nums2){int len1=nums1.length;int len2=nums2.length;int[]res=newint[len1];if(len1<1){returnres;}for(int i=0;i<len1;i++){int curVal=nums1[i];int j=0;while(j<len2&&nums2[j]!=curVal)...
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,...
作者是 在线疯狂 发布于 2017年2月5日 在LeetCode. 题目描述: LeetCode 496. Next Greater Element I 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...