当前位置:网站首页>每日一题-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;
}
};
边栏推荐
猜你喜欢

Configuration of DNS server of Huawei ENSP simulator

B站视频 声音很小——解决办法

华为ensp模拟器 配置ACL访问控制列表

Idea configuration standard notes

华为ensp模拟器 DNS服务器的配置

【观察】联想:3X(1+N)智慧办公解决方案,释放办公生产力“乘数效应”

搭建一个仪式感点满的网站,并内网穿透发布到公网 1/2

How does wincc7.5 SP1 find variables and their positions through cross indexing?

Solution of 5g unstable 5g signal often dropped in NetWare r7000 Merlin system
![[1200. Minimum absolute difference]](/img/fa/4ffbedd8f24c75a20d3eaeaf0430ae.png)
[1200. Minimum absolute difference]
随机推荐
Sword finger offer II 80-100 (continuous update)
Idea configuration standard notes
[1200. Minimum absolute difference]
杰理之AD 系列 MIDI 功能说明【篇】
Jmeter 之压测入门
Why is TCP three handshakes and four waves
Quelques suggestions pour la conception de l'interface
BFC interview Brief
Idea restore default shortcut key
【微服务|SCG】Predicate的使用
The concept and application of hash table
redis RDB AOF
In the face of the same complex test task, why can the elder sort out the solution quickly? Ali's ten-year test engineers showed their skills
[solution] paddlepaddle 2 X call static graph mode
宝塔 7.9.2 宝塔控制面板绕过 手机绑定认证 绕过官方认证
HMS Core 机器学习服务
Huawei ENSP simulator configures ACL access control list
[buuctf.reverse] 151_[FlareOn6]DnsChess
VIM asynchronous problem
【解决方案】PaddlePaddle 2.x调用静态图模式
