当前位置:网站首页>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
边栏推荐
- Talk about the state management mechanism in Flink framework
- 242. Effective letter heteronyms
- Unity3d learning notes 5 - create sub mesh
- Dart: About zone
- typeScript
- Redis 笔记 01:入门篇
- DEJA_VU3D - Cesium功能集 之 053-地下模式效果
- Introduction to concurrent programming (I)
- repo Manifest Format
- 2020-09_ Shell Programming Notes
猜你喜欢

Symlink(): solution to protocol error in PHP artisan storage:link on win10

【附下载】密码获取工具LaZagne安装及使用

OpenGL draws colored triangles

MCDF Experiment 1

Visual studio 2022 downloading and configuring opencv4.5.5

ES6新特性

Unity3d learning notes 5 - create sub mesh

Vulnhub's Nagini

The future of cloud computing cloud native

为什么我的mysql容器启动不了呢
随机推荐
Unicode encoding table download
Develop plug-ins for idea
网络通讯之Socket-Tcp(一)
023 ([template] minimum spanning tree) (minimum spanning tree)
typeScript
242. Effective letter heteronyms
[combinatorics] permutation and combination (summary of permutation and combination content | selection problem | set permutation | set combination)
Dart: about grpc (I)
Kubectl_ Command experience set
OpenGL shader use
Dart: About zone
Dart: view the dill compiled code file
[official MySQL document] deadlock
在网上炒股开户可以吗?资金安全吗?
Unicode查询的官方网站
【mysql专项】读锁和写锁
使用BLoC 构建 Flutter的页面实例
Quantitative calculation research
Swagger
Itext7 uses iexternalsignature container for signature and signature verification