当前位置:网站首页>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;
}边栏推荐
- Design and implementation of redis 7.0 multi part AOF
- Kubernets first meeting
- Overview of relevant subclasses of beanfactorypostprocessor and beanpostprocessor
- (4) Canal multi instance use
- JS flattened array of number shape structure
- HMS v1.0 appointment.php editid参数 SQL注入漏洞(CVE-2022-25491)
- [high concurrency, high performance and high availability of massive data MySQL practice-7] - memory data drop disk
- BUU-Pwn-test_ your_ nc
- Signification des lettres du module optique et abréviation des paramètres Daquan
- Design and implementation of tcp/ip series overview
猜你喜欢

C # character similarity comparison general class

ANSYS command

Accidentally deleted the data file of Clickhouse, can it be restored?

接地继电器DD-1/60

Principle and practice of common defects in RSA encryption application

JS arguments parameter usage and explanation

Design and implementation of redis 7.0 multi part AOF

ES6 模块化
![[microservice] Nacos cluster building and loading file configuration](/img/50/7af220c57a06eb186729c9882d9dab.png)
[microservice] Nacos cluster building and loading file configuration

724. 寻找数组的中心下标
随机推荐
Arc135 a (time complexity analysis)
JS flattened array of number shape structure
High performance parallel programming and optimization | lesson 02 homework at home
Build an Internet of things infrared temperature measuring punch in machine with esp32 / rush to work after the Spring Festival? Baa, no matter how hard you work, you must take your temperature first
[Excel] 数据透视图
如何获取el-tree中所有节点的父节点
win10清除快速访问-不留下痕迹
webrtc 快速搭建 视频通话 视频会议
[excel] PivotChart
A little understanding of GSLB (global server load balance) technology
LC周赛300
How to solve the component conflicts caused by scrollbars in GridView
"In simple language programming competition (basic)" part 1 Introduction to language Chapter 3 branch structure programming
1.1 history of Statistics
Tf/pytorch/cafe-cv/nlp/ audio - practical demonstration of full ecosystem CPU deployment - Intel openvino tool suite course summary (Part 2)
Principle and practice of common defects in RSA encryption application
十二. golang其他
SQL injection - injection based on MSSQL (SQL Server)
体验碎周报第 102 期(2022.7.4)
Overview of relevant subclasses of beanfactorypostprocessor and beanpostprocessor