当前位置:网站首页>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);
}
};
边栏推荐
- Bit mask of bit operation
- Configuration and startup of kubedm series-02-kubelet
- [jailhouse article] performance measurements for hypervisors on embedded ARM processors
- On the characteristics of technology entrepreneurs from Dijkstra's Turing Award speech
- 个人开发的渗透测试工具Satania v1.2更新
- Multi screen computer screenshots will cut off multiple screens, not only the current screen
- 多屏电脑截屏会把多屏连着截下来,而不是只截当前屏
- 智慧工地“水电能耗在线监测系统”
- Appium foundation - use the first demo of appium
- shared_ Repeated release heap object of PTR hidden danger
猜你喜欢
从Dijkstra的图灵奖演讲论科技创业者特点
On the characteristics of technology entrepreneurs from Dijkstra's Turing Award speech
Educational Codeforces Round 116 (Rated for Div. 2) E. Arena
【云原生】微服务之Feign自定义配置的记录
Full Permutation Code (recursive writing)
Fried chicken nuggets and fifa22
Data visualization chart summary (I)
Solution to game 10 of the personal field
Time of process
Sqlmap tutorial (II) practical skills I
随机推荐
leetcode-6108:解密消息
传统数据库逐渐“难适应”,云原生数据库脱颖而出
Educational codeforces round 109 (rated for Div. 2) C. robot collisions D. armchairs
Introduction et expérience de wazuh open source host Security Solution
1040 Longest Symmetric String
SQLMAP使用教程(二)实战技巧一
SQLMAP使用教程(一)
Error ora-28547 or ora-03135 when Navicat connects to Oracle Database
Appium automation test foundation - Summary of appium test environment construction
1041 Be Unique
【Jailhouse 文章】Jailhouse Hypervisor
【Rust 笔记】17-并发(上)
QQ computer version cancels escape character input expression
SPI 详解
个人开发的渗透测试工具Satania v1.2更新
884. Uncommon words in two sentences
CF1634 F. Fibonacci Additions
智慧工地“水电能耗在线监测系统”
Appium foundation - use the first demo of appium
【Jailhouse 文章】Performance measurements for hypervisors on embedded ARM processors