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,j...
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...
下面这种解法博主感觉有些耍赖了,用到了STL的内置函数next_permutation,该数字实现的就是这样一个功能,找下一个全排序,刚好比当前的值大,贴上来权当好玩: 解法二: classSolution {public:intnextGreaterElement(intn) {stringstr =to_string(n); next_permutation(str.begin(), str.end());longlongres =stoll...
Leetcode 556. Next Greater Element III 2. Solution **解析:**Version 1,先将数字n变为字符数组,要找最小的大于n的数,则应该从右往左开始,依次寻找第i位字符右边的大于当前字符的最小数字,然后互换二者位置,由于新数字的第i位字符大于n中的第i位字符,因此新数字i位之后的字符应该从小到大排列,这样可以保...
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 publicclassSolution { publicintnextGreaterElement(intn) { char[] number = (n +"").toCharArray(); inti, j; // I) Start from the right most digit and ...
举个例子:53421,从右往左遍历可以3小于其右侧的任何一个值,则将3和右侧最小的整数进行交换得到51423,再对1右侧的所有数字按照从小到大进行排序,则可以得出大于53421的最小正整数,即51234 代码如下: public int nextGreaterElement(int n) { String value = String.valueOf(n); ...
如果每一位digit依次递减, 比如"54321"就没有next greater element. return -1. 所以说节点在于不是递减的位置, "52431", 找到 “2” 和“4”的位置不是递减. 然后在2的后面比2大的最小digit, 这里是3. "2", "3"换位, 变成"53421", 再把3后面的部分sort成从小到大 "53124"就是next greater elemen...
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 in the second array, so output -1. My solution: class Solution { ...
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
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