当前位置:网站首页>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());
}
};
边栏推荐
- leetcode-6111:螺旋矩阵 IV
- AtCoder Grand Contest 013 E - Placing Squares
- 1.15 - 输入输出系统
- 2022 极术通讯-Arm 虚拟硬件加速物联网软件开发
- 2022年貴州省職業院校技能大賽中職組網絡安全賽項規程
- Data visualization chart summary (I)
- One question per day 1765 The highest point in the map
- CCPC Weihai 2021m eight hundred and ten thousand nine hundred and seventy-five
- 【Jailhouse 文章】Performance measurements for hypervisors on embedded ARM processors
- LVS简介【暂未完成(半成品)】
猜你喜欢
![[cloud native] record of feign custom configuration of microservices](/img/39/05cf7673155954c90e75a8a2eecd96.jpg)
[cloud native] record of feign custom configuration of microservices

Groupbykey() and reducebykey() and combinebykey() in spark

Overview of variable resistors - structure, operation and different applications

CF1634 F. Fibonacci Additions

RGB LED infinite mirror controlled by Arduino

Appium foundation - use the first demo of appium

Data visualization chart summary (I)

CF1637E Best Pair

Traditional databases are gradually "difficult to adapt", and cloud native databases stand out

Personal developed penetration testing tool Satania v1.2 update
随机推荐
传统数据库逐渐“难适应”,云原生数据库脱颖而出
[practical skills] how to do a good job in technical training?
In this indifferent world, light crying
Codeforces Round #716 (Div. 2) D. Cut and Stick
Real time clock (RTC)
2022 pole technology communication arm virtual hardware accelerates the development of Internet of things software
1039 Course List for Student
1.14 - 流水线
Daily question 2006 Number of pairs whose absolute value of difference is k
Educational Codeforces Round 116 (Rated for Div. 2) E. Arena
实时时钟 (RTC)
【实战技能】非技术背景经理的技术管理
Introduction to convolutional neural network
Liunx starts redis
中职网络安全技能竞赛——广西区赛中间件渗透测试教程文章
Simply sort out the types of sockets
shared_ Repeated release heap object of PTR hidden danger
【Rust 笔记】17-并发(上)
How to adjust bugs in general projects ----- take you through the whole process by hand
1040 Longest Symmetric String