当前位置:网站首页>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
边栏推荐
- DEJA_ Vu3d - 054 of cesium feature set - simulate the whole process of rocket launch
- 网上炒股开户安不安全?谁给回答一下
- Use of QT OpenGL camera
- error: expected reference but got (raw string)
- Unicode查询的官方网站
- [combinatorics] permutation and combination (example of permutation and combination)
- Redis notes 01: Introduction
- Quantitative calculation research
- 【附下载】密码获取工具LaZagne安装及使用
- Qt+vtk+occt reading iges/step model
猜你喜欢

shardingSphere分库分表<3>

Colleagues wrote a responsibility chain model, with countless bugs

PHP導出word方法(一mht)

PHP导出word方法(一phpword)

During FTP login, the error "530 login incorrect.login failed" is reported

Quantitative calculation research

Socket TCP for network communication (I)

Download address and installation tutorial of vs2015

If you can't learn, you have to learn. Jetpack compose writes an im app (I)

PHP export word method (one MHT)
随机推荐
Wechat applet development - page Jump transfer parameters
pragma-pack语法与使用
(construction notes) ADT and OOP
Redis notes 01: Introduction
Swagger
Prompt unread messages and quantity before opening chat group
2020-11_ Technical experience set
PHP get the file list and folder list under the folder
2020-09_ Shell Programming Notes
Why can't my MySQL container start
Atomic atomic operation
Pki/ca and digital certificate
在网上炒股开户可以吗?资金安全吗?
Solution à la défaillance de l'installation d'Electron
网上炒股开户安不安全?谁给回答一下
2.7 overview of livedata knowledge points
1-2 project technology selection and structure
【嵌入式】---- 内存四区介绍
2.6 preliminary cognition of synergetic couroutines
(construction notes) grasp learning experience