当前位置:网站首页>556. The next larger element III
556. The next larger element III
2022-07-03 14:22:00 【anieoo】
Original link :556. Next bigger element III
solution:
class Solution {
public:
int nextGreaterElement(int n) {
string s = to_string(n); // Process numeric programming strings
int k = s.size() - 1;
while(k > 0 && s[k - 1] >= s[k]) k--; // From back to front, if it is in ascending order, it is the largest number
if(!k) return -1; // If the whole string is in ascending order from back to front , Nothing is bigger than it
int t = k;
while(t + 1 < s.size() && s[t + 1] > s[k - 1]) t++; // from k + 1 Start , Find the first match s[k - 1] Big numbers
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;
}
};
边栏推荐
- Sendmail无法发送邮件及发送过慢解决
- 使用并行可微模拟加速策略学习
- 7-22 tortoise and rabbit race (result oriented)
- 7-11 calculation of residential water charges by sections
- Exercise 9-3 plane vector addition
- Exercise 8-7 string sorting
- Exercise 10-2 recursive factorial sum
- The mail function of LNMP environment cannot send mail
- [Jilin University] information sharing of postgraduate entrance examination and re examination
- Exercise 10-1 judge the three digits that meet the conditions
猜你喜欢
Understand the application scenario and implementation mechanism of differential segment
好看、好用、强大的手写笔记软件综合评测:Notability、GoodNotes、MarginNote、随手写、Notes Writers、CollaNote、CollaNote、Prodrafts、Noteshelf、FlowUs、OneNote、苹果备忘录
Exercise 10-8 recursive implementation of sequential output of integers
Exercise 10-2 recursive factorial sum
Why is this error reported when modifying records in the database
Scroll detection, so that the content in the lower right corner is not displayed at the top of the page, but is displayed as the mouse slides
concat和concat_ws()区别及group_concat()和repeat()函数的使用
编程语言:类型系统的本质
愉悦资本新双币基金近40亿元完成首次关账
Redis:Redis的数据结构、key的操作命令
随机推荐
Fabric. JS document
Reflection -- basic usage
JS Part III
Sendmail can't send mail and it's too slow to send. Solve it
simpleParallax. JS (create poor visual effects for website pictures)
Doris学习笔记之数据表的创建
Simulated access
7-4 BCD decryption (10 points)
7-19 check denomination (solve binary linear equation)
Understand the application scenario and implementation mechanism of differential segment
Current situation, analysis and prediction of information and innovation industry
Exercise 8-8 moving letters
7-17 crawling worms (break exercise)
Onmenusharetimeline custom shared content is invalid, and the title and icon are not displayed
Redis:字符串類型數據的操作命令
js . Find the first palindrome string in the array
JS get DPI, PX to cm, cm to PX
Scroll detection, so that the content in the lower right corner is not displayed at the top of the page, but is displayed as the mouse slides
js 2023. String pair equal to the target string after connection
Comprehensive evaluation of good-looking, easy-to-use and powerful handwriting note taking software: notability, goodnotes, marginnote, handwriting, notes writers, collanote, collanote, prodrafts, not