当前位置:网站首页>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;
}
}
边栏推荐
- Newton iterative method (solving nonlinear equations)
- 驾驶证体检医院(114---2 挂对应的医院司机体检)
- Ministry of transport and Ministry of Education: widely carry out water traffic safety publicity and drowning prevention safety reminders
- Configuration and startup of kubedm series-02-kubelet
- 利用请求头开发多端应用
- 2310. The number of bits is the sum of integers of K
- 信息與熵,你想知道的都在這裏了
- What is a firewall? Explanation of basic knowledge of firewall
- Kotlin introductory notes (VII) data class and singleton class
- Nodejs modularization
猜你喜欢
Add discount recharge and discount shadow ticket plug-ins to the resource realization applet
Kotlin introductory notes (VIII) collection and traversal
Global configuration tabbar
Applet (global data sharing)
fs. Path module
编辑器-vi、vim的使用
Rebuild my 3D world [open source] [serialization-1]
3D reconstruction open source code summary [keep updated]
Kotlin introductory notes (V) classes and objects, inheritance, constructors
Creation and reference of applet
随机推荐
Transfer learning and domain adaptation
Introduction Guide to stereo vision (3): Zhang calibration method of camera calibration [ultra detailed and worthy of collection]
Kotlin introductory notes (I) kotlin variables and non variables
Kotlin introductory notes (IV) circular statements (simple explanation of while, for)
Confusion matrix
Shutter uses overlay to realize global pop-up
nodejs_ 01_ fs. readFile
Applet global style configuration window
我的一生.
2309. The best English letters with both upper and lower case
. Net service governance flow limiting middleware -fireflysoft RateLimit
Introduction Guide to stereo vision (7): stereo matching
2310. 个位数字为 K 的整数之和
Rebuild my 3D world [open source] [serialization-2]
AUTOSAR从入门到精通100讲(103)-dbc文件的格式以及创建详解
The location search property gets the login user name
Summary of "reversal" problem in challenge Programming Competition
Introduction Guide to stereo vision (6): level constraints and polar correction of fusiello method
Kotlin introductory notes (II) a brief introduction to kotlin functions
[code practice] [stereo matching series] Classic ad census: (5) scan line optimization