当前位置:网站首页>2022.7.3-----leetcode.556
2022.7.3-----leetcode.556
2022-07-04 05:53:00 【路Lu727】
public int nextGreaterElement(int n) {
if(n<10) return -1;
int num=n;//保存原数
//将数字倒序拆分
int idx=0;
int[] arr=new int[String.valueOf(n).length()];
while(n>0){
arr[idx++]=n%10;
n/=10;
}
//从低位开始在[0,i-1]范围寻找大于arr[i]的最小数字替换arr[i],再将后面从大到小排序
for(int i=1;i<arr.length;i++){
int k=0;//最小数索引
int min=10;//大于arr[i]的最小值
for(int j=0;j<i;j++){
if(arr[j]>arr[i]&&arr[j]<min){
min=arr[j];
k=j;
}
}
if(min==10) continue;//找不到,说明该位后面一定有序
//与arr[i]交换
int tmp1=arr[k];
arr[k]=arr[i];
arr[i]=tmp1;
//将后面的数排序
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;//找到了就返回
}
//合成
long ans=0;
for(int i=arr.length-1;i>=0;i--){
ans=ans*10+arr[i];
}
//判断是否合法
if(ans==num||ans>Integer.MAX_VALUE) return -1;
return (int)ans;
}
边栏推荐
- 如何获取el-tree中所有节点的父节点
- 谷歌 Chrome 浏览器将支持选取文字翻译功能
- Excel 比较日器
- 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
- C # character similarity comparison general class
- tutle时钟改进版
- My NVIDIA developer journey - optimizing graphics card performance
- Arc135 C (the proof is not very clear)
- BUU-Pwn-test_ your_ nc
- JS扁平化数形结构的数组
猜你喜欢
Steady! Huawei micro certification Huawei cloud computing service practice is stable!
What is MQ?
724. Find the central subscript of the array
QT 获取随机颜色值设置label背景色 代码
Leetcode question brushing record | 206_ Reverse linked list
js如何将秒转换成时分秒显示
[MySQL practice of massive data with high concurrency, high performance and high availability -8] - transaction isolation mechanism of InnoDB
SQL injection - injection based on MSSQL (SQL Server)
LayoutManager布局管理器:FlowLayout、BorderLayout、GridLayout、GridBagLayout、CardLayout、BoxLayout
Kubernets first meeting
随机推荐
VB. Net calls ffmpeg to simply process video (class Library-6)
BUU-Real-[PHP]XXE
如何判断数组中是否含有某个元素
【雕爷学编程】Arduino动手做(105)---压电陶瓷振动模块
Grounding relay dd-1/60
ANSYS command
FreeRTOS 中 RISC-V-Qemu-virt_GCC 的 锁机制 分析
检漏继电器JY82-2P
Penetration tool - sqlmap
体验碎周报第 102 期(2022.7.4)
1.1 history of Statistics
Risc-v-qemu-virt in FreeRTOS_ Lock mechanism analysis of GCC
XII Golang others
How to clone objects
fastjson
Uninstall Google drive hard drive - you must exit the program to uninstall
left_ and_ right_ Net normal version
Install pytoch geometric
70000 words of detailed explanation of the whole process of pad openvino [CPU] - from environment configuration to model deployment
Leetcode question brushing record | 206_ Reverse linked list