[抄题]:

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

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

[思维问题]:

完全没思路啊:这种题一般是从递增/递减的角度来考虑

[英文数据结构或算法,为什么不用别的数据结构或算法]:

parselong里面是字符串型

long val = Long.parseLong(new String(nums));

[一句话思路]:

找到递增元素,把前面的一个最小元素往后换,然后后半截排序

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. smallest是大的那边,所以从i开始换, nums[i-1]保持不变就行了

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

找到递增元素,把前面的一个最小元素往后换,然后后半截排序

[复杂度]:Time complexity: O() Space complexity: O()

[算法思想:迭代/递归/分治/贪心]:

[关键模板化代码]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

[是否头一次写此类driver funcion的代码] :

[潜台词] :

class Solution {
public int nextGreaterElement(int n) {
//initilization: new array
char[] nums = (n + "").toCharArray();
int i; //find the increasing element from the end to ensure the first bigger
for (i = nums.length - 1; i > 0; i--) {
if (nums[i] > nums[i - 1]) {
break;
}
}
//corner case: decrease, put the inner variable together
if (i == 0) return -1; //find the later element
int smallest = i;
for (int j = i + 1; j < nums.length; j++) {
if (nums[j] > nums[i - 1] && nums[j] <= nums[smallest]) {
smallest = j;
}
} //swap the smaller with the later element
char temp = nums[smallest];
nums[smallest] = nums[i - 1];
nums[i - 1] = temp; //sort the later element
Arrays.sort(nums, i, nums.length); //change to long and int, notice corner case
long val = Long.parseLong(new String(nums));
return (val <= Integer.MAX_VALUE) ? (int) val : -1;
}
}

556. Next Greater Element III下一个更大的数字的更多相关文章

  1. [LeetCode] 556. Next Greater Element III 下一个较大的元素 III

    Given a positive 32-bit integer n, you need to find the smallest 32-bit integer which has exactly th ...

  2. 496 Next Greater Element I 下一个更大元素 I

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

  3. Leetcode496.Next Greater Element I下一个更大的元素1

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

  4. 503 Next Greater Element II 下一个更大元素 II

    给定一个循环数组(最后一个元素的下一个元素是数组的第一个元素),输出每个元素的下一个更大元素.数字 x 的下一个更大的元素是按数组遍历顺序,这个数字之后的第一个比它更大的数,这意味着你应该循环地搜索它 ...

  5. [LeetCode] Next Greater Element III 下一个较大的元素之三

    Given a positive 32-bit integer n, you need to find the smallest 32-bit integer which has exactly th ...

  6. [LeetCode] 496. Next Greater Element I 下一个较大的元素 I

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

  7. [LeetCode] 503. Next Greater Element II 下一个较大的元素 II

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

  8. [LeetCode] 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] Next Greater Element I 下一个较大的元素之一

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

随机推荐

  1. Vue创建头部组件示例

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta ht ...

  2. 1.1.8 怎样在Word的页眉中插入一级标题

    可以通过域来实现,其具体的操作步骤: 1.为章.节标题使用标题样式.例如:章标题使用标题1样式,节标题使用标题2样式.操作方法:选中章(节)标题,然后点击选项卡中“样式”中的). 2.设置文档页眉和页 ...

  3. [C#]typeof,Gettype()和is的区别

    typeof 参数是一个类型名称,比如你自己编写的一个类 GetType()是类的方法,继承自object,返回该实例的类型 is 用来检测实例的兼容性(是否可以相互转换) 例: class Anim ...

  4. Maya中输出alembic文件的方法

    Maya中输出alembic文件是有现成api调用的,与maya中大部分api一样,这个功能参数的传入是非常类似mel的,本质上讲都是kwargs类型的参数,所以我们传入的参数就需要整理成类似于mel ...

  5. git与github区别与简介

    From: https://blog.csdn.net/skyxmstar/article/details/65631658 git和github是两个完全不同的概念. git 是一个版本管理工具,是 ...

  6. 前端-JavaScript1-5——JavaScript之变量的类型

    5.1 概述 基本类型5种 number          数字类型 string             字符串类型 undefined      undefined类型,变量未定义时的值,这个值自 ...

  7. centos7-网络连接

    Centos系统在安装完毕后,默认联网状态为no,需要手动开启联网状态. 编辑网卡文件 vim /etc/sysconfig/network-scripts/ifcfg-eno16777736 修改O ...

  8. [转][C#]文件流读取

    { internal static class FileUtils { public static string GetRelativePath(string absPath, string base ...

  9. [UE4]字体材质

    一.准备好一个字体文件,直接拖放到内容浏览器 二.创建一个名为testFontMaterial的UserWidget,添加一个TextBlock到默认的CanvasPanel.Font Family: ...

  10. Centos7.5 安装高版本Cmake 3.6.2

    下载Cmake wget https://cmake.org/files/v3.6/cmake-3.6.2.tar.gz 解压Cmake tar xvf cmake-3.6.2.tar.gz & ...