当前位置:网站首页>Leetcode-31: next spread
Leetcode-31: next spread
2022-07-05 06:09:00 【Chrysanthemum headed bat】
leetcode-31: Next spread
subject
Topic linking
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]
Problem solving
Method 1 :
- With 12385764 As an example , Because you want to find the next bigger , So we need to traverse in reverse order .
- Reverse traversal , Find the first incremental adjacent element 57 ( Therefore, the following elements must be decreasing ,764)
- However, we cannot directly let 5 and 7 swapping , Rather let 5 and 6 In exchange for . So we can traverse in reverse order to find the first ratio 5 Big elements , yes 6
- After the exchange, it became 12386754, However, this is not the next larger element you want ( But there are several elements below ). So sort the latter part 12386 754---->12386 457
class Solution {
public:
void nextPermutation(vector<int>& nums) {
int n=nums.size();
int i=n-2,j=n-1;
while(i>=0&&nums[i]>=nums[j]){
i--;
j--;
}
int k=n-1;
if(i>=0){
while(nums[i]>=nums[k]) k--;
swap(nums[i],nums[k]);
}
sort(nums.begin()+i+1,nums.end());
}
};
边栏推荐
- Control unit
- 对for(var i = 0;i < 5;i++) {setTimeout(() => console.log(i),1000)}的深入分析
- 【Rust 笔记】17-并发(下)
- Some common problems in the assessment of network engineers: WLAN, BGP, switch
- Daily question 2006 Number of pairs whose absolute value of difference is k
- 【实战技能】非技术背景经理的技术管理
- leetcode-6110:网格图中递增路径的数目
- API related to TCP connection
- 【Rust 笔记】13-迭代器(下)
- R language [import and export of dataset]
猜你喜欢

Sqlmap tutorial (1)

全排列的代码 (递归写法)

CF1637E Best Pair

Navicat連接Oracle數據庫報錯ORA-28547或ORA-03135

Implement an iterative stack

MIT-6874-Deep Learning in the Life Sciences Week 7

Sqlmap tutorial (II) practical skills I

shared_ Repeated release heap object of PTR hidden danger

SQLMAP使用教程(二)实战技巧一

leetcode-6110:网格图中递增路径的数目
随机推荐
中职网络安全技能竞赛——广西区赛中间件渗透测试教程文章
leetcode-22:括号生成
Appium automation test foundation - Summary of appium test environment construction
一些工具的记录2022
多屏电脑截屏会把多屏连着截下来,而不是只截当前屏
【Jailhouse 文章】Performance measurements for hypervisors on embedded ARM processors
智慧工地“水电能耗在线监测系统”
One question per day 2047 Number of valid words in the sentence
7. Processing the input of multidimensional features
Typical use cases for knapsacks, queues, and stacks
RGB LED infinite mirror controlled by Arduino
leetcode-6110:网格图中递增路径的数目
[jailhouse article] performance measurements for hypervisors on embedded ARM processors
Smart construction site "hydropower energy consumption online monitoring system"
Bit mask of bit operation
F - Two Exam(AtCoder Beginner Contest 238)
R language [import and export of dataset]
Traditional databases are gradually "difficult to adapt", and cloud native databases stand out
Spark中groupByKey() 和 reduceByKey() 和combineByKey()
Time of process