LeetCode-Permutations-解题报告

本文介绍了LeetCode中全排列问题的多种解法,包括使用next_permutation函数、回溯法及逆康托展开等,并提供了C++实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原题链接 https://leetcode.com/problems/permutations/

Given a collection of numbers, return all possible permutations.

For example,
[1,2,3] have the following permutations:
[1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1]


求全排列。


我开始想到了c++算法库中的next_permutation,然后就是了一下 然后就ac了。(ps: 数组要先排序)。


当然也可以使用回溯法解决。


然后我就上网查了一下next_permutation的实现方法。

大家可以去看这个 http://www.cnblogs.com/devymex/archive/2010/08/17/1801122.html


还有一种方法就是使用逆康托展开,直接算出来。

我就偷懒不叙述了。


class Solution {
public:
    vector<vector<int> > permute(vector<int>& nums) {
		sort(nums.begin(), nums.end());
		vector<vector<int> >ans;
		ans.push_back(nums);
		while (next_permutation(nums.begin(), nums.end()))
			ans.push_back(nums);
		return ans;
	}
};


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值