当前位置:网站首页>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;
}
}
边栏推荐
- 3D reconstruction open source code summary [keep updated]
- C # compare the differences between the two images
- scipy. misc. imread()
- Newton iterative method (solving nonlinear equations)
- Array, date, string object method
- Characteristic Engineering
- Confusing basic concepts member variables local variables global variables
- Meta tag details
- 什么是防火墙?防火墙基础知识讲解
- 2310. The number of bits is the sum of integers of K
猜你喜欢
![[code practice] [stereo matching series] Classic ad census: (6) multi step parallax optimization](/img/54/cb1373fbe7b21c5383580e8b638a2c.jpg)
[code practice] [stereo matching series] Classic ad census: (6) multi step parallax optimization

高性能Spark_transformation性能

Kotlin introductory notes (VIII) collection and traversal

Understanding rotation matrix R from the perspective of base transformation

Add discount recharge and discount shadow ticket plug-ins to the resource realization applet

Hosting environment API

Global configuration tabbar

Introduction Guide to stereo vision (2): key matrix (essential matrix, basic matrix, homography matrix)

Applet (global data sharing)

Can't find the activitymainbinding class? The pit I stepped on when I just learned databinding
随机推荐
2311. Longest binary subsequence less than or equal to K
Svgo v3.9.0+
Multiple solutions to one problem, asp Net core application startup initialization n schemes [Part 1]
Introduction Guide to stereo vision (6): level constraints and polar correction of fusiello method
np. allclose
Codeworks round 638 (Div. 2) cute new problem solution
Characteristic Engineering
OpenGL - Lighting
Kotlin introductory notes (VII) data class and singleton class
混淆矩阵(Confusion Matrix)
Wxss template syntax
.NET服务治理之限流中间件-FireflySoft.RateLimit
[code practice] [stereo matching series] Classic ad census: (4) cross domain cost aggregation
Kubedm series-00-overview
Kotlin introductory notes (IV) circular statements (simple explanation of while, for)
STM32简易多级菜单(数组查表法)
Rebuild my 3D world [open source] [serialization-2]
[beauty of algebra] solution method of linear equations ax=0
Talking about label smoothing technology
Kotlin introductory notes (II) a brief introduction to kotlin functions