当前位置:网站首页>Leetcode t31: next spread
Leetcode t31: next spread
2022-07-01 08:17:00 【Fan Qianzhi】
Title Description
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 .
Example 1:
Input :nums = [1,2,3]
Output :[1,3,2]
Example 2:
Input :nums = [3,2,1]
Output :[1,2,3]
Example 3:
Input :nums = [1,1,5]
Output :[1,5,1]
Tips
1 <= nums.length <= 100
0 <= nums[i] <= 100
Ideas
First, look from right to left , Find a subscript i n d ind ind, Make all the numbers on the right of it in descending order . for example :
↓
1432, ind=0
And then , stay [ i n d + 1 , n u m s . l e n g t h − 1 ] [ind+1, nums.length-1] [ind+1,nums.length−1] Choose a book that is the smallest and greater than a [ i n d ] a[ind] a[ind] Number of numbers , And a [ i n d ] a[ind] a[ind] Swap places , And will i n d ind ind The numbers on the right are arranged in ascending order .
Code
int findInd(int[] a) {
// Look from right to left , Find a subscript , All the numbers on the right are from big to small , Such as 1.432,
// that , Need from 432( That is, find the smallest of all the numbers in reverse order , namely 2), Replace 1, And let 143 Ascending order
int ind = a.length-1;
while(ind > 0) {
if(a[ind] <= a[ind-1]) ind--;
else break;
}
return ind-1;
}
void next_permutation(int[] a) {
int ind = findInd(a);
if(ind == -1) {
Arrays.sort(a);
}
else {
int tar = -1;
for(int i = a.length-1; i > ind; i--) {
if(a[i] > a[ind]) {
tar = i;
break;
}
}
int t = a[ind]; a[ind] = a[tar]; a[tar] = t;
Arrays.sort(a, ind+1, a.length);
}
}
边栏推荐
- Tupu software has passed CMMI5 certification| High authority and high-level certification in the international software field
- Source code analysis of open source API gateway APIs IX
- php laravel微信支付
- Practice and Thinking on the architecture of a set of 100000 TPS im integrated message system
- Principle and process of embossing
- 5大组合拳,解决校园6大难题,护航教育信息化建设
- How to troubleshoot SharePoint online map network drive failure?
- Leetcode T40: 组合总和II
- On June 30, 2022, the record of provincial competition + national competition of Bluebridge
- 源代码加密的意义和措施
猜你喜欢
随机推荐
数字转excel的字符串坐标
String coordinates of number to excel
Serial port oscilloscope software ns-scope
01 NumPy介绍
leetcode T31:下一排列
Leetcode T40: 组合总和II
軟鍵盤高度報錯
[getting started] enter the integer array and sorting ID, and sort its elements in ascending or descending order
【力扣10天SQL入门】Day10 控制流
Cmake I two ways to compile source files
Book of quantitative trading - reading notes of the man who conquers the market
Manually dig XSS vulnerabilities
XX攻击——反射型 XSS 攻击劫持用户浏览器
Provincial election + noi Part VI skills and ideas
Rumtime 1200 upgrade: London upgrade support, pledge function update and more
Li Kou daily question - day 31 -1790 Can a string exchange be performed only once to make two strings equal
Count number of rows per group and add result to original data frame
slice扩容机制分析
The Windows C disk is full
[dynamic planning] p1020 missile interception (variant of the longest increasing subsequence)






![[untitled]](/img/be/3523d0c14d555b293673af2b6fbcff.jpg)
