当前位置:网站首页>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;
}
}
边栏推荐
- OpenGL - Lighting
- 交通运输部、教育部:广泛开展水上交通安全宣传和防溺水安全提醒
- STM32简易多级菜单(数组查表法)
- Composition of applet code
- Kotlin introductory notes (VI) interface and function visibility modifiers
- Summary and Reflection on issues related to seq2seq, attention and transformer in hands-on deep learning
- Kotlin introductory notes (IV) circular statements (simple explanation of while, for)
- Array, date, string object method
- 深入浅出PyTorch中的nn.CrossEntropyLoss
- Codeworks round 639 (Div. 2) cute new problem solution
猜你喜欢

nodejs_ 01_ fs. readFile

一文详解图对比学习(GNN+CL)的一般流程和最新研究趋势
![Rebuild my 3D world [open source] [serialization-2]](/img/e6/aad5f432aca619b992753187729dcf.jpg)
Rebuild my 3D world [open source] [serialization-2]

Applet global style configuration window

Applet network data request
![C [essential skills] use of configurationmanager class (use of file app.config)](/img/8b/e56f87c2d0fbbb1251ec01b99204a1.png)
C [essential skills] use of configurationmanager class (use of file app.config)
![一题多解,ASP.NET Core应用启动初始化的N种方案[上篇]](/img/c4/27ae0d259abc4e61286c1f4d90c06a.png)
一题多解,ASP.NET Core应用启动初始化的N种方案[上篇]

Introduction Guide to stereo vision (7): stereo matching

Applet (use of NPM package)

Codeworks round 639 (Div. 2) cute new problem solution
随机推荐
Wechat H5 official account to get openid climbing account
[code practice] [stereo matching series] Classic ad census: (5) scan line optimization
混淆矩阵(Confusion Matrix)
[code practice] [stereo matching series] Classic ad census: (6) multi step parallax optimization
Can't find the activitymainbinding class? The pit I stepped on when I just learned databinding
Introduction Guide to stereo vision (2): key matrix (essential matrix, basic matrix, homography matrix)
2309. The best English letters with both upper and lower case
[ctfhub] Title cookie:hello guest only admin can get flag. (cookie spoofing, authentication, forgery)
notepad++
Introduction Guide to stereo vision (3): Zhang calibration method of camera calibration [ultra detailed and worthy of collection]
Mengxin summary of LIS (longest ascending subsequence) topics
Return of missing persons
C # draw Bezier curve with control points for lattice images and vector graphics
np. allclose
浅谈Label Smoothing技术
Creation and reference of applet
Summary and Reflection on issues related to seq2seq, attention and transformer in hands-on deep learning
Multiple solutions to one problem, asp Net core application startup initialization n schemes [Part 1]
基于STM32单片机的测温仪(带人脸检测)
How many checks does kubedm series-01-preflight have