当前位置:网站首页>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;
}边栏推荐
- Arc135 C (the proof is not very clear)
- Tf/pytorch/cafe-cv/nlp/ audio - practical demonstration of full ecosystem CPU deployment - Intel openvino tool suite course summary (Part 2)
- JS get the attribute values nested in the object
- 【微服务】Nacos集群搭建以及加载文件配置
- APScheduler如何设置任务不并发(即第一个任务执行完再执行下一个)?
- js如何将秒转换成时分秒显示
- Understanding of cross domain and how to solve cross domain problems
- win10清除快速访问-不留下痕迹
- "In simple language programming competition (basic)" part 1 Introduction to language Chapter 3 branch structure programming
- AWT introduction
猜你喜欢

js arguments参数使用和详解

Sword finger offer II 038 Daily temperature

Arc135 C (the proof is not very clear)

SQL injection - injection based on MSSQL (SQL Server)

Configure cross compilation tool chain and environment variables

Introduction to AMBA

What is MQ?

My NVIDIA developer journey - optimizing graphics card performance

APScheduler如何设置任务不并发(即第一个任务执行完再执行下一个)?

gslb(global server load balance)技术的一点理解
随机推荐
The difference between PX EM rem
Overview of relevant subclasses of beanfactorypostprocessor and beanpostprocessor
VB. Net calls ffmpeg to simply process video (class Library-6)
724. Find the central subscript of the array
Excel 比较日器
input显示当前选择的图片
Halcon图片标定,使得后续图片处理过后变成与模板图片一样
win10清除快速访问-不留下痕迹
(4) Canal multi instance use
Webrtc quickly set up video call and video conference
QT 获取随机颜色值设置label背景色 代码
我的NVIDIA开发者之旅——优化显卡性能
每周小结(*63):关于正能量
What are the reasons for the frequent high CPU of ECS?
BUU-Crypto-[HDCTF2019]basic rsa
Actual cases and optimization solutions of cloud native architecture
2022.7.3-----leetcode.556
JS扁平化数形结构的数组
[Chongqing Guangdong education] electronic circuit homework question bank of RTVU secondary school
Canoe panel learning video