当前位置:网站首页>每日一题-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;
}
};
边栏推荐
猜你喜欢
MySQL --- 数据库查询 - 聚合函数的使用、聚合查询、分组查询
RFID仓库管理系统解决方案有哪些功能模块
How does wincc7.5 SP1 find variables and their positions through cross indexing?
华为ensp模拟器 DNS服务器的配置
偷窃他人漏洞报告变卖成副业,漏洞赏金平台出“内鬼”
NetWare r7000 Merlin system virtual memory creation failed, prompting that the USB disk reading and writing speed does not meet the requirements. Solution, is it necessary to create virtual memory??
[server data recovery] a case of RAID5 data recovery stored in a brand of server
黄金k线图中的三角形有几种?
Day24:文件系统
PS vertical English and digital text how to change direction (vertical display)
随机推荐
搭建一个仪式感点满的网站,并内网穿透发布到公网 1/2
Huawei simulator ENSP common commands
Go notes (3) usage of go language FMT package
[micro service SCG] use of predict
redis缓存
PS vertical English and digital text how to change direction (vertical display)
acwing 3302. 表达式求值
VIM asynchronous problem
华为模拟器ensp的路由配置以及连通测试
admas零件名重复
Configuration of DNS server of Huawei ENSP simulator
偷窃他人漏洞报告变卖成副业,漏洞赏金平台出“内鬼”
2021 CCPC 哈尔滨 B. Magical Subsequence(思维题)
Y56. Chapter III kubernetes from entry to proficiency -- business image version upgrade and rollback (29)
y56.第三章 Kubernetes从入门到精通 -- 业务镜像版本升级及回滚(二九)
黄金k线图中的三角形有几种?
华为ensp模拟器 DNS服务器的配置
Idea plug-in
杰理之AD 系列 MIDI 功能说明【篇】
LeetCode 8. String conversion integer (ATOI)