当前位置:网站首页>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;
}
};
边栏推荐
猜你喜欢
Golang中UTF编码和字符集
uniapp 富文本编辑器使用
TweenMax表情按钮js特效
[observation] Lenovo: 3x (1+n) smart office solution, releasing the "multiplier effect" of office productivity
【微信小程序】协同工作与发布
Jerry's ad series MIDI function description [chapter]
Explication détaillée du mécanisme de distribution des événements d'entrée multimodes
杰理之AD 系列 MIDI 功能说明【篇】
PS vertical English and digital text how to change direction (vertical display)
Solution of 5g unstable 5g signal often dropped in NetWare r7000 Merlin system
随机推荐
Explication détaillée du mécanisme de distribution des événements d'entrée multimodes
Procurement in software development
巅峰不止,继续奋斗!城链科技数字峰会于重庆隆重举行
redis缓存
杰理之增加进关机前把触摸模块关闭流程【篇】
Huawei ENSP simulator enables devices of multiple routers to access each other
Redis bloom filter
Golang中UTF编码和字符集
冰河的海报封面
杰理之AD 系列 MIDI 功能说明【篇】
__ init__ () missing 2 required positive arguments
maya灯建模
Embedded TC test case
Actual combat simulation │ JWT login authentication
FastDfs的快速入门,三分钟带你上传下载文件到云服务器
B站视频 声音很小——解决办法
奋斗正当时,城链科技战略峰会广州站圆满召开
Can be displayed in CAD but not displayed in print
Jerry's ad series MIDI function description [chapter]
插入排序,选择排序,冒泡排序