当前位置:网站首页>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);
}
};
边栏推荐
- Wazuh開源主機安全解决方案的簡介與使用體驗
- In this indifferent world, light crying
- LeetCode 0107.二叉树的层序遍历II - 另一种方法
- MIT-6874-Deep Learning in the Life Sciences Week 7
- leetcode-6108:解密消息
- Convolution neural network -- convolution layer
- 1996. number of weak characters in the game
- Appium foundation - use the first demo of appium
- CPU内核和逻辑处理器的区别
- leetcode-6110:网格图中递增路径的数目
猜你喜欢

数据可视化图表总结(一)

7. Processing the input of multidimensional features

1.13 - RISC/CISC

Wazuh開源主機安全解决方案的簡介與使用體驗

Appium基础 — 使用Appium的第一个Demo
![[cloud native] record of feign custom configuration of microservices](/img/39/05cf7673155954c90e75a8a2eecd96.jpg)
[cloud native] record of feign custom configuration of microservices

CF1634 F. Fibonacci Additions

Introduction and experience of wazuh open source host security solution

SQLMAP使用教程(一)

Data visualization chart summary (II)
随机推荐
【云原生】微服务之Feign自定义配置的记录
Appium自动化测试基础 — Appium测试环境搭建总结
CPU内核和逻辑处理器的区别
Transform optimization problems into decision-making problems
Data visualization chart summary (I)
Appium基础 — 使用Appium的第一个Demo
SQLMAP使用教程(二)实战技巧一
【Rust 笔记】17-并发(下)
中职网络安全技能竞赛——广西区赛中间件渗透测试教程文章
2017 USP Try-outs C. Coprimes
多屏电脑截屏会把多屏连着截下来,而不是只截当前屏
Educational codeforces round 109 (rated for Div. 2) C. robot collisions D. armchairs
Control unit
【Rust 笔记】13-迭代器(中)
Common optimization methods
Real time clock (RTC)
leetcode-556:下一个更大元素 III
2020ccpc Qinhuangdao J - Kingdom's power
One question per day 1447 Simplest fraction
redis发布订阅命令行实现