当前位置:网站首页>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());
}
};
边栏推荐
- 2020ccpc Qinhuangdao J - Kingdom's power
- [article de jailhouse] jailhouse hypervisor
- RGB LED infinite mirror controlled by Arduino
- Control unit
- 快速使用Amazon MemoryDB并构建你专属的Redis内存数据库
- leetcode-3:无重复字符的最长子串
- Dynamic planning solution ideas and summary (30000 words)
- Appium foundation - use the first demo of appium
- Redis publish subscribe command line implementation
- Transform optimization problems into decision-making problems
猜你喜欢

Appium基础 — 使用Appium的第一个Demo

Some common problems in the assessment of network engineers: WLAN, BGP, switch

Appium自动化测试基础 — Appium测试环境搭建总结

SPI 详解

Scope of inline symbol

开源存储这么香,为何我们还要坚持自研?

7. Processing the input of multidimensional features

Sqlmap tutorial (1)

Matrixdb V4.5.0 was launched with a new mars2 storage engine!

API related to TCP connection
随机推荐
shared_ Repeated release heap object of PTR hidden danger
Typical use cases for knapsacks, queues, and stacks
Dichotomy, discretization, etc
打印机脱机时一种容易被忽略的原因
Binary search template
Over fitting and regularization
Daily question 1342 Number of operations to change the number to 0
1040 Longest Symmetric String
快速使用Amazon MemoryDB并构建你专属的Redis内存数据库
[practical skills] how to do a good job in technical training?
Introduction et expérience de wazuh open source host Security Solution
Bit mask of bit operation
CF1634E Fair Share
多屏电脑截屏会把多屏连着截下来,而不是只截当前屏
AtCoder Grand Contest 013 E - Placing Squares
Navicat連接Oracle數據庫報錯ORA-28547或ORA-03135
MIT-6874-Deep Learning in the Life Sciences Week 7
【Rust 笔记】17-并发(下)
2020ccpc Qinhuangdao J - Kingdom's power
Daily question 2006 Number of pairs whose absolute value of difference is k