-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3309629
commit f55b6e1
Showing
2 changed files
with
119 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
--- | ||
title: 33.下一个排列 | ||
date: 2024-03-18 | ||
lang: 'zh-CN' | ||
sidebar: 'auto' | ||
categories: | ||
- LeeCode | ||
tags: | ||
location: HangZhou | ||
--- | ||
|
||
# Heading | ||
|
||
[[toc]] | ||
|
||
[33.下一个排列](https://leetcode.cn/problems/next-permutation/description/) | ||
|
||
Tags: algorithms google array | ||
|
||
Langs: c cpp csharp dart golang java javascript kotlin php python python3 racket ruby rust scala swift typescript | ||
|
||
- algorithms | ||
- Medium (38.97%) | ||
- Likes: 2448 | ||
- Dislikes: - | ||
- Total Accepted: 496.6K | ||
- Total Submissions: 1.3M | ||
- Testcase Example: '[1,2,3]' | ||
|
||
<p>整数数组的一个 <strong>排列</strong> 就是将其所有成员以序列或线性顺序排列。</p> | ||
|
||
<ul> | ||
<li>例如,<code>arr = [1,2,3]</code> ,以下这些都可以视作 <code>arr</code> 的排列:<code>[1,2,3]</code>、<code>[1,3,2]</code>、<code>[3,1,2]</code>、<code>[2,3,1]</code> 。</li> | ||
</ul> | ||
|
||
<p>整数数组的 <strong>下一个排列</strong> 是指其整数的下一个字典序更大的排列。更正式地,如果数组的所有排列根据其字典顺序从小到大排列在一个容器中,那么数组的 <strong>下一个排列</strong> 就是在这个有序容器中排在它后面的那个排列。如果不存在下一个更大的排列,那么这个数组必须重排为字典序最小的排列(即,其元素按升序排列)。</p> | ||
|
||
<ul> | ||
<li>例如,<code>arr = [1,2,3]</code> 的下一个排列是 <code>[1,3,2]</code> 。</li> | ||
<li>类似地,<code>arr = [2,3,1]</code> 的下一个排列是 <code>[3,1,2]</code> 。</li> | ||
<li>而 <code>arr = [3,2,1]</code> 的下一个排列是 <code>[1,2,3]</code> ,因为 <code>[3,2,1]</code> 不存在一个字典序更大的排列。</li> | ||
</ul> | ||
|
||
<p>给你一个整数数组 <code>nums</code> ,找出 <code>nums</code> 的下一个排列。</p> | ||
|
||
<p>必须<strong><a href="https://baike.baidu.com/item/%E5%8E%9F%E5%9C%B0%E7%AE%97%E6%B3%95" target="_blank"> 原地 </a></strong>修改,只允许使用额外常数空间。</p> | ||
|
||
<p> </p> | ||
|
||
<p><strong>示例 1:</strong></p> | ||
|
||
<pre> | ||
<strong>输入:</strong>nums = [1,2,3] | ||
<strong>输出:</strong>[1,3,2] | ||
</pre> | ||
|
||
<p><strong>示例 2:</strong></p> | ||
|
||
<pre> | ||
<strong>输入:</strong>nums = [3,2,1] | ||
<strong>输出:</strong>[1,2,3] | ||
</pre> | ||
|
||
<p><strong>示例 3:</strong></p> | ||
|
||
<pre> | ||
<strong>输入:</strong>nums = [1,1,5] | ||
<strong>输出:</strong>[1,5,1] | ||
</pre> | ||
|
||
<p> </p> | ||
|
||
<p><strong>提示:</strong></p> | ||
|
||
<ul> | ||
<li><code>1 <= nums.length <= 100</code></li> | ||
<li><code>0 <= nums[i] <= 100</code></li> | ||
</ul> | ||
|
||
<<< @/src/LeeCode/33.下一个排列.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* @lc app=leetcode.cn id=31 lang=javascript | ||
* | ||
* [31] 下一个排列 | ||
*/ | ||
|
||
// @lc code=start | ||
/** | ||
* @param {number[]} nums | ||
* @return {void} Do not return anything, modify nums in-place instead. | ||
*/ | ||
var nextPermutation = function (nums) { | ||
const swap = (a, b) => { | ||
const tmp = nums[a] | ||
nums[a] = nums[b] | ||
nums[b] = tmp | ||
} | ||
|
||
const revert = (a, b) => { | ||
while (a < b) { | ||
swap(a++, b--) | ||
} | ||
} | ||
|
||
for (let i = nums.length - 1; i > 0; i--) { | ||
if (nums[i] > nums[i - 1]) { | ||
for (let j = nums.length - 1; j >= i; j--) { | ||
if (nums[j] > nums[i - 1]) { | ||
swap(j, i - 1) | ||
revert(i, nums.length - 1) | ||
return | ||
} | ||
} | ||
break | ||
} | ||
} | ||
revert(0, nums.length - 1) | ||
} | ||
// @lc code=end |