Skip to content

Commit

Permalink
feat: update leetcode solutions: No.0046. Permutations
Browse files Browse the repository at this point in the history
  • Loading branch information
yanglbme committed Apr 4, 2021
1 parent bb4bd30 commit 2113330
Show file tree
Hide file tree
Showing 4 changed files with 186 additions and 28 deletions.
94 changes: 94 additions & 0 deletions solution/0000-0099/0046.Permutations/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,110 @@

<!-- 这里可写当前语言的特殊实现逻辑 -->

回溯法:

```python
class Solution:
def permute(self, nums: List[int]) -> List[List[int]]:
def dfs(nums, i, res, path, used):
if i == len(nums):
res.append(copy.deepcopy(path))
return
for j in range(len(nums)):
if not used[j]:
path.append(nums[j])
used[j] = True
dfs(nums, i + 1, res, path, used)
used[j] = False
path.pop()

res, path = [], []
used = [False] * len(nums)
dfs(nums, 0, res, path, used)
return res
```

切分数组:

```python
class Solution:
def permute(self, nums: List[int]) -> List[List[int]]:
if len(nums) <= 1:
return [nums]
res = []
for i, num in enumerate(nums):
n = nums[:i] + nums[i + 1:]
for item in self.permute(n):
res.append([num] + item)
return res
```

### **Java**

<!-- 这里可写当前语言的特殊实现逻辑 -->

回溯法:

```java
class Solution {
public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
List<Integer> path = new ArrayList<>();
boolean[] used = new boolean[nums.length];
dfs(nums, 0, res, path, used);
return res;
}

private void dfs(int[] nums, int i, List<List<Integer>> res, List<Integer> path, boolean[] used) {
if (i == nums.length) {
res.add(new ArrayList<>(path));
return;
}
for (int j = 0; j < nums.length; ++j) {
if (!used[j]) {
path.add(nums[j]);
used[j] = true;
dfs(nums, i + 1, res, path, used);
used[j] = false;
path.remove(path.size() - 1);
}
}
}
}
```

- 递归:

```java
class Solution {
public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
permute(res, nums, 0);
return res;
}

private void permute(List<List<Integer>> res, int[] nums, int start) {
if (start == nums.length) {
List<Integer> t = new ArrayList<>();
for (int e : nums) {
t.add(e);
}
res.add(t);
return;
}
for (int i = start; i < nums.length; ++i) {
swap(nums, i, start);
permute(res, nums, start + 1);
swap(nums, i, start);
}
}

private void swap(int[] nums, int i, int j) {
int t = nums[i];
nums[i] = nums[j];
nums[j] = t;
}
}
```

### **...**
Expand Down
71 changes: 70 additions & 1 deletion solution/0000-0099/0046.Permutations/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,82 @@
### **Python3**

```python

class Solution:
def permute(self, nums: List[int]) -> List[List[int]]:
if len(nums) <= 1:
return [nums]
res = []
for i, num in enumerate(nums):
n = nums[:i] + nums[i + 1:]
for item in self.permute(n):
res.append([num] + item)
return res
```

### **Java**

Backtracking:

```java
class Solution {
public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
List<Integer> path = new ArrayList<>();
boolean[] used = new boolean[nums.length];
dfs(nums, 0, res, path, used);
return res;
}

private void dfs(int[] nums, int i, List<List<Integer>> res, List<Integer> path, boolean[] used) {
if (i == nums.length) {
res.add(new ArrayList<>(path));
return;
}
for (int j = 0; j < nums.length; ++j) {
if (!used[j]) {
path.add(nums[j]);
used[j] = true;
dfs(nums, i + 1, res, path, used);
used[j] = false;
path.remove(path.size() - 1);
}
}
}
}
```

- Recursion:

```java
class Solution {
public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
permute(res, nums, 0);
return res;
}

private void permute(List<List<Integer>> res, int[] nums, int start) {
if (start == nums.length) {
List<Integer> t = new ArrayList<>();
for (int e : nums) {
t.add(e);
}
res.add(t);
return;
}
for (int i = start; i < nums.length; ++i) {
swap(nums, i, start);
permute(res, nums, start + 1);
swap(nums, i, start);
}
}

private void swap(int[] nums, int i, int j) {
int t = nums[i];
nums[i] = nums[j];
nums[j] = t;
}
}
```

### **...**
Expand Down
30 changes: 15 additions & 15 deletions solution/0000-0099/0046.Permutations/Solution.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
class Solution {
public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> list = new ArrayList<>();
permute(list, nums, 0);
return list;
List<List<Integer>> res = new ArrayList<>();
permute(res, nums, 0);
return res;
}

private void permute(List<List<Integer>> list, int[] nums, int start) {
int end = nums.length - 1;
if (start == end) {
list.add(Arrays.stream(nums).boxed().collect(Collectors.toList()));

private void permute(List<List<Integer>> res, int[] nums, int start) {
if (start == nums.length) {
List<Integer> t = new ArrayList<>();
for (int e : nums) {
t.add(e);
}
res.add(t);
return;
}

for (int i = start; i <= end; ++i) {
for (int i = start; i < nums.length; ++i) {
swap(nums, i, start);
permute(list, nums, start + 1);
permute(res, nums, start + 1);
swap(nums, i, start);
}


}
private static void swap(int[] nums, int i, int j) {

private void swap(int[] nums, int i, int j) {
int t = nums[i];
nums[i] = nums[j];
nums[j] = t;
Expand Down
19 changes: 7 additions & 12 deletions solution/0000-0099/0046.Permutations/Solution.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
class Solution:
def permute(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
if len(nums) <= 1:
def permute(self, nums: List[int]) -> List[List[int]]:
if len(nums) <= 1:
return [nums]
ans = []
res = []
for i, num in enumerate(nums):
n = nums[:i] + nums[i+1:]
for y in self.permute(n):
ans.append([num] + y)
return ans

n = nums[:i] + nums[i + 1:]
for item in self.permute(n):
res.append([num] + item)
return res

0 comments on commit 2113330

Please sign in to comment.