当前位置:网站首页>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;
}边栏推荐
- Basic concept of bus
- js arguments参数使用和详解
- 总线的基本概念
- buuctf-pwn write-ups (8)
- Uninstall Google drive hard drive - you must exit the program to uninstall
- 【微服务】Nacos集群搭建以及加载文件配置
- BUU-Reverse-easyre
- 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
- 70000 words of detailed explanation of the whole process of pad openvino [CPU] - from environment configuration to model deployment
- el-select如何实现懒加载(带搜索功能)
猜你喜欢

Leakage detection relay jy82-2p

VB. Net GIF (making and disassembling - optimizing code, class library - 5)

报错cvc-complex-type.2.4.a: 发现了以元素 ‘base-extension‘ 开头的无效内容。应以 ‘{layoutlib}‘ 之一开头。

Detectron:训练自己的数据集——将自己的数据格式转换成COCO格式

win10清除快速访问-不留下痕迹

Configure cross compilation tool chain and environment variables

ANSYS command

剑指 Offer II 038. 每日温度

JS flattened array of number shape structure

BeanFactoryPostProcessor 与 BeanPostProcessor 相关子类概述
随机推荐
JS arguments parameter usage and explanation
BUU-Crypto-Cipher
QT 获取随机颜色值设置label背景色 代码
Excel 比较日器
left_and_right_net正常版本
Nexus 6p从8.0降级6.0+root
FreeRTOS 中 RISC-V-Qemu-virt_GCC 的 锁机制 分析
How to configure static IP for Kali virtual machine
Signification des lettres du module optique et abréviation des paramètres Daquan
JS flattened array of number shape structure
Halcon图片标定,使得后续图片处理过后变成与模板图片一样
每周小结(*63):关于正能量
1480. 一维数组的动态和
fastjson
LayoutManager布局管理器:FlowLayout、BorderLayout、GridLayout、GridBagLayout、CardLayout、BoxLayout
VB. Net GIF (making and disassembling - optimizing code, class library - 5)
Leakage detection relay jy82-2p
Excel comparator
如何展开Collapse 的所有折叠面板
SQL injection - injection based on MSSQL (SQL Server)