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...
leetcode556. Next Greater Element III 题目要求 Given a positive32-bitintegern, you need to find the smallest32-bitinteger which has exactly the same digits existing in the integernand is greater in value than n. If no such positive32-bitinteger exists, you need to return -1. Example 1:...
解法一: classSolution {public:intnextGreaterElement(intn) {stringstr =to_string(n);intlen = str.size(), i = len -1;for(; i >0; --i) {if(str[i] > str[i -1])break; }if(i ==0)return-1;for(intj = len -1; j >= i; --j) {if(str[j] > str[i -1]) { swap(st...
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 Description 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 n and is greater in value than n. If no such positive 32-bit integer exists, you need to ...
题目链接: https://leetcode-cn.com/problems/next-greater-element-iii难度:中等 通过率:26.3% 题目描述:给定一个 32位 正整数 n ,你需要找到最小的 32位 整数,其与 n 中存在的位数完全相同,并且其值大于n…
Can you solve this real interview question? Next Greater Element III - Given a positive integer n, find the smallest integer which has exactly the same digits existing in the integer n and is greater in value than n. If no such positive integer exists, r
Leetcode 556. Next Greater Element III 1. Description 2. Solution **解析:**Version 1,先将数字n变为字符数组,要找最小的大于n的数,则应该从右往左开始,依次寻找第i位字符右边的大于当前字符的最小数字,然后互换二者位置,由于新数字的第i位字符大于n中的第i位字符,因此新数字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 n and is greater in value than n. If no such positive 32-bit integer exists, you need to return -1. ...
【Leetcode】556. Next Greater Element III 1 要注意结果,因为可能结果超出32bit能表示的范围了 2 思路是https://leetcode.com/problems/next-greater-element-iii/discuss/101824/Simple-Java-solution-(4ms)-with-explanation.