diff options
Diffstat (limited to 'Assets/Algorithms/Recursion.cs')
-rw-r--r-- | Assets/Algorithms/Recursion.cs | 23 |
1 files changed, 16 insertions, 7 deletions
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<List<T>>();
- foreach(List<T> perm in perms)
+ foreach (List<T> perm in perms)
{
List<T> p = new List<T>(perm);
perms.Add(p);
@@ -26,18 +26,18 @@ namespace AlgorithmCollection.Recursion // 生成器形式,每次返回一个组合
public static IEnumerable Permutations<T>(List<T> 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<T>(List<T> data, int start , int end, List<T> perm = null)
+ private static IEnumerable _Permutations<T>(List<T> data, int start, int end, List<T> perm = null)
{
- if(perm == null)
+ if (perm == null)
perm = new List<T>(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 |