[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.

Example 1:

Input: 12
Output: 21 

Example 2:

Input: 21
Output: -1

思路:

首先,将整型数字转换成字符串,然后利用stl提供的next_permutation()函数,求字符的全排列,对应的字符串再转换回整型,随时记录大小即可。

int nextGreaterElement2(int n) {
        char buf[80];
        sprintf(buf, "%d", n);
        string s = buf;
        puts(s.data());
        sort(s.begin(), s.end());
        long long ans = INT_MAX + 1LL;
        do {
            long long tmp = atoll(s.c_str());
            if (tmp > n) {
                ans = min(ans, tmp);
            }
        } while (next_permutation(s.begin(), s.end()));
        return ans <= INT_MAX ? ans : -1;
    }
时间: 2024-10-12 03:53:59

[leetcode-556-Next Greater Element III]的相关文章

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. Exampl

556. Next Greater Element III

跟字典序那道题一样 找出下一个 大于MAX_VALUE或者相同就return-1 1 class Solution { 2 public int nextGreaterElement(int n) { 3 if(n == 0) return -1; 4 String str = "" + n; 5 char[] arr = str.toCharArray(); 6 int[] arr1 = new int[arr.length]; 7 for(int i = 0; i < arr

LeetCode 556. 下一个更大元素 III(Next Greater Element III)

556. 下一个更大元素 III 556. Next Greater Element III 题目描述 给定一个 32 位正整数 n,你需要找到最小的 32 位整数,其与 n 中存在的位数完全相同,并且其值大于 n.如果不存在这样的 32 位整数,则返回-1. LeetCode556. Next Greater Element III中等 示例 1: 输入: 12 输出: 21 示例 2: 输入: 21 输出: -1 示例 3: 输入: 12443322 输出: 13222344 Java 实现

Leetcode 503. Next Greater Element II JAVA语言

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 ar

[栈] leetcode 503 Next Greater Element II

problem:https://leetcode.com/problems/next-greater-element-ii/ 一道比较简单的单调队列题目.不过由于题目要求是循环的,需要两个pass,第二个pass处理循环生效的next greater,同时需要把下标已经超出范围的队首数据及时pop出来. class Solution { public: vector<int> nextGreaterElements(vector<int>& nums) { deque<

leetcode556—— Next Greater Element III (JAVA)

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. Example 1: I

[Swift]LeetCode556. 下一个更大元素 III | 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. Example 1: In

LeetCode &quot;496. Next Greater Element I&quot; !

Play it in your mind.. or it may be hard to reach to this intuitive solution... class Solution(object): def nextGreaterElement(self, findNums, nums): hm = {} stk = [] for n in nums: while len(stk)>0 and stk[-1] < n: hm[stk[-1]] = n stk.pop() stk.app

LeetCode 496. 下一个更大元素 I(Next Greater Element I) 35

496. 下一个更大元素 I 496. Next Greater Element I 题目描述 给定两个没有重复元素的数组?nums1 和?nums2,其中 nums1?是?nums2?的子集.找到?nums1?中每个元素在?nums2?中的下一个比其大的值. nums1?中数字?x?的下一个更大元素是指?x?在?nums2?中对应位置的右边的第一个比?x?大的元素.如果不存在,对应位置输出 -1. 每日一算法2019/6/7Day 35LeetCode496. Next Greater Ele