当前位置:网站首页>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;
}
};
边栏推荐
- IIC (STM32)
- Difference between ApplicationContext and beanfactory (MS)
- Google colab踩坑
- async await 在map中使用
- 网络命名空间
- Foxit pdf editor v10.1.8 green version
- Pytorch---使用Pytorch实现LinkNet进行语义分割
- Jerry added the process of turning off the touch module before turning it off [chapter]
- admas零件名重复
- Maidong Internet won the bid of Beijing life insurance
猜你喜欢
解读创客教育中的各类智能化组织发展
巅峰不止,继续奋斗!城链科技数字峰会于重庆隆重举行
仿ps样式js网页涂鸦板插件
Jmeter 之压测入门
Can be displayed in CAD but not displayed in print
搭建一个仪式感点满的网站,并内网穿透发布到公网 1/2
Detailed explanation of multi-mode input event distribution mechanism
Maidong Internet won the bid of Beijing life insurance
华为模拟器ensp的路由配置以及连通测试
uniapp 富文本编辑器使用
随机推荐
Vue cleans up the keepalive cache scheme in a timely manner
实战模拟│JWT 登录认证
软件开发过中的采购
__ init__ () missing 2 required positive arguments
Introduction to pressure measurement of JMeter
[buuctf.reverse] 151_ [FlareOn6]DnsChess
Detailed explanation of multi-mode input event distribution mechanism
基于OpenCV haarcascades的对象检测
[solution] paddlepaddle 2 X call static graph mode
UTF encoding and character set in golang
【1200. 最小绝对差】
CAD中能显示打印不显示
async await 在map中使用
杰理之AD 系列 MIDI 功能说明【篇】
Poster cover of glacier
Jerry added the process of turning off the touch module before turning it off [chapter]
Test case (TC)
Remember to build wheels repeatedly at one time (the setting instructions of obsidian plug-in are translated into Chinese)
华为ensp模拟器 配置ACL访问控制列表
解析steam教育中蕴含的众创空间