当前位置:网站首页>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;
}
边栏推荐
- Programmers don't talk about morality, and use multithreading for Heisi's girlfriend
- BUU-Crypto-[GXYCTF2019]CheckIn
- Viewing and using binary log of MySQL
- Canoe panel learning video
- el-select如何实现懒加载(带搜索功能)
- Accidentally deleted the data file of Clickhouse, can it be restored?
- Tf/pytorch/cafe-cv/nlp/ audio - practical demonstration of full ecosystem CPU deployment - Intel openvino tool suite course summary (Part 2)
- How to configure static IP for Kali virtual machine
- buuctf-pwn write-ups (8)
- Actual cases and optimization solutions of cloud native architecture
猜你喜欢
70000 words of detailed explanation of the whole process of pad openvino [CPU] - from environment configuration to model deployment
如何获取el-tree中所有节点的父节点
509. 斐波那契数、爬楼梯所有路径、爬楼梯最小花费
JS execution mechanism
Penetration tool - sqlmap
【微服务】Nacos集群搭建以及加载文件配置
C语言中的函数(详解)
冲击继电器JC-7/11/DC110V
How to solve the component conflicts caused by scrollbars in GridView
测试岗的中年危机该如何选择?是坚守还是另寻出路?且看下文
随机推荐
Review | categories and mechanisms of action of covid-19 neutralizing antibodies and small molecule drugs
如何判断数组中是否含有某个元素
(4) Canal multi instance use
[excel] PivotChart
Detectron:训练自己的数据集——将自己的数据格式转换成COCO格式
70000 words of detailed explanation of the whole process of pad openvino [CPU] - from environment configuration to model deployment
SQL injection - injection based on MSSQL (SQL Server)
FreeRTOS 中 RISC-V-Qemu-virt_GCC 的 锁机制 分析
Recommended system 1 --- framework
My NVIDIA developer journey - optimizing graphics card performance
1.1 history of Statistics
JS扁平化数形结构的数组
LayoutManager布局管理器:FlowLayout、BorderLayout、GridLayout、GridBagLayout、CardLayout、BoxLayout
LC weekly 300
Overview of relevant subclasses of beanfactorypostprocessor and beanpostprocessor
Uninstall Google drive hard drive - you must exit the program to uninstall
High performance parallel programming and optimization | lesson 02 homework at home
gslb(global server load balance)技术的一点理解
Luogu deep foundation part 1 Introduction to language Chapter 5 array and data batch storage
px em rem的区别