zl程序教程

您现在的位置是:首页 >  其它

当前栏目

leetcode516_leetcode46

2023-06-13 09:14:45 时间

大家好,又见面了,我是你们的朋友全栈君。

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>& nums) {
		vector<vector<int>> res;
		vector<int> path;
		scan( nums, path, res);
		return res;

	}
	void scan(vector<int>& nums, vector<int>& path, vector<vector<int>> &res){
		if (path.size() == nums.size()){
			res.push_back(path);
			return;
		}

		for (int i = 0; i < nums.size(); i++){
			if (find(path.begin(),path.end(), nums[i]) == path.end()){
				path.push_back(nums[i]);
				scan(nums, path, res);
				path.pop_back();
			}
		}
	}
};

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/191246.html原文链接:https://javaforall.cn