当前位置:网站首页>[556. Next larger element III]
[556. Next larger element III]
2022-07-03 13:41:00 【[email protected]】
source : Power button (LeetCode)
describe :
Give you a positive integer n , Please find the smallest integer that meets the conditions , It consists of rearranging n Each number present in the consists of , And its value is greater than n . If there is no such positive integer , Then return to -1 .
Be careful , The returned integer should be a 32 An integer , If there is an answer that satisfies the meaning of the question , But it's not 32 An integer , Also return to -1 .
Example 1:
Input :n = 12
Output :21
Example 2:
Input :n = 21
Output :-1
Tips :
1 <= n <= 231 - 1
Method 1 : Next spread
hold n Convert to string ( A character array ), So this problem is actually solving the character array Next spread , Return when there is no next spread −1.
Code :
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;
}
};
Execution time :0 ms, In all C++ Defeated in submission 100.00% Users of
Memory consumption :5.8 MB, In all C++ Defeated in submission 57.91% Users of
Complexity analysis
Time complexity : O(logn).
Spatial complexity :O O(logn).
Method 2 : mathematics
Do not convert to character array , How to use O(1) Space complexity to solve this problem ?
If not required 64 Bit integer ?
Similar method I , We from n Start , Constantly compare the size of the lowest digit and the second lowest digit , If the next lowest digit is not lower than the lowest digit , Then remove the lowest digit , Continue to cycle . At the end of the cycle n It corresponds to the subscript of method one i, namely nums Before i + 1 Characters .
For method one, the subscript j The same is true for the calculation of .
Code :
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; // hold x2 % 10 Switch to targetDigit On
for (int i = 0; i < cnt; ++i, n /= 10) {
// reverse n Last cnt Spell the numbers to x after
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;
}
};
Execution time :0 ms, In all C++ Defeated in submission 100.00% Users of
Memory consumption :5.7 MB, In all C++ Defeated in submission 94.76% Users of
Complexity analysis
Time complexity : O(logn).
Spatial complexity : O(1). We just need a constant space to hold a few variables .
author:LeetCode-Solution
版权声明
本文为[[email protected]]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/184/202207031310265002.html
边栏推荐
- Red hat satellite 6: better management of servers and clouds
- 【被动收入如何挣个一百万】
- Unity render streaming communicates with unity through JS
- Kivy tutorial how to automatically load kV files
- Students who do not understand the code can also send their own token, which is easy to learn BSC
- JS convert pseudo array to array
- IBEM 数学公式检测数据集
- 双链笔记 RemNote 综合评测:快速输入、PDF 阅读、间隔重复/记忆
- Asp. Net core1.1 without project JSON, so as to generate cross platform packages
- Task5: multi type emotion analysis
猜你喜欢

Flick SQL knows why (10): everyone uses accumulate window to calculate cumulative indicators

MySQL installation, uninstallation, initial password setting and general commands of Linux
[email protected]奇安信:透视俄乌网络战 —— 网络空间基础设施面临的安全对抗与制裁博弈..."/>开始报名丨CCF C³[email protected]奇安信:透视俄乌网络战 —— 网络空间基础设施面临的安全对抗与制裁博弈...

Flink SQL knows why (16): dlink, a powerful tool for developing enterprises with Flink SQL

The difference between stratifiedkfold (classification) and kfold (regression)

PowerPoint 教程,如何在 PowerPoint 中將演示文稿另存為視頻?

TensorBoard可视化处理案例简析

掌握Cypress命令行选项,是真正掌握Cypress的基础

PowerPoint tutorial, how to save a presentation as a video in PowerPoint?

MySQL
随机推荐
Task6: using transformer for emotion analysis
Swiftui development experience: the five most powerful principles that a programmer needs to master
Kivy教程之 如何自动载入kv文件
106. How to improve the readability of SAP ui5 application routing URL
Flink SQL knows why (19): the transformation between table and datastream (with source code)
The reasons why there are so many programming languages in programming internal skills
When we are doing flow batch integration, what are we doing?
显卡缺货终于到头了:4000多块可得3070Ti,比原价便宜2000块拿下3090Ti
Open PHP error prompt under Ubuntu 14.04
网上开户哪家证券公司佣金最低,我要开户,网上客户经理开户安全吗
Anan's doubts
untiy世界边缘的物体阴影闪动,靠近远点的物体阴影正常
ThreadPoolExecutor realizes multi-threaded concurrency and obtains the return value (elegant and concise way)
MapReduce实现矩阵乘法–实现代码
已解决(机器学习中查看数据信息报错)AttributeError: target_names
The principle of human voice transformer
【被动收入如何挣个一百万】
(first) the most complete way to become God of Flink SQL in history (full text 180000 words, 138 cases, 42 pictures)
CVPR 2022 | interpretation of 6 excellent papers selected by meituan technical team
JS 将伪数组转换成数组