46:全排列
This commit is contained in:
parent
0347cbcb08
commit
e2a8b1cf99
78
src/main/java/leetcode/editor/cn/Permutations.java
Normal file
78
src/main/java/leetcode/editor/cn/Permutations.java
Normal file
@ -0,0 +1,78 @@
|
|||||||
|
//给定一个不含重复数字的数组 nums ,返回其 所有可能的全排列 。你可以 按任意顺序 返回答案。
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 1:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入:nums = [1,2,3]
|
||||||
|
//输出:[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 2:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入:nums = [0,1]
|
||||||
|
//输出:[[0,1],[1,0]]
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 3:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入:nums = [1]
|
||||||
|
//输出:[[1]]
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 提示:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 1 <= nums.length <= 6
|
||||||
|
// -10 <= nums[i] <= 10
|
||||||
|
// nums 中的所有整数 互不相同
|
||||||
|
//
|
||||||
|
// Related Topics 数组 回溯
|
||||||
|
// 👍 1450 👎 0
|
||||||
|
|
||||||
|
package leetcode.editor.cn;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
//46:全排列
|
||||||
|
public class Permutations {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//测试代码
|
||||||
|
Solution solution = new Permutations().new Solution();
|
||||||
|
solution.permute(new int[]{1,2,3});
|
||||||
|
}
|
||||||
|
|
||||||
|
//力扣代码
|
||||||
|
//leetcode submit region begin(Prohibit modification and deletion)
|
||||||
|
class Solution {
|
||||||
|
private List<List<Integer>> list;
|
||||||
|
|
||||||
|
public List<List<Integer>> permute(int[] nums) {
|
||||||
|
list = new ArrayList<>();
|
||||||
|
dfs(nums, new ArrayList<>());
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void dfs(int[] nums, List<Integer> temp) {
|
||||||
|
if (temp.size() == nums.length) {
|
||||||
|
list.add(new ArrayList<>(temp));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
for (int num : nums) {
|
||||||
|
if (!temp.contains(num)) {
|
||||||
|
temp.add(num);
|
||||||
|
dfs(nums, temp);
|
||||||
|
temp.remove(temp.size() - 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//leetcode submit region end(Prohibit modification and deletion)
|
||||||
|
|
||||||
|
}
|
35
src/main/java/leetcode/editor/cn/Permutations.md
Normal file
35
src/main/java/leetcode/editor/cn/Permutations.md
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
<p>给定一个不含重复数字的数组 <code>nums</code> ,返回其 <strong>所有可能的全排列</strong> 。你可以 <strong>按任意顺序</strong> 返回答案。</p>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>示例 1:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>nums = [1,2,3]
|
||||||
|
<strong>输出:</strong>[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong>示例 2:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>nums = [0,1]
|
||||||
|
<strong>输出:</strong>[[0,1],[1,0]]
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong>示例 3:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>nums = [1]
|
||||||
|
<strong>输出:</strong>[[1]]
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>提示:</strong></p>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li><code>1 <= nums.length <= 6</code></li>
|
||||||
|
<li><code>-10 <= nums[i] <= 10</code></li>
|
||||||
|
<li><code>nums</code> 中的所有整数 <strong>互不相同</strong></li>
|
||||||
|
</ul>
|
||||||
|
<div><div>Related Topics</div><div><li>数组</li><li>回溯</li></div></div>\n<div><li>👍 1450</li><li>👎 0</li></div>
|
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue
Block a user