当前位置:网站首页>Leetcode-556: the next larger element III
Leetcode-556: the next larger element III
2022-07-05 06:09:00 【Chrysanthemum headed bat】
leetcode-556: Next bigger element III
subject
Give you a positive integer n , Please find the smallest integer that meets the conditions , It consists of rearranging n Each number present in the consists of , And its value is greater than n . If there is no such positive integer , Then return to -1 .
Be careful , The returned integer should be a 32 An integer , If there is an answer that satisfies the meaning of the question , But it's not 32 An integer , Also return to -1 .
Example 1:
Input :n = 12
Output :21
Example 2:
Input :n = 21
Output :-1
Problem solving
and leetcode-31: Next spread Same idea , Just based on it , Excessive overflow judgment
Method 1 :
class Solution {
public:
// Overflow judgment
bool isValidInt(string& s){
string maxS=to_string(INT_MAX);
if(s.size()<maxS.size()) return true;
for(int i=0;i<s.size();i++){
if(s[i]>maxS[i]) return false;
else if(s[i]==maxS[i]) continue;
else if(s[i]<maxS[i]) return true;
}
return true;
}
int nextGreaterElement(int n) {
string s=to_string(n);
int len=s.size();
int i=len-2,j=len-1;
while(i>=0&&s[i]>=s[j]){
i--;
j--;
}
if(i<0) return -1;
int k=len-1;
while(s[i]>=s[k]){
k--;
}
swap(s[i],s[k]);
sort(s.begin()+i+1,s.end());
if(!isValidInt(s)) return -1;
else return stoi(s);
}
};
边栏推荐
- 2022年貴州省職業院校技能大賽中職組網絡安全賽項規程
- 2022年贵州省职业院校技能大赛中职组网络安全赛项规程
- MIT-6874-Deep Learning in the Life Sciences Week 7
- The sum of the unique elements of the daily question
- Introduction to convolutional neural network
- 2022 极术通讯-Arm 虚拟硬件加速物联网软件开发
- R language [import and export of dataset]
- Educational codeforces round 109 (rated for Div. 2) C. robot collisions D. armchairs
- Daily question 2006 Number of pairs whose absolute value of difference is k
- leetcode-6110:网格图中递增路径的数目
猜你喜欢
LeetCode 0108.将有序数组转换为二叉搜索树 - 数组中值为根,中值左右分别为左右子树
SQLMAP使用教程(二)实战技巧一
Introduction and experience of wazuh open source host security solution
【Jailhouse 文章】Jailhouse Hypervisor
Sqlmap tutorial (1)
SQLMAP使用教程(一)
Smart construction site "hydropower energy consumption online monitoring system"
SPI details
liunx启动redis
Dynamic planning solution ideas and summary (30000 words)
随机推荐
做 SQL 性能优化真是让人干瞪眼
1996. number of weak characters in the game
redis发布订阅命令行实现
Educational codeforces round 109 (rated for Div. 2) C. robot collisions D. armchairs
【Rust 笔记】16-输入与输出(上)
Wazuh開源主機安全解决方案的簡介與使用體驗
LVS简介【暂未完成(半成品)】
On the characteristics of technology entrepreneurs from Dijkstra's Turing Award speech
Daily question 2013 Detect square
Smart construction site "hydropower energy consumption online monitoring system"
Dynamic planning solution ideas and summary (30000 words)
[practical skills] technical management of managers with non-technical background
[jailhouse article] look mum, no VM exits
打印机脱机时一种容易被忽略的原因
[rust notes] 14 set (Part 2)
Matrixdb V4.5.0 was launched with a new mars2 storage engine!
中职网络安全技能竞赛——广西区赛中间件渗透测试教程文章
Doing SQL performance optimization is really eye-catching
2022 pole technology communication arm virtual hardware accelerates the development of Internet of things software
Typical use cases for knapsacks, queues, and stacks