当前位置:网站首页>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());
}
};
边栏推荐
- SQLMAP使用教程(一)
- 2020ccpc Qinhuangdao J - Kingdom's power
- 2017 USP Try-outs C. Coprimes
- 全排列的代码 (递归写法)
- CCPC Weihai 2021m eight hundred and ten thousand nine hundred and seventy-five
- On the characteristics of technology entrepreneurs from Dijkstra's Turing Award speech
- 【Rust 笔记】16-输入与输出(下)
- Arduino 控制的 RGB LED 无限镜
- Redis publish subscribe command line implementation
- Overview of variable resistors - structure, operation and different applications
猜你喜欢
Erreur de connexion Navicat à la base de données Oracle Ora - 28547 ou Ora - 03135
Appium自动化测试基础 — Appium测试环境搭建总结
Appium基础 — 使用Appium的第一个Demo
leetcode-6108:解密消息
Real time clock (RTC)
Some common problems in the assessment of network engineers: WLAN, BGP, switch
RGB LED infinite mirror controlled by Arduino
6. Logistic model
Wazuh開源主機安全解决方案的簡介與使用體驗
wordpress切换页面,域名变回了IP地址
随机推荐
Daily question 1342 Number of operations to change the number to 0
LeetCode 1200.最小绝对差
Sqlmap tutorial (1)
[practical skills] technical management of managers with non-technical background
Time of process
Codeforces Round #732 (Div. 2) D. AquaMoon and Chess
leetcode-6111:螺旋矩阵 IV
开源存储这么香,为何我们还要坚持自研?
Analysis of backdoor vulnerability in remote code execution penetration test / / phpstudy of national game title of national secondary vocational network security B module
API related to TCP connection
剑指 Offer II 058:日程表
liunx启动redis
Data visualization chart summary (I)
[rust notes] 13 iterator (Part 2)
Scope of inline symbol
Erreur de connexion Navicat à la base de données Oracle Ora - 28547 ou Ora - 03135
快速使用Amazon MemoryDB并构建你专属的Redis内存数据库
Real time clock (RTC)
Appium automation test foundation - Summary of appium test environment construction
Introduction et expérience de wazuh open source host Security Solution