当前位置:网站首页>每日一题-LeetCode556-下一个更大元素III-字符串-双指针-next_permutation
每日一题-LeetCode556-下一个更大元素III-字符串-双指针-next_permutation
2022-07-04 20:23:00 【李烦烦搞快点】
Note:
先从后往前找,找到第一个变小的数,然后把他和后面 比他大的最小的一个数进行交换
再把换完之后他后面的所有位置reverse一遍,返回答案即可
代码如下:
class Solution {
public:
int nextGreaterElement(int n) {
string s = to_string(n);
int k = s.size() - 1;
while(k && s[k - 1] >= s[k]) k --;
if(!k) return -1;
int t = k;
while(t + 1 < s.size() && s[t + 1] > s[k - 1]) t ++;
swap(s[k - 1], s[t]);
reverse(s.begin() + k, s.end());
long long res = stoll(s);
if(res > INT_MAX) return -1;
return res;
}
};
边栏推荐
- 【申博攻略】六.如何联系心仪的博导
- Actual combat simulation │ JWT login authentication
- acwing 3302. Expression evaluation
- Idea restore default shortcut key
- Huawei ENSP simulator enables devices of multiple routers to access each other
- Quelques suggestions pour la conception de l'interface
- B站视频 声音很小——解决办法
- Configuration of DNS server of Huawei ENSP simulator
- 为什么说不变模式可以提高性能
- Go notes (1) go language introduction and characteristics
猜你喜欢
随机推荐
WinCC7.5 SP1如何通过交叉索引来寻找变量及其位置?
华为ensp模拟器 DNS服务器的配置
Gobang go to work fishing tools can be LAN / man-machine
杰理之AD 系列 MIDI 功能说明【篇】
Introduction to pressure measurement of JMeter
扩展你的KUBECTL功能
Poster cover of glacier
记一次重复造轮子(Obsidian 插件设置说明汉化)
__init__() missing 2 required positional arguments 不易查明的继承错误
Some suggestions for interface design
搭建一个仪式感点满的网站,并内网穿透发布到公网 1/2
Huawei ENSP simulator configures DHCP for router
Idea plug-in
RFID仓储管理系统解决方案的优点
Idea case shortcut
Jekins initialization password not found or not found
vim异步问题
Go language notes (2) some simple applications of go
[buuctf.reverse] 151_[FlareOn6]DnsChess
华为ensp模拟器 给路由器配置DHCP





![[1200. Minimum absolute difference]](/img/fa/4ffbedd8f24c75a20d3eaeaf0430ae.png)




