解法一: 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...
[LeetCode] 496. Next Greater Element I 下一个较大的元素 I [LeetCode] 503. Next Greater Element II 下一个较大的元素 II All LeetCode Questions List 题目汇总
复制 classSolution:defnextGreaterElement(self,n:int)->int:digits=list(str(n))i=len(digits)-2whilei>-1and digits[i]>=digits[i+1]:i-=1ifi==-1:return-1j=i+1whilej<len(digits)and digits[j]>digits[i]:j+=1j-=1digits[i],digits[j]=digits[j],digits[i]result=int(''.join(digits...
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:...
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...
【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.
[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...
Explanation: The first 1's next greater number is 2; The number 2 can't find next greater number. The second 1's next greater number needs to search circularly, which is also 2. Example 2: Input:nums = [1,2,3,4,3]Output:[2,3,4,-1,4] ...
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 ...
1 读题:LeetCode 496E 下一个最大元素 2 解题思路 单调栈(栈底最大)的作用在于: 保持栈底始终是一个全栈最大的数值; 目标原来过来,和栈顶的元素对比 (此时要求栈非空),如果栈顶的元素比它大,那么就找到了一个离它最近的、且比它大的元素;