556. 下一个更大元素 III

给定一个32位正整数 n,你需要找到最小的32位整数,其与 n 中存在的位数完全相同,并且其值大于n。如果不存在这样的32位整数,则返回-1。

  • 示例 1:

    输入: 12

    输出: 21
  • 示例 2:

    输入: 21

    输出: -1

代码

class Solution {
public int nextGreaterElement(int n) {
int[] array = transfer(n);
/*
求出的下一个全排列可能会超出int类型的范围
这并不符合得出的答案是32位的结果
所以我将结果用long保存
如果它超出了int的表示范围
就返回-1
*/
long result = nextArray(array);
if (result > Integer.MAX_VALUE) {
return -1;
}
return (int)result;
//return nextArray(array);
} public long nextArray(int[] array) {
int length = array.length; /*
* 第一步,寻找最后一个正序
* 寻找方法 : 从后向前找
*/
int i;
for (i = length-1; i > 0 ; i--) {
if (array[i-1] < array[i]) {
break;
}
}
/*
* 如果没找到,也就是到了最后的一个
*/
if (i == 0) {
return -1;
} /*
*第二步,从后往前找一个比array[i-1]大的第一个数字.
*/
int j;
for (j = length-1; j > i; j --) {
if (array[j] > array[i-1]) {
break;
}
} /*
第三步,交换array[i-1],array[j]
*/
int temp = array[i-1];
array[i-1] = array[j];
array[j] = temp; /*
最后一步,把i-1后面(从i-2开始至length-1)的序列反序
*/
while (i < length-1) {
temp = array[i];
array[i] = array[length-1];
array[length-1] = temp; i ++;
length --;
}
/*
反序完毕即为一次排列结束
得到的序列就是下一个全排列
*/ long result = 0;
for (int index = 0; index < array.length; index++) {
result *= 10;
result += array[index];
} return result;
} private int[] transfer(int n) {
String str = Integer.toString(n);
int[] arr = new int[str.length()];
for (int i = 0; i < arr.length; i++) {
arr[i] = Integer.parseInt(String.valueOf(str.charAt(i)));
}
return arr;
}
}

556. 下一个更大元素 III的更多相关文章

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

    556. 下一个更大元素 III 556. Next Greater Element III 题目描述 给定一个 32 位正整数 n,你需要找到最小的 32 位整数,其与 n 中存在的位数完全相同,并 ...

  2. Java实现 LeetCode 556 下一个更大元素 III(数组的翻转)

    556. 下一个更大元素 III 给定一个32位正整数 n,你需要找到最小的32位整数,其与 n 中存在的位数完全相同,并且其值大于n.如果不存在这样的32位整数,则返回-1. 示例 1: 输入: 1 ...

  3. Leetcode 556.下一个更大元素III

    下一个更大元素III 给定一个32位正整数 n,你需要找到最小的32位整数,其与 n 中存在的位数完全相同,并且其值大于n.如果不存在这样的32位整数,则返回-1. 示例 1: 输入: 12 输出: ...

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

  5. LeetCode 503. 下一个更大元素 II(Next Greater Element II)

    503. 下一个更大元素 II 503. Next Greater Element II 题目描述 给定一个循环数组(最后一个元素的下一个元素是数组的第一个元素),输出每个元素的下一个更大元素.数字 ...

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

    496. 下一个更大元素 I 496. Next Greater Element I 题目描述 给定两个没有重复元素的数组 nums1 和 nums2,其中 nums1 是 nums2 的子集.找到  ...

  7. [Swift]LeetCode496. 下一个更大元素 I | Next Greater Element I

    You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of n ...

  8. [Swift]LeetCode503. 下一个更大元素 II | Next Greater Element II

    Given a circular array (the next element of the last element is the first element of the array), pri ...

  9. Leetcode 496. 下一个更大元素 I

    1.题目描述 给定两个没有重复元素的数组 nums1 和 nums2 ,其中nums1 是 nums2 的子集.找到 nums1 中每个元素在 nums2 中的下一个比其大的值. nums1 中数字  ...

随机推荐

  1. 请求转发和重定向实现与Ajax实现表单登陆

    private void login(HttpServletRequest request, HttpServletResponse response) throws ServletException ...

  2. 2.2 spring5源码 -- ioc加载的整体流程

    之前我们知道了spring ioc的加载过程, 具体如下图. 下面我们就来对照下图, 看看ioc加载的源代码. 下面在用装修类比, 看看个个组件都是怎么工作的. 接下来是源码分析的整体结构图. 对照上 ...

  3. PyCharm专业版编辑器安装(含破解步骤)

    一.首先下载好安装包: 二.安装步骤: 1.双击安装包,打开安装界面,点击"next": 2.选择安装路径,点击"next": 3.电脑是64位的就勾选64-b ...

  4. Django在Linux上uwsgi 与nginx的问题与解决

    1.出现只有weclome to nginx 多半是是nginx的配置文件没有修改,把他的路由注释掉. 我是修改错文件夹了,一直在自己下载而非运行的文件夹修改 2.出现502 出现了多次502这里一一 ...

  5. Go-The process cannot access the file because it is being used by another process.

    where? Go程序在读取文件时候 why? 因为有其他进程也在读取和Go程序想要读取的文件,参数冲突 way? 关闭其他程序进程对该文件的读取操作

  6. 05 sublime环境配置及编译运行后输出中文乱码的解决

    编译后的乱码问题 编译后的输出:中文显示异常: 编译C出现乱码问题解决 解决思路:解决办法很简单,就是先设置文件编码为GBK格式,之后再输入中文文字,运行时的中文就不是乱码了. 首先,sublime中 ...

  7. lens distortion

    来源:http://michel.thoby.free.fr/Fisheye_history_short/International_Standards_about_Distortion.html H ...

  8. VS Code 搭建编写Shell环境(WSL)

    安装过程 Win10开启WSL,方法略 安装VSCode,方法略 安装语法提示插件:shellman 安装格式化插件:shell-format(右键 -> 格式化文档(Ctrl + Alt + ...

  9. Jmeter接口测试--上传附件

    jmeter接口测试上传附件指引 1.添加HTTP请求取样器--在取样器中的HTTP请求项中对"使用KeepAlive"."对POST使用multipart/form-d ...

  10. windows server2008 r2激活

    KMS激活: 管理员运行cmd 输入以下命令 slmgr /ipk 密钥slmgr /skms zh.us.toslmgr /atoslmgr /xpr 可用密钥如下: KMS Windows Ser ...