当前位置:网站首页>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;
}
}
边栏推荐
- asp. Net (c)
- Pearson correlation coefficient
- 图神经网络+对比学习,下一步去哪?
- Svg optimization by svgo
- Blogger article navigation (classified, real-time update, permanent top)
- 信息与熵,你想知道的都在这里了
- Return of missing persons
- Multiple solutions to one problem, asp Net core application startup initialization n schemes [Part 1]
- nodejs_ 01_ fs. readFile
- 嗨 FUN 一夏,与 StarRocks 一起玩转 SQL Planner!
猜你喜欢
![[ctfhub] Title cookie:hello guest only admin can get flag. (cookie spoofing, authentication, forgery)](/img/78/d9d1a66fc239e7c62de1fce426d30d.jpg)
[ctfhub] Title cookie:hello guest only admin can get flag. (cookie spoofing, authentication, forgery)

优先级队列(堆)

利用请求头开发多端应用

Svgo v3.9.0+

RT thread kernel quick start, kernel implementation and application development learning with notes

Blogger article navigation (classified, real-time update, permanent top)

C form click event did not respond

AUTOSAR从入门到精通100讲(103)-dbc文件的格式以及创建详解
![3D reconstruction open source code summary [keep updated]](/img/ec/984aede7ef9e758abd52fb5ff4e144.jpg)
3D reconstruction open source code summary [keep updated]

2020 "Lenovo Cup" National College programming online Invitational Competition and the third Shanghai University of technology programming competition
随机推荐
Newton iterative method (solving nonlinear equations)
OpenGL - Coordinate Systems
Mengxin summary of LIS (longest ascending subsequence) topics
生成对抗网络
Rebuild my 3D world [open source] [serialization-2]
Progressive JPEG pictures and related
什么是防火墙?防火墙基础知识讲解
Luo Gu p3177 tree coloring [deeply understand the cycle sequence of knapsack on tree]
【ManageEngine】如何利用好OpManager的报表功能
Introduction Guide to stereo vision (1): coordinate system and camera parameters
Driver's license physical examination hospital (114-2 hang up the corresponding hospital driver physical examination)
Multiple solutions to one problem, asp Net core application startup initialization n schemes [Part 1]
嗨 FUN 一夏,与 StarRocks 一起玩转 SQL Planner!
2311. 小于等于 K 的最长二进制子序列
顶会论文看图对比学习(GNN+CL)研究趋势
Golang foundation - the time data inserted by golang into MySQL is inconsistent with the local time
Ecmascript6 introduction and environment construction
Generate confrontation network
Return of missing persons
Kotlin introductory notes (IV) circular statements (simple explanation of while, for)