当前位置:网站首页>556. 下一个更大元素 III
556. 下一个更大元素 III
2022-07-03 13:43:00 【anieoo】
原题链接:556. 下一个更大元素 III
solution:
class Solution {
public:
int nextGreaterElement(int n) {
string s = to_string(n); //将数字编程字符串处理
int k = s.size() - 1;
while(k > 0 && 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++; //从k + 1开始,找打第一个比s[k - 1]大的数字
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;
}
};
边栏推荐
- QT learning 19 standard dialog box in QT (top)
- How to bold text in AI
- 全局事件总线
- UiO-66-COOH装载苯达莫司汀|羟基磷灰石( HA) 包裹MIL-53(Fe)纳米粒子|装载黄芩苷锰基金属有机骨架材料
- 可编程逻辑器件软件测试
- protobuf与grpc
- 小项目(servelt+jsp+mysql+EL+JSTL)完成一个登录功能的Servlet,具有增删改查的操作。实现登录身份验证,防止非法登录,防止多点登录,记住用户名密码功能。
- Understanding of closures
- Function calling convention
- Common mixins
猜你喜欢
金属有机骨架MOFs装载非甾体类抗炎药物|ZIF-8包裹普鲁士蓝负载槲皮素(制备方法)
天图投资冲刺港股:资产管理规模249亿 投了小红书与奈雪
Exercise 9-3 plane vector addition
Programmable logic device software testing
Qt学习21 Qt 中的标准对话框(下)
好看、好用、强大的手写笔记软件综合评测:Notability、GoodNotes、MarginNote、随手写、Notes Writers、CollaNote、CollaNote、Prodrafts、Noteshelf、FlowUs、OneNote、苹果备忘录
牛客网:过河卒
中感微冲刺科创板:年营收2.4亿净亏1782万 拟募资6亿
Redis:字符串类型数据的操作命令
QT learning 21 standard dialog box in QT (Part 2)
随机推荐
1px problem of mobile terminal
QT learning 25 layout manager (4)
How to use lxml to judge whether the website announcement is updated
Configure stylelint
核酸修饰的金属有机框架药物载体|PCN-223金属有机骨架包载Ad金刚烷|ZIF-8包裹阿霉素(DOX)
Nucleic acid modified metal organic framework drug carrier | pcn-223 metal organic framework encapsulated ad adamantane | zif-8 encapsulated adriamycin (DOX)
Exercise 10-3 recursive implementation of exponential functions
Learn to punch in today
Print. JS -- web page file printing
JVM垃圾回收机
QT learning 19 standard dialog box in QT (top)
Exercise 10-1 judge the three digits that meet the conditions
Analysis of the characteristics of page owner
28:第三章:开发通行证服务:11:在配置文件中定义属性,然后在代码中去获取;
Global event bus
JS new challenges
Exercise 10-1 calculate the sum of 1 to n using recursive functions
Dlopen() implements dynamic loading of third-party libraries
Raft 协议
Folic acid modified metal organic framework (zif-8) baicalin loaded metal organic framework composite magnetic material (AU- [email