当前位置:网站首页>2022.7.3-----leetcode. five hundred and fifty-six
2022.7.3-----leetcode. five hundred and fifty-six
2022-07-04 05:55:00 【Lu 727】
public int nextGreaterElement(int n) {
if(n<10) return -1;
int num=n;// Save original number
// Split the numbers in reverse order
int idx=0;
int[] arr=new int[String.valueOf(n).length()];
while(n>0){
arr[idx++]=n%10;
n/=10;
}
// Start from the low position at [0,i-1] The range search is greater than arr[i] Replace with the smallest number of arr[i], Then sort the following from large to small
for(int i=1;i<arr.length;i++){
int k=0;// Decimal index
int min=10;// Greater than arr[i] The minimum value of
for(int j=0;j<i;j++){
if(arr[j]>arr[i]&&arr[j]<min){
min=arr[j];
k=j;
}
}
if(min==10) continue;// Can't find , It means that this bit must be followed by order
// And arr[i] In exchange for
int tmp1=arr[k];
arr[k]=arr[i];
arr[i]=tmp1;
// Sort the following numbers
for(int l=i-1;l>0;l--){
boolean ex=false;
for(int j=0;j<l;j++){
if(arr[j]<arr[l]){
int tmp2=arr[j];
arr[j]=arr[l];
arr[l]=tmp2;
ex=true;
}
}
if(!ex) break;
}
break;// Go back when you find it
}
// synthesis
long ans=0;
for(int i=arr.length-1;i>=0;i--){
ans=ans*10+arr[i];
}
// Judge whether it is legal
if(ans==num||ans>Integer.MAX_VALUE) return -1;
return (int)ans;
}
边栏推荐
- JS扁平化数形结构的数组
- Upper computer software development - log information is stored in the database based on log4net
- BeanFactoryPostProcessor 与 BeanPostProcessor 相关子类概述
- input显示当前选择的图片
- 检漏继电器JY82-2P
- Design and implementation of redis 7.0 multi part AOF
- MySQL information_ Schema database
- My NVIDIA developer journey - optimizing graphics card performance
- JS execution mechanism
- Online shrimp music will be closed in January next year. Netizens call No
猜你喜欢
724. Find the central subscript of the array
Zhanrui tankbang | jointly build, cooperate and win-win zhanrui core ecology
云原生架构实战案例及优化解决方案
Leakage detection relay jy82-2p
如何避免 JVM 内存泄漏?
Yiwen unlocks Huawei's new cloud skills - the whole process of aiot development [device access - ESP end-to-side data collection [mqtt]- real time data analysis] (step-by-step screenshot is more detai
19. Framebuffer application programming
BUU-Crypto-[GXYCTF2019]CheckIn
input显示当前选择的图片
测试岗的中年危机该如何选择?是坚守还是另寻出路?且看下文
随机推荐
BUU-Crypto-Cipher
Online shrimp music will be closed in January next year. Netizens call No
How to configure static IP for Kali virtual machine
卸载Google Drive 硬盘-必须退出程序才能卸载
Zhanrui tankbang | jointly build, cooperate and win-win zhanrui core ecology
Error CVC complex type 2.4. a: Invalid content beginning with element 'base extension' was found. Should start with one of '{layoutlib}'.
Canoe panel learning video
FRP intranet penetration, reverse proxy
安装 Pytorch geometric
JS get the attribute values nested in the object
Kubernets first meeting
Talk about the SQL server version of DTM sub transaction barrier function
Layoutmanager layout manager: flowlayout, borderlayout, GridLayout, gridbaglayout, CardLayout, BoxLayout
BeanFactoryPostProcessor 与 BeanPostProcessor 相关子类概述
MySQL information_ Schema database
实用的小工具指令
注释与注解
APScheduler如何设置任务不并发(即第一个任务执行完再执行下一个)?
Leetcode 184 Employees with the highest wages in the Department (July 3, 2022)
Qt发布多语言国际化翻译