LeetCode Permutations

本博客详细介绍了如何基于给定的博客【标题】、【标签】和【内容】,生成新标题、摘要、关键词以及新标签的过程。通过分析原始内容,我们能够提炼关键信息并创建具有吸引力的新标题,同时生成包含技术领域相关信息的摘要、关键词和标签。

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

题目:

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].

class Solution {
public:
	vector<vector<int> > permute(vector<int> &num) {
		res.clear();
		memset(used, false, sizeof(used));
		dfs(0, num.size(), num);
		return res;
	}

private:
	vector<vector<int> > res;
	bool used[100];
	int a[100];
	void dfs(int dep, int maxDep, vector<int> &num) {
		if (dep == maxDep) {
			vector<int> tt;
			for (int i = 0; i < maxDep; i++)
				tt.push_back(a[i]);
			res.push_back(tt);
			return;
		}
		for (int i = 0; i < maxDep; i++) {
			if (!used[i]) {
				used[i] = true;
				a[dep] = num[i];
				dfs(dep + 1, maxDep, num);
				used[i] = false;
			}
		}
	}
};


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值