mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-10 04:06:51 +08:00
Update 0046.全排列.md
This commit is contained in:
@ -147,6 +147,30 @@ public:
|
|||||||
|
|
||||||
|
|
||||||
Java:
|
Java:
|
||||||
|
class Solution {
|
||||||
|
List<List<Integer>> result=new ArrayList<List<Integer>>();
|
||||||
|
Deque<Integer> path=new LinkedList<Integer>();
|
||||||
|
void backtracking(int []nums,Boolean []used) {
|
||||||
|
if(path.size()==nums.length) {
|
||||||
|
result.add(new ArrayList<Integer>(path));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
for(int i=0;i<nums.length;i++) {
|
||||||
|
if(used[i]) continue;
|
||||||
|
used[i]=true;
|
||||||
|
path.addLast(nums[i]);
|
||||||
|
backtracking(nums, used);
|
||||||
|
used[i]=false;
|
||||||
|
path.pollLast();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public List<List<Integer>> permute(int[] nums) {
|
||||||
|
Boolean []used=new Boolean[nums.length];
|
||||||
|
for(int i=0;i<used.length;i++) used[i]=false;
|
||||||
|
backtracking(nums, used);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
Reference in New Issue
Block a user