当前位置:网站首页>556. next larger element iii- (31. next permutation) - two iterations
556. next larger element iii- (31. next permutation) - two iterations
2022-06-11 19:27:00 【hequnwang10】
One 、 Title Description
Give you a positive integer n , Please find the smallest integer that meets the conditions , It consists of rearranging n Each number present in the consists of , And its value is greater than n . If there is no such positive integer , Then return to -1 .
Be careful , The returned integer should be a 32 An integer , If there is an answer that satisfies the meaning of the question , But it's not 32 An integer , Also return to -1 .
Example 1:
Input :n = 12
Output :21
Example 2:
Input :n = 21
Output :-1
Two 、 Problem solving
Two traversal






This question is similar to 31 Next arrangement , But what appears here is to give a positive integer ,31 The question is an array , So you need to change a positive integer into a character array .
public class Solution {
public int nextGreaterElement(int n) {
// Convert numbers to character arrays
char[] nums = ("" + n).toCharArray();
int length = nums.length;
int end = length - 2;
// If the current value is greater than the following value , Keep looking for , Always find the first ascending subscript
while(end >= 0 && nums[end] >= nums[end+1]){
end--;
}
// Pay attention to the boundary conditions
if(end < 0){
return -1;
}
// After finding the subscript value , Find a value greater than the subscript value after
if(end >= 0){
int next = length - 1;
while(next >= 0 && nums[end] >= nums[next]){
next--;
}
swap(nums,end,next);
}
// Flip
reverse(nums,end+1);
// To prevent cross-border
try {
return Integer.parseInt(new String(nums));
} catch (Exception e) {
return -1;
}
}
// Flip array
public void reverse(char[] nums,int left){
int right = nums.length-1;
while(left <= right){
swap(nums,left,right);
left++;
right--;
}
}
// Swap arrays
public void swap(char[] nums,int left,int right){
char temp = nums[left];
nums[left] = nums[right];
nums[right] = temp;
}
}

边栏推荐
- mysql 联合索引和BTree
- Introduction to go language (V) -- branch statement
- [signal denoising] signal denoising based on FFT and fir with matlab code
- 【图像去噪】基于绝对差分中值滤波、加权中值滤波法、改进加权中值滤波实现脉冲噪声图像去噪附matlab代码
- Specific methods for porting WinCC flexible 2008 project to botu WinCC
- Key contents that wwdc22 developers need to pay attention to
- PIL pilot image processing [1] - installation and creation
- CMU 15-445 database course lesson 5 text version - buffer pool
- Cf:d. black and white stripe
- cf:G. Count the Trains【sortedset + bisect + 模拟维持严格递减序列】
猜你喜欢

2022-2023 MEM pre approval interview notice of School of management, Xi'an Jiaotong University
2022 coming of age ceremony, to every college entrance examination student

leetcode:剑指 Offer 56 - II. 数组中数字出现的次数 II【简单排序】

SQL injection vulnerability learning 1: phpstudy integrated environment building DVWA shooting range
CMU 15-445 database course lesson 5 text version - buffer pool

E-commerce (njupt)

MBA, EMBA, MPa, MEM, pre interview (pre interview) time batch of national colleges and universities has been released (continuously updated) - Wendu Management Institute
![[video denoising] video denoising based on salt with matlab code](/img/79/e1f8255061a342e02232ac1e393217.png)
[video denoising] video denoising based on salt with matlab code

Specific methods for porting WinCC flexible 2008 project to botu WinCC

SISO decoder for repetition (supplementary Chapter 4)
随机推荐
Find the maximum 3 same digits in the string
Detailed explanation of iSCSI (IV) -- actual configuration of iSCSI server
cf:D. Black and White Stripe【连续k个中最少的个数 + 滑动窗口】
Learn about random library · 1
Operator new and placement new
Highcharts sets the histogram width, gradient, fillet, and data above the column
Undefined reference to 'g2o:: vertexe3:: vertexe3()'
Database design graduation information management
High performance architecture design
CMU 15-445 數據庫課程第五課文字版 - 緩沖池
I don't want to open an account online. Is it safe to open an account online?
cf:A. Print a Pedestal (Codeforces logo?)【简单遍历模拟】
cf:E. Price Maximization【排序 + 取mod + 双指针+ 配对】
Raki's notes on reading paper: learning fast, learning slow: a general continuous learning method
【图像去噪】基于绝对差分中值滤波、加权中值滤波法、改进加权中值滤波实现脉冲噪声图像去噪附matlab代码
The 2023 MBA (Part-time) of Beijing University of Posts and telecommunications has been launched
cf:F. Shifting String【字符串按指定顺序重排 + 分组成环(切割联通分量) + 各组最小相同移动周期 + 最小公倍数】
How are functional components different from class components
Gmail: how do I recall an outgoing message?
[untitled]