题目英文
Given a collection of distinct integers, return all possible permutations.
Example:
Input: [1,2,3]
Output:
[
[1,2,3],
[1,3,2],
[2,1,3],
[2,3,1],
[3,1,2],
[3,2,1]
]
题目中文
给定一个没有重复数字的序列,返回其所有可能的全排列。
示例:
输入: [1,2,3]
输出:
[
[1,2,3],
[1,3,2],
[2,1,3],
[2,3,1],
[3,1,2],
[3,2,1]
]
算法实现
回溯法(back tracking) 是一种选优搜索法,又称为试探法,按选优条件向前搜索,以达到目标。但当探索到某一步时,发现原先选择并不优或达不到目标,就退回一步重新选择,这种走不通就退回再走的技术为回溯法,而满足回溯条件的某个状态的点称为“回溯点”。
白话:回溯法可以理解为通过选择不同的岔路口寻找目的地,一个岔路口一个岔路口的去尝试找到目的地。如果走错了路,继续返回来找到岔路口的另一条路,直到找到目的地。
本练习的回溯过程如下所示:
public class Solution
{
private IList<IList<int>> _result;
private bool[] _used;
public IList<IList<int>> Permute(int[] nums)
{
_result = new List<IList<int>>();
if (nums == null || nums.Length == 0)
return _result;
_used = new bool[nums.Length];
FindPath(nums, 0, new List<int>());
return _result;
}
public void FindPath(int[] nums, int count, List<int> path)
{
if (count == nums.Length)
{
//递归终止条件
List<int> item = new List<int>();
item.AddRange(path);
//加入拷贝
_result.Add(item);
return;
}
for (int i = 0; i < nums.Length; i++)
{
if (_used[i] == false)
{
path.Add(nums[i]);
_used[i] = true;
FindPath(nums, count + 1, path);
path.RemoveAt(path.Count - 1);
_used[i] = false;
}
}
}
}
实验结果
- 状态:通过
- 25 / 25 个通过测试用例
- 执行用时: 364 ms, 在所有 C# 提交中击败了 80.00% 的用户
- 内存消耗: 30.6 MB, 在所有 C# 提交中击败了 7.14% 的用户
相关图文
1. “数组”类算法
- LeetCode实战:三数之和
- LeetCode实战:最接近的三数之和
- LeetCode实战:求众数
- LeetCode实战:缺失的第一个正数
- LeetCode实战:快乐数
- LeetCode实战:寻找两个有序数组的中位数
- LeetCode实战:盛最多水的容器
- LeetCode实战:删除排序数组中的重复项
- LeetCode实战:搜索旋转排序数组
2. “链表”类算法
- LeetCode实战:两数相加
- LeetCode实战:删除链表的倒数第N个节点
- LeetCode实战:合并两个有序链表
- LeetCode实战:合并K个排序链表
- LeetCode实战:两两交换链表中的节点
- LeetCode实战:旋转链表
- LeetCode实战:环形链表
3. “栈”类算法
4. “队列”类算法
5. “递归”类算法
6. “字符串”类算法
7. “树”类算法
8. “哈希”类算法
9. “搜索”类算法
10. “动态规划”类算法
11. “数值分析”类算法