当前位置:网站首页>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);
}
};
边栏推荐
- Introduction to LVS [unfinished (semi-finished products)]
- Appium automation test foundation - Summary of appium test environment construction
- Smart construction site "hydropower energy consumption online monitoring system"
- Appium基础 — 使用Appium的第一个Demo
- [jailhouse article] performance measurements for hypervisors on embedded ARM processors
- Codeforces Round #732 (Div. 2) D. AquaMoon and Chess
- leetcode-6111:螺旋矩阵 IV
- 【Rust 笔记】15-字符串与文本(上)
- Simple knapsack, queue and stack with deque
- QT判断界面当前点击的按钮和当前鼠标坐标
猜你喜欢
传统数据库逐渐“难适应”,云原生数据库脱颖而出
Overview of variable resistors - structure, operation and different applications
Wazuh開源主機安全解决方案的簡介與使用體驗
[practical skills] how to do a good job in technical training?
从Dijkstra的图灵奖演讲论科技创业者特点
【云原生】微服务之Feign自定义配置的记录
Solution to game 10 of the personal field
实时时钟 (RTC)
个人开发的渗透测试工具Satania v1.2更新
LeetCode 0107.二叉树的层序遍历II - 另一种方法
随机推荐
“磐云杯”中职网络安全技能大赛A模块新题
Wazuh开源主机安全解决方案的简介与使用体验
[practical skills] technical management of managers with non-technical background
API related to TCP connection
SQLMAP使用教程(二)实战技巧一
Introduction and experience of wazuh open source host security solution
leetcode-6109:知道秘密的人数
1039 Course List for Student
Overview of variable resistors - structure, operation and different applications
【实战技能】如何做好技术培训?
【Jailhouse 文章】Jailhouse Hypervisor
Sqlmap tutorial (II) practical skills I
PC register
927. Trisection simulation
CF1637E Best Pair
leetcode-6110:网格图中递增路径的数目
QT判断界面当前点击的按钮和当前鼠标坐标
Educational Codeforces Round 116 (Rated for Div. 2) E. Arena
Daily question 1342 Number of operations to change the number to 0
从Dijkstra的图灵奖演讲论科技创业者特点