当前位置: 首页 > 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--;
    }
};

相关文章:

  • Qt中QRadioButton的样式设置
  • ChatGPT平替自由!DeepSeek-R1私有化部署全景攻略
  • 八股文实战之JUC:静态方法的锁和普通方法的锁
  • 进程间通信中间件---ZeroMQ
  • Verilog define预处理命令
  • AI 大模型:点亮乡村振兴的新曙光
  • AWS S3深度解析:十大核心应用场景与高可用架构设计实践
  • sh脚本把服务器B,服务器C目录的文件下载到服务器A目录,添加开机自启动并且一小时执行一次脚本
  • 蓝桥与力扣刷题(蓝桥 交换瓶子)
  • ctfshow——phps源码泄露
  • Java APM如何Profiling:使用火焰图多维度分析应用性能瓶颈
  • Java Idea配置问题
  • 【多语言生态篇三】【DeepSeek×Go:高并发推理服务设计】
  • 2502C++,C++继承的多态性
  • 【误差理论与可靠性】第二章 可靠性的基本概念和参数体系
  • 25林业研究生复试面试问题汇总 林业专业知识问题很全! 林业复试全流程攻略 林业考研复试真题汇总
  • 【JavaWeb12】数据交换与异步请求:JSON与Ajax的绝妙搭配是否塑造了Web的交互革命?
  • 京东外卖骑手全部缴纳五险一金
  • 1.vue使用vite构建初始化项目
  • C/C++ 中 volatile 关键字详解
  • 李峰已任上海青浦区委常委
  • 商务部:今年前3月自贸试验区进出口总额达2万亿元
  • 不是10点!乌克兰官员称尚未就俄乌谈判开始时间达成一致
  • 男子恶意遗弃幼子获刑,最高法发布涉未成年人家庭保护典型案例
  • “异常”只停留在医院里,用艺术为“泡泡宝贝”加油
  • 国台办:实现祖国完全统一是大势所趋、大义所在、民心所向