当前位置:网站首页>LeetCode 0556. Next bigger element III - end of step 4
LeetCode 0556. Next bigger element III - end of step 4
2022-07-03 12:23:00 【Tisfy】
【LetMeFly】4 End of step :556. Next bigger element III
Force button topic link :https://leetcode.cn/problems/next-greater-element-iii/
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 : simulation
In fact, it is to find the next full permutation of the string corresponding to the number , And it's very simple (4 Step ):
Looking back and forth , Find the first “ There is a number greater than it ” Number of numbers ( Write it down as s[i] = x)
stay s[i ~ Last ] Minimum greater than found in x Number of numbers ( Write it down as s[j] = y)
In exchange for s[i] and s[j]
stay s[i+1 ~ Last ] Sort the range from small to large
Then return the number corresponding to the string
- Time complexity O ( N 2 ) O(N^2) O(N2), among N N N Is the number of digits ( N ≤ 10 N\leq 10 N≤10)
- Spatial complexity O ( N log N ) O(N\log N) O(NlogN), Spatial complexity is mainly caused by sorting
AC Code
C++
class Solution {
public:
int nextGreaterElement(int n) {
string s = to_string(n);
bool changed = false;
for (int i = s.size() - 2; i >= 0; i--) {
char m = 58; // '9' = 57
int locm = i;
for (int j = i + 1; j < s.size(); j++) {
if (s[j] > s[i] && s[j] < m) {
m = s[j];
locm = j;
}
}
if (locm != i) {
changed = true;
swap(s[i], s[locm]);
sort(s.begin() + i + 1, s.end());
break;
}
}
if (!changed)
return -1;
ll ans = atoll(s.c_str());
if (ans > INT_MAX)
return -1;
return ans;
}
};
Make a little record hey
Synchronous posting on CSDN, Originality is not easy. , Reprint please attach Link to the original text Oh ~
Tisfy:https://letmefly.blog.csdn.net/article/details/125582351
边栏推荐
- 257. All paths of binary tree
- Integer int compare size
- Visual studio 2022 downloading and configuring opencv4.5.5
- Unicode encoding table download
- 102. Sequence traversal of binary tree
- 2020-10_ Development experience set
- (construction notes) learn the specific technology of how to design reusable software entities from three levels: class, API and framework
- Cloud Computing future - native Cloud
- 023(【模板】最小生成树)(最小生成树)
- Unicode查询的官方网站
猜你喜欢
During FTP login, the error "530 login incorrect.login failed" is reported
PHP導出word方法(一mht)
LeetCode 0556.下一个更大元素 III - 4步讲完
OpenGL index cache object EBO and lineweight mode
If you can't learn, you have to learn. Jetpack compose writes an im app (II)
Shutter: add gradient stroke to font
(construction notes) ADT and OOP
Visual studio 2022 downloading and configuring opencv4.5.5
4000 word super detailed pointer
Vulnhub's Tomato (tomato)
随机推荐
php 获取文件夹下面的文件列表和文件夹列表
PHP导出word方法(一phpword)
2020-11_ Technical experience set
Oracle advanced (I) realize DMP by expdp impdp command
102. Sequence traversal of binary tree
JVM内存模型
Wechat applet pages always report errors when sending values to the background. It turned out to be this pit!
Develop plug-ins for idea
2.8 overview of ViewModel knowledge
1-2 project technology selection and structure
MySQL time zone solution
Dart: self study system
Capturing and sorting out external Fiddler -- Conversation bar and filter [2]
Integer string int mutual conversion
Dart: view the dill compiled code file
Flutter: about monitoring on flutter applications
Redis 笔记 01:入门篇
error: expected reference but got (raw string)
2020-09_ Shell Programming Notes
repo Manifest Format