mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 08:50:15 +08:00
Update 0046.全排列.md
更新Java版本
This commit is contained in:
@ -147,7 +147,41 @@ public:
|
||||
|
||||
|
||||
Java:
|
||||
```java
|
||||
class Solution {
|
||||
List<List<Integer>> result = new ArrayList<>();// 存放符合条件结果的集合
|
||||
LinkedList<Integer> path = new LinkedList<>();// 用来存放符合条件结果
|
||||
boolean[] used;
|
||||
public List<List<Integer>> permute(int[] nums) {
|
||||
if (nums.length == 0){
|
||||
return result;
|
||||
}
|
||||
used = new boolean[nums.length];
|
||||
permuteHelper(nums);
|
||||
return result;
|
||||
}
|
||||
|
||||
private void permuteHelper(int[] nums){
|
||||
if (path.size() == nums.length){
|
||||
result.add(new ArrayList<>(path));
|
||||
return;
|
||||
}
|
||||
for (int i = 0; i < nums.length; i++){
|
||||
// if (path.contains(nums[i])){
|
||||
// continue;
|
||||
// }
|
||||
if (used[i]){
|
||||
continue;
|
||||
}
|
||||
used[i] = true;
|
||||
path.add(nums[i]);
|
||||
permuteHelper(nums);
|
||||
path.removeLast();
|
||||
used[i] = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Python:
|
||||
|
||||
|
Reference in New Issue
Block a user