From 5d81d8d8b4062def695c27fa02f67625871e8dce Mon Sep 17 00:00:00 2001 From: chai Date: Mon, 21 Jun 2021 22:59:39 +0800 Subject: *misc --- Assets/Algorithms/Recursion.cs | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) (limited to 'Assets/Algorithms/Recursion.cs') diff --git a/Assets/Algorithms/Recursion.cs b/Assets/Algorithms/Recursion.cs index 30ebd8a..5a02035 100644 --- a/Assets/Algorithms/Recursion.cs +++ b/Assets/Algorithms/Recursion.cs @@ -16,7 +16,7 @@ namespace AlgorithmCollection.Recursion { if (perms == null) perms = new List>(); - foreach(List perm in perms) + foreach (List perm in perms) { List p = new List(perm); perms.Add(p); @@ -26,18 +26,18 @@ namespace AlgorithmCollection.Recursion // 生成器形式,每次返回一个组合 public static IEnumerable Permutations(List data) { - foreach(var perm in _Permutations(data, 0, data.Count - 1)) + foreach (var perm in _Permutations(data, 0, data.Count - 1)) { yield return perm; } } // 计算start~end范围内的全排列 - private static IEnumerable _Permutations(List data, int start , int end, List perm = null) + private static IEnumerable _Permutations(List data, int start, int end, List perm = null) { - if(perm == null) + if (perm == null) perm = new List(data.Count); - if(start == end) + if (start == end) { perm.Add(data[start]); yield return perm; @@ -50,14 +50,23 @@ namespace AlgorithmCollection.Recursion perm.Add(data[i]); Algorithms.Swap(ref data, start, i); IEnumerator itor = _Permutations(data, start + 1, end, perm).GetEnumerator(); - while(itor.MoveNext()) + while (itor.MoveNext()) yield return itor.Current; Algorithms.Swap(ref data, start, i); perm.RemoveAt(perm.Count - 1); } } } - #endregion + #endregion + + #region 分治 + + public static void DivideAndConquer() + { + + } + + #endregion } } \ No newline at end of file -- cgit v1.1-26-g67d0