题解 | #没有重复项数字的全排列#
没有重复项数字的全排列
https://www.nowcoder.com/practice/4bcf3081067a4d028f95acee3ddcd2b1
#include <iostream> #include <algorithm> #include <vector> #include <cmath> using namespace std; vector<int> vis; //class Solution { //public: void recursion(vector<vector<int>>& res, vector<int>& ans, vector<int>& num, int index) { if (index == num.size()) { res.push_back(ans); return; } for (int i = 0; i < num.size(); i++) { if (vis[i] == 1)//访问数组为0才进行访问 continue; vis[i] = 1; ans.push_back(num[i]); recursion(res, ans, num, index + 1);//递归idx + 1表示下一个位 vis[i] = 0;//回溯 ans.pop_back(); } } vector<vector<int> > permute(vector<int>& num) { sort(num.begin(), num.end()); vector<vector<int>> res; vis.resize(num.size()); vector<int> ans; recursion(res, ans, num, 0); return res; } int main() { vector<int> num; int temp; for (int i = 0; i < 3; i++) { cin >> temp; num.push_back(temp); } vector<vector<int>> end = permute(num); for (int i = 0; i < end.size(); i++) { for (int j = 0; j < end[i].size(); j++) { cout << end[i][j] << ','; } cout << ' '; } return 1; } //};