当前位置:网站首页>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);
}
};
边栏推荐
- Daily question 1342 Number of operations to change the number to 0
- Groupbykey() and reducebykey() and combinebykey() in spark
- Over fitting and regularization
- leetcode-556:下一个更大元素 III
- Real time clock (RTC)
- Control unit
- Simple knapsack, queue and stack with deque
- Bit mask of bit operation
- 2022年貴州省職業院校技能大賽中職組網絡安全賽項規程
- R language [import and export of dataset]
猜你喜欢
[jailhouse article] look mum, no VM exits
AtCoder Grand Contest 013 E - Placing Squares
【实战技能】非技术背景经理的技术管理
On the characteristics of technology entrepreneurs from Dijkstra's Turing Award speech
可变电阻器概述——结构、工作和不同应用
Introduction et expérience de wazuh open source host Security Solution
1.14 - 流水线
CF1634E Fair Share
Scope of inline symbol
Personal developed penetration testing tool Satania v1.2 update
随机推荐
F - Two Exam(AtCoder Beginner Contest 238)
Bit mask of bit operation
Codeforces Round #716 (Div. 2) D. Cut and Stick
【Rust 笔记】14-集合(下)
R language [import and export of dataset]
Convolution neural network -- convolution layer
2022 pole technology communication arm virtual hardware accelerates the development of Internet of things software
可变电阻器概述——结构、工作和不同应用
Scope of inline symbol
Configuration and startup of kubedm series-02-kubelet
leetcode-22:括号生成
leetcode-31:下一个排列
Sqlmap tutorial (II) practical skills I
js快速将json数据转换为url参数
Traditional databases are gradually "difficult to adapt", and cloud native databases stand out
[jailhouse article] jailhouse hypervisor
SPI details
数据可视化图表总结(二)
[practical skills] how to do a good job in technical training?
Error ora-28547 or ora-03135 when Navicat connects to Oracle Database