当前位置:网站首页>[leetcode] 31. Next arrangement
[leetcode] 31. Next arrangement
2022-07-24 09:13:00 【Xiaoqu classmate】
31、 Next spread
subject :
An integer array array Is to arrange all its members in sequence or linear order .
for example ,arr = [1,2,3] , The following can be regarded as arr Permutation :[1,2,3]、[1,3,2]、[3,1,2]、[2,3,1] .
Integer array Next spread It refers to the next lexicographic order of its integers . More formally , Arrange all the containers in the order from small to large , So the array of Next spread It is the arrangement behind it in this ordered container . If there is no next larger arrangement , Then the array must be rearranged to the lowest order in the dictionary ( namely , Its elements are arranged in ascending order ).
for example ,arr = [1,2,3] The next line up for is [1,3,2] .
Similarly ,arr = [2,3,1] The next line up for is [3,1,2] .
and arr = [3,2,1] The next line up for is [1,2,3] , because [3,2,1] There is no greater order of dictionaries .
Give you an array of integers nums , find nums The next permutation of .
must In situ modify , Only additional constant spaces are allowed .
Their thinking :
This question means :
“ Next spread ” Is defined as : The next larger order in the dictionary order of a given sequence of numbers . If there is no next larger arrangement , Then rearrange the numbers in the smallest order ( In ascending order ).
We can formally describe the problem as : Given several numbers , Combine them into an integer . How to rearrange these numbers , To get the next larger integer . Such as 123 The next larger number is 132. If there is no larger integer , The smallest integer is output .
With 1,2,3,4,5,6 For example , The order is :
123456
123465
123546
...
654321
You can see such a relationship :123456 < 123465 < 123546 < … < 654321.
The standard “ Next spread ” The algorithm can be described as :
- from Backward forward Find the first adjacent ascending element pair (i,j), Satisfy A[i] < A[j]. here [j,end) It must be in descending order
- stay [j,end) Find the first satisfaction from back to front A[i] < A[k] Of k.A[i]、A[k] They are the above 「 decimal 」、「 Large number 」
- take A[i] And A[k] In exchange for
- It can be concluded that at this time [j,end) It must be in descending order , Inversion [j,end), In ascending order
- If in step 1 No matching pair of adjacent elements found , Show the current [begin,end) For a descending order , Then jump to step 4
Reference code :
public void nextPermutation(int[] nums) {
int len = nums.length;
for (int i = len - 1; i > 0; i--) {
if (nums[i] > nums[i - 1]) {
Arrays.sort(nums, i, len);
for (int j = i; j <len; j++) {
if (nums[j] > nums[i - 1]) {
int temp = nums[j];
nums[j] = nums[i - 1];
nums[i - 1] = temp;
return;
}
}
}
}
Arrays.sort(nums);
return;
}

边栏推荐
- TiFlash 源码阅读(五) DeltaTree 存储引擎设计及实现分析 - Part 2
- Vim: use tags file to extend the automatic code completion function of YCM for the third-party library of C language
- Wenxin big model raises a new "sail", and the tide of industrial application has arrived
- Getting started with sorting - insert sorting and Hill sorting
- Aruba学习笔记06-无线控制AC基础配置(CLI)
- Office fallback version, from 2021 to 2019
- One click openstack single point mode environment deployment - preliminary construction
- Android system security - 5.2-apk V1 signature introduction
- Aruba learning notes 06 wireless control AC basic configuration (CLI)
- Tiktok 16 popular categories, tiktok popular products to see which one you are suitable for?
猜你喜欢

Redis learning - Introduction to redis and NiO principles

Account 1-3

Opencv Chinese document 4.0.0 learning notes (updating...)

How to integrate and use log4net logging plug-in in vs2019 class library

gnuplot软件学习笔记

Un7.22: how to upload videos and pictures simultaneously with the ruoyi framework in idea and vs Code?

Office fallback version, from 2021 to 2019

Houdini official HDA sidefx labs installation

Unity solves the package manager "you see to be offline"

C language practice questions + Answers:
随机推荐
[FFH] openharmony gnawing paper growth plan -- Application of cjson in traditional c/s model
How to judge and analyze NFT market briefly through NFT go?
[translation] integration challenges in microservice architecture using grpc and rest
One year after I came to Ali, I ushered in my first job change
Pulse netizens have a go interview question, can you answer it correctly?
Tiktok shop will add a new site, and the Singapore site will be launched on June 9
代码随想录笔记_链表_25K个一组翻转链表
Onpropertychange property
Data collection solution for forestry survey and patrol inspection
SQL optimization principles
C# 简述里氏替换原则的应用
Tiktok shop platform will take disciplinary measures against sellers who violate rules and policies
Six pictures show you why TCP shakes three times?
03_ UE4 advanced_ illumination
Leetcode94 detailed explanation of middle order traversal of binary tree
VGA character display based on FPGA
Aruba学习笔记06-无线控制AC基础配置(CLI)
来阿里一年后我迎来了第一次工作变动....
Rocky basics shell script Basics
数据中台:始于阿里,兴于DaaS