AI代码解释 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 ma...
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...
publicintnextGreaterElement(intn) { char[] number = (n +"").toCharArray(); inti, j; // I) Start from the right most digit and // find the first digit that is // smaller than the digit next to it. for(i = number.length-1; i >0; i--) if(number[i-1] < number[i]) br...
Leetcode 556. Next Greater Element III 2. Solution **解析:**Version 1,先将数字n变为字符数组,要找最小的大于n的数,则应该从右往左开始,依次寻找第i位字符右边的大于当前字符的最小数字,然后互换二者位置,由于新数字的第i位字符大于n中的第i位字符,因此新数字i位之后的字符应该从小到大排列,这样可以保...
next_permutation(str.begin(), str.end());longlongres =stoll(str);return(res > INT_MAX || res <= n) ? -1: res; } }; 类似题目: Next Greater Element II Next Greater Element I 参考资料: https://discuss.leetcode.com/topic/85740/c-4-lines-next_permutation ...
public int nextGreaterElement(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]) { ...
LeetCode 556. Next Greater Element III 题目: Given a positive 32-bit integer n, you need to find the smallest 32-bit integer which has exactly the same digits existing in the integer nand is greater in value than n. If no such positive 32-bit integer exists, you need to return -1....
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
public int[] nextGreaterElement(int[] findNums, int[] nums) { Map<Integer, Integer> map = new HashMap<>(); // map from x to next greater element of x Stack<Integer> stack = new Stack<>(); for (int num : nums) { //pay attention, we use nums2 to constuact our map, and ...
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 of nums2. The Next Greater Number of a number x in nums1 is the first...