当前位置:网站首页>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

Render function and virtual DOM

WGCNA分析基本教程总结

巅峰不止,继续奋斗!城链科技数字峰会于重庆隆重举行

迈动互联中标北京人寿保险

【微信小程序】协同工作与发布

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

torch. Tensor and torch The difference between tensor

Introduction to pressure measurement of JMeter

ApplicationContext 与 BeanFactory 区别(MS)
随机推荐
Liu Jincheng won the 2022 China e-commerce industry innovation Figure Award
render函数与虚拟dom
Introduction to pressure measurement of JMeter
五子棋 上班摸鱼工具 可局域网/人机
[solution] paddlepaddle 2 X call static graph mode
Render function and virtual DOM
福昕PDF编辑器v10.1.8绿色版
redis管道
解析steam教育中蕴含的众创空间
torch.tensor和torch.Tensor的区别
Can be displayed in CAD but not displayed in print
redis03——Redis的网络配置与心跳机制
Jerry's ad series MIDI function description [chapter]
Hwinfo hardware detection tool v7.26 green version
A quick start to fastdfs takes you three minutes to upload and download files to the ECS
创客思维在高等教育中的启迪作用
IIC (STM32)
In the release version, the random white screen does not display the content after opening the shutter
2021 CCPC Harbin B. magical subsequence (thinking question)
maya灯建模
