当前位置: 首页 > news >正文

C++/JavaScript ⭐算法OJ⭐下一个排列

题目描述 31. Next Permutation

A permutation of an array of integers is an arrangement of its members into a sequence or linear order.

For example, for arr = [1,2,3], the following are all the permutations of arr: [1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1].
The next permutation of an array of integers is the next lexicographically greater permutation of its integer. More formally, if all the permutations of the array are sorted in one container according to their lexicographical order, then the next permutation of that array is the permutation that follows it in the sorted container. If such arrangement is not possible, the array must be rearranged as the lowest possible order (i.e., sorted in ascending order).

For example, the next permutation of arr = [1,2,3] is [1,3,2].
Similarly, the next permutation of arr = [2,3,1] is [3,1,2].
While the next permutation of arr = [3,2,1] is [1,2,3] because [3,2,1] does not have a lexicographical larger rearrangement.
Given an array of integers nums, find the next permutation of nums.

The replacement must be in place and use only constant extra memory.

给定一个整数数组,找到它的下一个排列。下一个排列是指将数组中的元素重新排列成字典序中下一个更大的排列。如果不存在下一个更大的排列,则将数组重新排列为字典序最小的排列(即升序排列)。

解题思路

  • 从数组的末尾开始,找到第一个不满足递增关系的元素 nums[i],即 nums[i] < nums[i+1]
  • 如果找不到这样的 i,说明数组已经是最大的排列,直接反转数组即可。
  • 如果找到了 i,再从数组的末尾开始,找到第一个大于 nums[i] 的元素 nums[j]
  • 交换 nums[i]nums[j]
  • 最后将 i 之后的元素反转,得到下一个排列。

步骤详解

  • 步骤 1:从后向前找到第一个不满足递增关系的元素 nums[i]
  • 步骤 2:从后向前找到第一个大于 nums[i] 的元素 nums[j]
  • 步骤 3:交换 nums[i]nums[j]
  • 步骤 4:将 i 之后的元素反转。
class Solution {
public:void nextPermutation(vector<int>& nums) {int n = nums.size();int i = n - 2;// 步骤 1:找到第一个不满足递增关系的元素while (i >= 0 && nums[i] >= nums[i + 1]) {i--;}if (i >= 0) {int j = n - 1;// 步骤 2:找到第一个大于 nums[i] 的元素while (j >= 0 && nums[j] <= nums[i]) {j--;}// 步骤 3:交换 nums[i] 和 nums[j]swap(nums[i], nums[j]);}// 步骤 4:反转 i 之后的元素reverse(nums.begin() + i + 1, nums.end());}
};
/*** @param {number[]} nums* @return {void} Do not return anything, modify nums in-place instead.*/
var nextPermutation = function(nums) {let n = nums.length;let i = n - 2;// 步骤 1:找到第一个不满足递增关系的元素while (i >= 0 && nums[i] >= nums[i + 1]) {i--;}if (i >= 0) {let j = n - 1;// 步骤 2:找到第一个大于 nums[i] 的元素while (j >= 0 && nums[j] <= nums[i]) {j--;}// 步骤 3:交换 nums[i] 和 nums[j][nums[i], nums[j]] = [nums[j], nums[i]];}// 步骤 4:反转 i 之后的元素let left = i + 1, right = n - 1;while (left < right) {[nums[left], nums[right]] = [nums[right], nums[left]];left++;right--;}
};
http://www.lryc.cn/news/541888.html

相关文章:

  • 《Mycat核心技术》第17章:实现MySQL的读写分离
  • Windows 11 使用容器(Docker Podman)
  • 代码审计入门学习之sql注入
  • 2024信息技术、信息安全、网络安全、数据安全等国家标准合集共125份。
  • element ui的select选择框
  • 文档检索服务平台
  • 使用FastAPI进行可视化部署
  • 设计模式 之 工厂模式(简单工厂模式、工厂方法模式、抽象工厂模式)(C++)
  • 3、Kubernetes 集群部署 Prometheus 和 Grafana
  • 【C语言】第八期——指针
  • 如何在 Mac 上安装并配置 JDK 环境变量
  • 【git-hub项目:YOLOs-CPP】本地实现05:项目移植
  • LeetCode 热题 100 206. 反转链表
  • 2025年02月21日Github流行趋势
  • WebXR教学 03 项目1 旋转彩色方块
  • 深入解析JVM垃圾回收机制
  • 【简单】209.长度最小的子数组
  • 细说 Java 引用(强、软、弱、虚)和 GC 流程(二)
  • CSS通过webkit-scrollbar设置滚动条样式
  • Win10配置VSCode的C/C++编译环境
  • 数据结构与算法再探(七)查找-排序
  • 【C语言】指针(5)
  • 大数据组件(四)快速入门实时数据湖存储系统Apache Paimon(2)
  • PLC通讯
  • 前端js进阶,ES6语法,包详细
  • Scrum方法论指导下的Deepseek R1医疗AI部署开发
  • LINUX安装使用Redis
  • 基于java新闻管理系统,推荐一款开源cms内容管理系统ruoyi-fast-cms
  • 054 redisson
  • 【数据结构】(12) 反射、枚举、lambda 表达式