首页 技术 正文
技术 2022年11月15日
0 收藏 867 点赞 4,642 浏览 1800 个字

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

Approach #1: String. [Java]

class Solution {
public int nextGreaterElement(int n) {
int i, j;
char[] nums = new String(n + "").toCharArray();
for (i = nums.length-1; i > 0; --i) {
if (nums[i-1] < nums[i])
break;
} if (i == 0) return -1; int x = nums[i-1], smallest = i;
for (j = i + 1; j < nums.length; ++j) {
if (nums[j] > x && nums[j] < nums[smallest])
smallest = j;
}
char temp = nums[i-1];
nums[i-1] = nums[smallest];
nums[smallest] = temp;
Arrays.sort(nums, i, nums.length);
long ret = Long.parseLong(new String(nums)); return ret < Integer.MAX_VALUE ? (int)ret : -1;
}
}

  

Analysis:

At first, let’s look at the edge cases:

1. If all digits sorted in descending order, then output is always “Not Possible”. Foe example 4321.

2. If all digits are sorted in ascending order, then we need to swap last two digits. For example, 1234.

3. For other cases, we need to process the number from rightmost side.

The main algorithm works in following steps:

1. Traverse the given number from rightmost digit, keep traversing till you find a digit which is smaller the given trversed digit. For example, if the input number is “534976”, we stop at 4 because 4 is smaller than next digit 9. If we do not find such a digit, then outpit is “Not Possible”.

2. Now search the right side of above found digit ‘d’ for the smallest digit greater than ‘d’. For “534976”, the right side of 4 contains “976”. The smallest digit greater than 4 to 6.

3. Swap the above found two digits, we get 536974 in above example.

4. Now sort all digits form position next to ‘d’ to the end of number. The number that we get after sorting is the output. For above example, we sort digits in bold 53974. We get “536479” which is the next greater number for input 534976.

Reference:

https://leetcode.com/problems/next-greater-element-iii/discuss/101824/Simple-Java-solution-(4ms)-with-explanation.

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,086
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,561
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,411
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,184
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,820
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,904