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
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
[思维问题]:
完全没思路啊:这种题一般是从递增/递减的角度来考虑
[英文数据结构或算法,为什么不用别的数据结构或算法]:
parselong里面是字符串型
long val = Long.parseLong(new String(nums));
[一句话思路]:
找到递增元素,把前面的一个最小元素往后换,然后后半截排序
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:

[一刷]:
- 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下一个更大的数字的更多相关文章
- [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 ...
- 496 Next Greater Element I 下一个更大元素 I
给定两个没有重复元素的数组 nums1 和 nums2 ,其中nums1 是 nums2 的子集.找到 nums1 中每个元素在 nums2 中的下一个比其大的值.nums1 中数字 x 的下一个更大 ...
- Leetcode496.Next Greater Element I下一个更大的元素1
给定两个没有重复元素的数组 nums1 和 nums2 ,其中nums1 是 nums2 的子集.找到 nums1 中每个元素在 nums2 中的下一个比其大的值. nums1 中数字 x 的下一个更 ...
- 503 Next Greater Element II 下一个更大元素 II
给定一个循环数组(最后一个元素的下一个元素是数组的第一个元素),输出每个元素的下一个更大元素.数字 x 的下一个更大的元素是按数组遍历顺序,这个数字之后的第一个比它更大的数,这意味着你应该循环地搜索它 ...
- [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 ...
- [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 ...
- [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 ...
- [LeetCode] Next Greater Element II 下一个较大的元素之二
Given a circular array (the next element of the last element is the first element of the array), pri ...
- [LeetCode] Next Greater Element I 下一个较大的元素之一
You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of n ...
随机推荐
- enumerate()使用
enumerate()使用 如果对一个列表,既要遍历索引又要遍历元素时,首先可以这样写: list1 = ["这", "是", "一个", ...
- PythonStudy——函数嵌套定义 Function nesting definition
# 在一个函数内部定义另一个函数 # 函数对象(变量)与普通对象(变量)一样,在函数内部定义,随函数调用而产生, # 调用结束而销毁,所以只能在函数内部调用 def outer(): print('o ...
- MySQL通过分组计算百分比
公司在做柯米克的分析报告,需要我这边把汽车之家柯米克论坛的评论数据和评论用户所在地的数据获取,通过爬虫的方式很快的解决了数据的问题,但是需要我提取下各省评论人数的比例,所以在数据库里面直接计算了相关的 ...
- py-day2-4 python 集合
# 集合是由 { ,} 组成 test = {1,2,8,9,7,5} print(test) {1, 2, 5, 7, 8, 9} # 集合的结果是 去重的,且排序是 无序的 test = {1,2 ...
- 【剑指offer】数组中只出现一次的数字
题目:一个整型数组里除了两个数字之外,其他的数字都出现了偶数次.请写程序找出这两个只出现一次的数字. 思路1:使用HashMap存上所有的数字,数字作为Key,Value为对应的出现次数.这种做法可以 ...
- globals和locals的区别
Python的两个内置函数,locals 和globals,它们提供了基于字典的访问局部和全局变量的方式. 1.locals()是只读的.globals()不是.这里说的只读,是值对于原有变量的只读. ...
- perl open函数的使用
本文和大家重点讨论一下如何读写Perl文件,主要包括打开.关闭Perl文件,读写Perl文件,Perl文件的状态,命令行参数和打开管道六部分内容,希望通过本文的学习你对读写Perl文件有深刻的认识. ...
- html/css/js-layer弹出层的初次使用
学习前端有时很多时候要用到弹出层,原生的js写有些麻烦,而且不美观,基于jQuery的弹出层组件layer应运而生,近些年来备受青睐.官网上有使用教程,但当初用的时候还是糊里糊涂,今天来记录一下lay ...
- wampserver_x86_3.0.6 允许外网访问配置教程
1.打开wamp目录下的apache配置文件中的httpd.conf 用可以看行数的编辑器打开 大概244行: 改为 <Directory /> AllowOverride none Re ...
- css实现垂直居中的方法整理
1.表格布局法.(利用表格的显示模式)需要用到一些冗余的 HTML 元素,因此这里不多介绍. 2.行内块法.也不作讨论,因为在我看来这种方法 hack 的味道很浓. 如果你有兴趣,可以去看看 Chri ...