首页 > 学院 > 开发设计 > 正文

LeetCode 46. Permutations

2019-11-08 02:46:23
字体:
来源:转载
供稿:网友

Given a collection of distinct 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],  [3,2,1]]answer:

class Solution {public:    vector<vector<int>> permute(vector<int>& nums) {        int length = nums.size();        vector<vector<int>> result1,result2;        vector<int> temp;        result1.push_back(temp);        bool flag = false;        for(int i = 0; i < length; i ++){            for(int k = 0;k < length; k ++){                for(int j = 0; j < result1.size(); j ++){                    flag = false;                    temp = result1[j];                    for(int l = 0; l < temp.size(); l ++){                        if(temp[l] == nums[k]){                            flag = true;                            break;                        }                    }                    if(flag) continue;                    temp.push_back(nums[k]);                    result2.push_back(temp);                    temp.pop_back();                }            }            result1 = result2;            result2.clear();        }        return result1;    }    };


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表