当前位置:网站首页>每日一题-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;
}
};
边栏推荐
- [server data recovery] a case of RAID5 data recovery stored in a brand of server
- 宝塔 7.9.2 宝塔控制面板绕过 手机绑定认证 绕过官方认证
- Jekins initialization password not found or not found
- 杰理之AD 系列 MIDI 功能说明【篇】
- admas零件名重复
- 华为ensp模拟器 配置ACL访问控制列表
- 搭建一个仪式感点满的网站,并内网穿透发布到公网 1/2
- LeetCode 7. Integer inversion
- TweenMax表情按钮js特效
- 【服务器数据恢复】某品牌服务器存储raid5数据恢复案例
猜你喜欢
Idea configuration standard notes
Pytorch---使用Pytorch实现LinkNet进行语义分割
The video sound of station B is very low - solution
HWiNFO硬件检测工具v7.26绿色版
Leetcode+ 81 - 85 monotone stack topic
华为ensp模拟器 三层交换机
From automation to digital twins, what can Tupo do?
Hands on deep learning (III) -- convolutional neural network CNN
杰理之增加进关机前把触摸模块关闭流程【篇】
多模輸入事件分發機制詳解
随机推荐
MySQL --- 数据库查询 - 聚合函数的使用、聚合查询、分组查询
Idea configuration standard notes
冰河的海报封面
Huawei ENSP simulator configures DHCP for router
Huawei ENSP simulator layer 3 switch
多模输入事件分发机制详解
2021 CCPC 哈尔滨 I. Power and Zero(二进制 + 思维)
async await 在map中使用
6月“墨力原创作者计划”获奖名单公布!邀您共话国产数据库
LeetCode 7. 整数反转
软件开发过中的采购
Redis:Redis配置文件相关配置、Redis的持久化
华为ensp模拟器 三层交换机
Jekins initialization password not found or not found
Day24:文件系统
Ten years' experience of byte test engineer directly hits the pain point of UI automation test
华为ensp模拟器 实现多个路由器的设备可以相互访问
BFC interview Brief
JS卡牌样式倒计时天数
【optimtool.unconstrain】无约束优化工具箱