当前位置:网站首页>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;
}
};
边栏推荐
- minidom 模塊寫入和解析 XML
- 杰理之AD 系列 MIDI 功能说明【篇】
- In the release version, the random white screen does not display the content after opening the shutter
- redis RDB AOF
- Hwinfo hardware detection tool v7.26 green version
- 数十亿公民信息遭泄漏!公有云上的数据安全还有“救”吗?
- Huawei ENSP simulator realizes communication security (switch)
- vim异步问题
- 冰河的海报封面
- TweenMax表情按钮js特效
猜你喜欢
Word文档中标题前面的黑点如何去掉
TweenMax表情按钮js特效
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??
杰理之AD 系列 MIDI 功能说明【篇】
render函数与虚拟dom
Render function and virtual DOM
Routing configuration and connectivity test of Huawei simulator ENSP
At the right time, the Guangzhou station of the city chain science and Technology Strategy Summit was successfully held
搭建一个仪式感点满的网站,并内网穿透发布到公网 1/2
shp数据制作3DTiles白膜
随机推荐
每日一题-LeetCode1200-最小绝对差-数组-排序
LeetCode 8. String conversion integer (ATOI)
acwing 3302. Expression evaluation
杰理之AD 系列 MIDI 功能说明【篇】
网件r7000梅林系统5g不稳定 5g信号经常掉线解决方法
仿ps样式js网页涂鸦板插件
Use of redis publish subscription
Foxit pdf editor v10.1.8 green version
Redis transaction
创客思维在高等教育中的启迪作用
In the release version, the random white screen does not display the content after opening the shutter
【微服务|SCG】Predicate的使用
Introduction to pressure measurement of JMeter
shp数据制作3DTiles白膜
colResizable.js自动调整表格宽度插件
UTF encoding and character set in golang
__init__() missing 2 required positional arguments 不易查明的继承错误
Jerry's ad series MIDI function description [chapter]
minidom 模塊寫入和解析 XML
基于OpenCV haarcascades的对象检测