当前位置:网站首页>【556. 下一个更大元素 III】
【556. 下一个更大元素 III】
2022-07-03 13:10:00 【千北@】
来源:力扣(LeetCode)
描述:
给你一个正整数 n ,请你找出符合条件的最小整数,其由重新排列 n 中存在的每位数字组成,并且其值大于 n 。如果不存在这样的正整数,则返回 -1 。
注意 ,返回的整数应当是一个 32 位整数 ,如果存在满足题意的答案,但不是 32 位整数 ,同样返回 -1 。
示例 1:
输入:n = 12
输出:21
示例 2:
输入:n = 21
输出:-1
提示:
1 <= n <= 231 - 1
方法一:下一个排列
把 n 转换成字符串(字符数组),那么本题实际上是在求字符数组的 下一个排列,当不存在下一个排列时返回 −1。
代码:
class Solution {
public:
int nextGreaterElement(int n) {
auto nums = to_string(n);
int i = (int) nums.length() - 2;
while (i >= 0 && nums[i] >= nums[i + 1]) {
i--;
}
if (i < 0) {
return -1;
}
int j = nums.size() - 1;
while (j >= 0 && nums[i] >= nums[j]) {
j--;
}
swap(nums[i], nums[j]);
reverse(nums.begin() + i + 1, nums.end());
long ans = stol(nums);
return ans > INT_MAX ? -1 : ans;
}
};
执行用时:0 ms, 在所有 C++ 提交中击败了100.00%的用户
内存消耗:5.8 MB, 在所有 C++ 提交中击败了57.91%的用户
复杂度分析
时间复杂度: O(logn)。
空间复杂度:O O(logn)。
方法二:数学
不转换成字符数组,如何用 O(1) 空间复杂度解决此题?
如果还要求不使用 64 位整数呢?
类似方法一,我们从 n 开始,不断比较其最低位数字和次低位数字的大小,如果次低位数字不低于最低位数字,则移除最低位数字,继续循环。循环结束后的 n 就对应着方法一的下标 i,即 nums 的前 i + 1 个字符。
对于方法一中下标 j 的计算也是同理。
代码:
class Solution {
public:
int nextGreaterElement(int n) {
int x = n, cnt = 1;
for (; x >= 10 && x / 10 % 10 >= x % 10; x /= 10) {
++cnt;
}
x /= 10;
if (x == 0) {
return -1;
}
int targetDigit = x % 10;
int x2 = n, cnt2 = 0;
for (; x2 % 10 <= targetDigit; x2 /= 10) {
++cnt2;
}
x += x2 % 10 - targetDigit; // 把 x2 % 10 换到 targetDigit 上
for (int i = 0; i < cnt; ++i, n /= 10) {
// 反转 n 末尾的 cnt 个数字拼到 x 后
int d = i != cnt2 ? n % 10 : targetDigit;
if (x > INT_MAX / 10 || x == INT_MAX / 10 && d > 7) {
return -1;
}
x = x * 10 + d;
}
return x;
}
};
执行用时:0 ms, 在所有 C++ 提交中击败了100.00%的用户
内存消耗:5.7 MB, 在所有 C++ 提交中击败了94.76%的用户
复杂度分析
时间复杂度: O(logn)。
空间复杂度: O(1)。我们只需要常数的空间保存若干变量。
author:LeetCode-Solution
边栏推荐
- Brief analysis of tensorboard visual processing cases
- 双链笔记 RemNote 综合评测:快速输入、PDF 阅读、间隔重复/记忆
- 道路建设问题
- 使用Tensorflow进行完整的深度神经网络CNN训练完成图片识别案例2
- Smbms project
- Today's sleep quality record 77 points
- IBEM mathematical formula detection data set
- MapReduce实现矩阵乘法–实现代码
- JS 将伪数组转换成数组
- Flink SQL knows why (12): is it difficult to join streams? (top)
猜你喜欢

Flick SQL knows why (10): everyone uses accumulate window to calculate cumulative indicators
![[quantitative trading] permanent portfolio, turtle trading rules reading, back testing and discussion](/img/3b/28327bbf5eb19254f03500a41e2adb.jpg)
[quantitative trading] permanent portfolio, turtle trading rules reading, back testing and discussion

File uploading and email sending

Setting up remote links to MySQL on Linux

Flink SQL knows why (XV): changed the source code and realized a batch lookup join (with source code attached)

Mobile phones and computers can be used, whole people, spoof code connections, "won't you Baidu for a while" teach you to use Baidu

Flink SQL knows why (16): dlink, a powerful tool for developing enterprises with Flink SQL
![[today in history] July 3: ergonomic standards act; The birth of pioneers in the field of consumer electronics; Ubisoft releases uplay](/img/18/b06e2e5a2f76dc2da1c2374b8424b3.png)
[today in history] July 3: ergonomic standards act; The birth of pioneers in the field of consumer electronics; Ubisoft releases uplay

Libuv Library - Design Overview (Chinese version)

Kivy教程之 盒子布局 BoxLayout将子项排列在垂直或水平框中(教程含源码)
随机推荐
Asp. Net core1.1 without project JSON, so as to generate cross platform packages
The reasons why there are so many programming languages in programming internal skills
Red hat satellite 6: better management of servers and clouds
Logback log framework
MapReduce实现矩阵乘法–实现代码
服务器硬盘冷迁移后网卡无法启动问题
Useful blog links
ThreadPoolExecutor realizes multi-threaded concurrency and obtains the return value (elegant and concise way)
PowerPoint 教程,如何在 PowerPoint 中将演示文稿另存为视频?
Spark实战1:单节点本地模式搭建Spark运行环境
双链笔记 RemNote 综合评测:快速输入、PDF 阅读、间隔重复/记忆
PowerPoint tutorial, how to save a presentation as a video in PowerPoint?
When updating mysql, the condition is a query
Flink SQL knows why (16): dlink, a powerful tool for developing enterprises with Flink SQL
【电脑插入U盘或者内存卡显示无法格式化FAT32如何解决】
DQL basic query
Smbms project
Annotation and reflection
【历史上的今天】7 月 3 日:人体工程学标准法案;消费电子领域先驱诞生;育碧发布 Uplay
18W word Flink SQL God Road manual, born in the sky