当前位置:网站首页>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
边栏推荐
- Kubernetes three dozen probes and probe mode
- Swagger
- Colleagues wrote a responsibility chain model, with countless bugs
- 为什么我的mysql容器启动不了呢
- error: expected reference but got (raw string)
- repo Manifest Format
- [combinatorics] permutation and combination (example of permutation and combination)
- 网络通讯之Socket-Tcp(一)
- Socket TCP for network communication (I)
- [learning notes] DP status and transfer
猜你喜欢
Swagger
QT OpenGL texture map
Basic knowledge of OpenGL (sort it out according to your own understanding)
ES6 standard
AOSP ~ NTP (Network Time Protocol)
Solution to the second weekly test of ACM intensive training of Hunan Institute of technology in 2022
为什么我的mysql容器启动不了呢
Shutter: add gradient stroke to font
Integer int compare size
OpenGL index cache object EBO and lineweight mode
随机推荐
Shutter: add gradient stroke to font
Adult adult adult
typeScript
Fluent: Engine Architecture
Dart: about Libraries
wpa_ cli
1-1 token
php 获取文件夹下面的文件列表和文件夹列表
Jsup crawls Baidu Encyclopedia
PHP export word method (phpword)
Qt+vtk+occt reading iges/step model
Official website of Unicode query
239. Sliding window maximum
(construction notes) learn the specific technology of how to design reusable software entities from three levels: class, API and framework
Php Export word method (One MHT)
Prompt unread messages and quantity before opening chat group
20. Valid brackets
PHP導出word方法(一mht)
242. Effective letter heteronyms
2020-11_ Technical experience set