一维数组组合

#include <iostream>
#include <vector>
using namespace std;

class Solution {
private:
    vector<int>p;
    vector<vector<int>> res;
    vector<bool>used;
public:
    void GetPerm(vector<int>&nums,int Index,vector<int>&p) {

        if (p.size() == 2) {
            res.push_back(p);
            return;
        }
        for (int i = Index; i < nums.size(); ++i) {
            if (!used[i]) {
                p.push_back(nums[i]);
                used[i] = true;
                GetPerm(nums, i, p);
                used[i] = false;
                p.pop_back();
            }
        }
        return;
    }
public:
    int Perm(vector<int>&nums) {
        p.clear();
        used = vector<bool>(nums.size(), false);
        GetPerm(nums, 0, p);
        return res.size();
    }
};

int main() {

    vector<int>nums(5, 0);
    for (int i = 0; i < 5; ++i) {
        nums[i] = i + 1;
    }
    int num = Solution().Perm(nums);
    cout << num << endl;
    cin.get();
}

 

posted @ 2019-10-07 17:37  pycodego  阅读(197)  评论(0编辑  收藏  举报