p124 数组的下一个排列(leetcode 31)
2021-02-17 09:18
标签:oid p12 div 思路 time swa || size while 一:解题思路 Time:O(n),Space:O(1) 二:完整代码示例 (C++版和Java版) C++: Java: p124 数组的下一个排列(leetcode 31) 标签:oid p12 div 思路 time swa || size while 原文地址:https://www.cnblogs.com/repinkply/p/12699906.htmlclass Solution {
public:
void nextPermutation(vectorint>& nums)
{
if (nums.size() == 0) return;
int n = nums.size();
int p = n - 2;
while (p >= 0 && nums[p] >= nums[p + 1]) p--;
if (p >= 0)
{
int i = n - 1;
while (i > p && nums[i] ;
swap(nums[p],nums[i]);
}
for (int i = p + 1, j = n - 1; i )
swap(nums[i],nums[j]);
}
};
class Solution
{
private void swap(int[] nums,int i,int j)
{
int temp=nums[i];
nums[i]=nums[j];
nums[j]=temp;
}
public void nextPermutation(int[] nums)
{
if(nums==null || nums.length2) return;
int n=nums.length;
int p=n-2;
while (p>=0 && nums[p]>=nums[p+1]) p--;
if(p>=0)
{
int i=n-1;
while (i>p && nums[i];
swap(nums,i,p);
}
for(int i=p+1,j=n-1;i
文章标题:p124 数组的下一个排列(leetcode 31)
文章链接:http://soscw.com/index.php/essay/56504.html