public class Solution { public IList<IList<int>> Permute(int[] nums) { var rtItem = new List<int>(); var visited= new Dictionary<int, bool>(); IList<IList<int>> rt = new List<IList<int>>(); BackTracking(nums, rtItem, rt, visited); return rt; } /*递归下面这种类型的 1 | 2 1 2 | 1 2 1 2 1 2 | 1 2 1 2 */ public void BackTracking(int[] nums, List<int> rtItem, IList<IList<int>> rt, Dictionary<int, bool> visited) { if (rtItem.Count == nums.Length) { rt.Add(new List<int>(rtItem.ToArray())); //return; } for (int i = 0; i < nums.Length; i++) { var num = nums[i]; if (!visited.ContainsKey(num) || !visited[num]) { rtItem.Add(num); visited[num]=true; BackTracking(nums, rtItem, rt, visited); rtItem.RemoveAt(rtItem.Count - 1); // 回溯啊 visited[num] = false; // 回溯啊 } } } }
标签:rt,rtItem,num,递归,nums,算法,树形,new,visited From: https://www.cnblogs.com/Insist-Y/p/17300005.html