首页 > 其他 > 详细

Permutations(Leet Code)

时间:2015-04-21 20:43:58      阅读:218      评论:0      收藏:0      [点我收藏+]

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>> result;
public:
	vector<vector<int> > permute(vector<int> &num) {
		int length=num.size();
		perm(num,0,length-1);
		return result;
	}
	void perm(vector<int> &num,int k,int m){
		if(k==m)
			result.push_back(num);
		for(int i=k;i<=m;i++){
			swap(num[k],num[i]);
			perm(num,k+1,m);
			swap(num[k],num[i]);
		}
	}
	void swap(int &a,int &b){
			int temp=a;
			a=b;
			b=temp;
	}
};

Permutations(Leet Code)

原文:http://blog.csdn.net/sxhlovehmm/article/details/45175427

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!