blob: 7d5160624576f953aa1af174b8a8e3f439b94d03 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace AlgorithmCollection
{
public static partial class Algorithms
{
public static void Swap<T>(ref T v1, ref T v2)
{
T temp = v1;
v1 = v2;
v2 = temp;
}
public static void Swap<T>(ref IList<T> data, int i1, int i2)
{
T temp = data[i1];
data[i1] = data[i2];
data[i2] = temp;
}
public static void Swap<T>(ref List<T> data, int i1, int i2)
{
T temp = data[i1];
data[i1] = data[i2];
data[i2] = temp;
}
public static void Swap<T>(ref T[] data, int i1, int i2)
{
T temp = data[i1];
data[i1] = data[i2];
data[i2] = temp;
}
// 阶乘
public static int Factorial(int n)
{
if (n == 1)
return 1;
return n * Factorial(n - 1);
}
public static T Min<T>(T v1, T v2) where T : IComparable
{
return v1.CompareTo(v2) < 0 ? v1 : v2;
}
public static T Max<T>(T v1, T v2) where T : IComparable
{
return v1.CompareTo(v2) > 0 ? v1 : v2;
}
public static string ListToString<T>(List<T> data)
{
string content = "";
//data.ForEach((T v) => { content += v.ToString(); });
for(int i = 0; i < data.Count; ++i)
{
content += data[i];
if (i != data.Count - 1)
content += ",";
}
return content;
}
}
}
|