当前位置:网站首页>LeetCode 556. 下一个更大元素 III
LeetCode 556. 下一个更大元素 III
2022-07-05 09:16:00 【Sasakihaise_】
【分析】这个和31. 下一个排列是一样的。
但是需要注意超int范围的情况
class Solution {
public int nextGreaterElement(int n) {
char[] s = String.valueOf(n).toCharArray();
int m = s.length;
if (m == 1) {
return -1;
}
int i, j;
for (i = m - 2; i >= 0; i--) {
if (s[i] < s[i + 1]) {
break;
}
}
if (i == -1) {
return -1;
}
for (j = m - 1; j > i; j--) {
if (s[j] > s[i]) {
break;
}
}
char t = s[i];
s[i] = s[j];
s[j] = t;
Arrays.sort(s, i + 1, m);
long ans = Long.parseLong(String.valueOf(s));
if (ans > ((long)1 << 31) - 1) return -1;
return (int)ans;
}
}
边栏推荐
- Talking about label smoothing technology
- C # compare the differences between the two images
- Explain NN in pytorch in simple terms CrossEntropyLoss
- Progressive JPEG pictures and related
- 太不好用了,长文章加图文,今后只写小短文
- OpenGL - Lighting
- Wxml template syntax
- AdaBoost use
- Kotlin introductory notes (VI) interface and function visibility modifiers
- Ecmascript6 introduction and environment construction
猜你喜欢
c语言指针深入理解
Introduction Guide to stereo vision (6): level constraints and polar correction of fusiello method
一文详解图对比学习(GNN+CL)的一般流程和最新研究趋势
[technical school] spatial accuracy of binocular stereo vision system: accurate quantitative analysis
Solution to the problems of the 17th Zhejiang University City College Program Design Competition (synchronized competition)
Information and entropy, all you want to know is here
Creation and reference of applet
NIPS2021 | 超越GraphCL,GNN+对比学习的节点分类新SOTA
Wxss template syntax
优先级队列(堆)
随机推荐
Alibaba cloud sends SMS verification code
【ManageEngine】如何利用好OpManager的报表功能
Kotlin introductory notes (I) kotlin variables and non variables
我的一生.
阿里云发送短信验证码
Uni app implements global variables
Applet (use of NPM package)
Oracle advanced (III) detailed explanation of data dictionary
Meta tag details
Introduction Guide to stereo vision (3): Zhang calibration method of camera calibration [ultra detailed and worthy of collection]
Ministry of transport and Ministry of Education: widely carry out water traffic safety publicity and drowning prevention safety reminders
12. Dynamic link library, DLL
Summary and Reflection on issues related to seq2seq, attention and transformer in hands-on deep learning
优先级队列(堆)
Codeworks round 639 (Div. 2) cute new problem solution
Nodejs modularization
asp. Net (c)
Mengxin summary of LIS (longest ascending subsequence) topics
c语言指针深入理解
Kotlin introductory notes (VI) interface and function visibility modifiers