题目:
思路 回溯
代码:
class Solution {
public List<List<Integer>> permute(int[] nums) {
LinkedList<List<Integer>> res = new LinkedList<>();
getRes(nums,res, new LinkedList<Integer>(),nums.length);
return res;
}
private void getRes(int[] nums,LinkedList<List<Integer>> lists, LinkedList<Integer> path,int len) {
if (path.size()==len){
lists.add(new LinkedList<>(path));
return;
}
for (int i = 0; i < len; i++) {
if (path.contains(nums[i])){
continue;
}else {
path.add(nums[i]);
getRes(nums,lists,path,len);
path.removeLast();
}
}
}
}
网友评论