当前位置:网站首页>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;
}边栏推荐
- How to expand all collapse panels
- Nexus 6p从8.0降级6.0+root
- High performance parallel programming and optimization | lesson 02 homework at home
- ansys命令
- input显示当前选择的图片
- Zhanrui tankbang | jointly build, cooperate and win-win zhanrui core ecology
- FreeRTOS 中 RISC-V-Qemu-virt_GCC 的 锁机制 分析
- Detectron:训练自己的数据集——将自己的数据格式转换成COCO格式
- win10清除快速访问-不留下痕迹
- Error CVC complex type 2.4. a: Invalid content beginning with element 'base extension' was found. Should start with one of '{layoutlib}'.
猜你喜欢

HMS v1.0 appointment. PHP editid parameter SQL injection vulnerability (cve-2022-25491)

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

Webrtc quickly set up video call and video conference

js如何将秒转换成时分秒显示

ES6 modularization

JS execution mechanism

Actual cases and optimization solutions of cloud native architecture

注释与注解

Take you to quickly learn how to use qsort and simulate qsort

Leetcode question brushing record | 206_ Reverse linked list
随机推荐
JS execution mechanism
How to implement lazy loading in El select (with search function)
JS arguments parameter usage and explanation
JS flattened array of number shape structure
BUU-Crypto-[HDCTF2019]basic rsa
Thinkphp6.0 middleware with limited access frequency think throttle
Accidentally deleted the data file of Clickhouse, can it be restored?
Viewing and using binary log of MySQL
VB. Net simple processing pictures, black and white (class library - 7)
509. 斐波那契数、爬楼梯所有路径、爬楼梯最小花费
Tutle clock improved version
Design and implementation of tcp/ip series overview
AWT common components, FileDialog file selection box
接地继电器DD-1/60
How much computing power does transformer have
left_and_right_net可解释性设计
Programmers don't talk about morality, and use multithreading for Heisi's girlfriend
Win10 clear quick access - leave no trace
win10清除快速访问-不留下痕迹
Luogu deep foundation part 1 Introduction to language Chapter 5 array and data batch storage