码迷,mamicode.com
首页 > 其他好文 > 详细

【LeetCode】Permutations

时间:2014-12-13 14:59:31      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   ar   color   os   sp   for   

Permutations

Given a collection of 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], and [3,2,1].

 

解法一:递归

class Solution 
{
public:
    vector<vector<int> > permute(vector<int> &num) 
    {
        vector<vector<int> > result;
        vector<int> cur = num;
        Helper(result, cur, 0);
        return result;
    }
    void Helper(vector<vector<int> >& result, vector<int> cur, int start)
    {
        if(start == cur.size()-1)
            result.push_back(cur);
        else
        {
            for(int i = start; i < cur.size(); i ++)
            {
                swap(cur[start], cur[i]);
                Helper(result, cur, start+1);
                swap(cur[i], cur[start]);
            }
        }
    }
};

bubuko.com,布布扣

【LeetCode】Permutations

标签:style   blog   http   io   ar   color   os   sp   for   

原文地址:http://www.cnblogs.com/ganganloveu/p/4161395.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!