题解 | #没有重复项数字的全排列#

没有重复项数字的全排列

https://www.nowcoder.com/practice/4bcf3081067a4d028f95acee3ddcd2b1

#include <bits/types/struct_tm.h>
#include <vector>
class Solution {
public:
    vector<vector<int> > permute(vector<int> &num) {
        vector<vector<int>> result;
        vector<int> tmp;
        vector<int> visit(num.size(), 0);

        backflow(num, result, tmp, visit);
        return  result;
    }

    void backflow(vector<int>& num, vector<vector<int>>& result, vector<int>& tmp, vector<int>& visit) {

        if (tmp.size() == num.size()) {
            result.push_back(tmp);
            return;
        }

        for (int i = 0; i < num.size(); i++) {
            if (visit[i] == 1) {
                continue;
            }
            tmp.push_back(num[i]);
            visit[i] = 1;
            backflow(num, result, tmp, visit);
            tmp.pop_back();
            visit[i] = 0;
        }
        
    }

};

全部评论

相关推荐

评论
点赞
收藏
分享
牛客网
牛客企业服务