全排列

代码:

void per(vector<int> nums,int start,vector<vector<int> > &result){ 

        if(start==nums.size()-1){ 

            result.push_back(nums); 

        } 

        else{ 

            for(int i=start;i<nums.size();i++){ 

                swap(nums[start],nums[i]); 

                per(nums,start+1,result); 

                swap(nums[start],nums[i]); 

            } 

        } 

    }  

 

class Solution { 

public: 

    /**

     * @param nums: A list of integers.

     * @return: A list of permutations.

     */ 

    vector<vector<int> > permute(vector<int> nums) { 

        // write your code here 

        vector<vector<int> > result; 

        if(nums.size()==0){  

            result.push_back(nums); 

            return result; 

        } 

        per(nums,0,result); 

        return result; 

    } 

};  

lintcode截图:

 

posted on 2017-07-25 18:07  这位黑洞同学  阅读(146)  评论(0编辑  收藏  举报

导航