g0501_0600.s0556_next_greater_element_iii.readme.md Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-java Show documentation
Show all versions of leetcode-in-java Show documentation
Java-based LeetCode algorithm problem solutions, regularly updated
556\. Next Greater Element III
Medium
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, return `-1`.
**Note** that the returned integer should fit in **32-bit integer**, if there is a valid answer but it does not fit in **32-bit integer**, return `-1`.
**Example 1:**
**Input:** n = 12
**Output:** 21
**Example 2:**
**Input:** n = 21
**Output:** -1
**Constraints:**
* 1 <= n <= 231 - 1