您的位置:首页 > 其它

【LeetCode】Permutations

2014-05-18 21:20 375 查看
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]
.

public class Solution {
public ArrayList<ArrayList<Integer>> permute(int[] num) {
ArrayList<ArrayList<Integer>> re =new ArrayList<ArrayList<Integer>>();
if(num.length==0)
return re;
ArrayList<Integer> ai = new ArrayList<Integer>();
ArrayList<ArrayList<Integer>> te =new ArrayList<ArrayList<Integer>>();
ai.add(num[0]);
re.add(ai);
te.add(ai);
if(num.length==1)
return re;
for(int i=1;i<num.length;i++){
int t = num[i];
Iterator<ArrayList<Integer>> it = re.iterator();
te =new ArrayList<ArrayList<Integer>>();
while(it.hasNext()){
ai = it.next();
for(int j=0;j<ai.size();j++){
ai.add(j, t);
ArrayList<Integer> tem = new ArrayList<Integer>();
for(int mm=0;mm<ai.size();mm++)
tem.add(ai.get(mm));
te.add(tem);
ai.remove(j);
}
ArrayList<Integer> ttem = new ArrayList<Integer>();
for(int nn=0;nn<ai.size();nn++)
ttem.add(ai.get(nn));
ttem.add(t);
te.add(ttem);

}
re=te;
te=new ArrayList<ArrayList<Integer>>();
}
return re;

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: