当前位置:网站首页>Daily question-leetcode556-next larger element iii-string-double pointer-next_ permutation
Daily question-leetcode556-next larger element iii-string-double pointer-next_ permutation
2022-07-04 21:24:00 【Li Fan, hurry up】
Note:
Look from the back to the front , Find the first smaller number , Then put him in the back Exchange the smallest number larger than him
And then change all the positions behind him reverse Again , Just return to the answer
The code is as follows :
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;
}
};
边栏推荐
- A quick start to fastdfs takes you three minutes to upload and download files to the ECS
- Jerry's ad series MIDI function description [chapter]
- redis事务
- __init__() missing 2 required positional arguments 不易查明的继承错误
- 多模輸入事件分發機制詳解
- JS卡牌样式倒计时天数
- 刘锦程荣获2022年度中国电商行业创新人物奖
- Configuration of DNS server of Huawei ENSP simulator
- nmap扫描
- Introduction to pressure measurement of JMeter
猜你喜欢
Jerry's ad series MIDI function description [chapter]
Gobang go to work fishing tools can be LAN / man-machine
Huawei ENSP simulator enables devices of multiple routers to access each other
B站视频 声音很小——解决办法
Configuration of DNS server of Huawei ENSP simulator
6月“墨力原创作者计划”获奖名单公布!邀您共话国产数据库
Jmeter 之压测入门
shp数据制作3DTiles白膜
heatmap.js图片热点热力图插件
Huawei ENSP simulator layer 3 switch
随机推荐
【optimtool.unconstrain】无约束优化工具箱
Foxit pdf editor v10.1.8 green version
杰理之AD 系列 MIDI 功能说明【篇】
Detailed explanation of multi-mode input event distribution mechanism
Vue cleans up the keepalive cache scheme in a timely manner
Render function and virtual DOM
heatmap.js图片热点热力图插件
acwing 3302. Expression evaluation
FastDfs的快速入门,三分钟带你上传下载文件到云服务器
JS卡牌样式倒计时天数
宝塔 7.9.2 宝塔控制面板绕过 手机绑定认证 绕过官方认证
B站视频 声音很小——解决办法
测试用例 (TC)
redis缓存
Difference between ApplicationContext and beanfactory (MS)
c语言函数形参自增自减情况分析
奋斗正当时,城链科技战略峰会广州站圆满召开
PS竖排英文和数字文字怎么改变方向(变竖直显示)
HMS Core 统一扫码服务
Golang中UTF编码和字符集