Skip to content

Commit 963a67a

Browse files
committed
增加了 RelationshipHelper.FindFragments 方法
1 parent ab89807 commit 963a67a

File tree

8 files changed

+475
-103
lines changed

8 files changed

+475
-103
lines changed

AlgorithmLib/AlgorithmLib.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<TargetFramework>netstandard2.0</TargetFramework>
5-
<Version>1.0.2</Version>
5+
<Version>1.0.3</Version>
66
<PackageId>AlgorithmLib</PackageId>
77
<Authors>Jiang Guogang</Authors>
88
<Company></Company>

AlgorithmLib/ArrayHelper.cs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using System;
2+
3+
namespace AlgorithmLib {
4+
public class ArrayHelper {
5+
6+
/// <summary>
7+
/// 数组元素位移,子数组位移
8+
/// </summary>
9+
/// <param name="array">数组</param>
10+
/// <param name="index">要位移的子数组的起始位置</param>
11+
/// <param name="length">要位移的子数组的长度</param>
12+
/// <param name="offset">位移量,正数表示向后移,负数表示向前移</param>
13+
/// <remarks>
14+
/// 越界会抛错,比如[1,2,3,4,5,6,7],我要[5,6]向后位移2,则抛错
15+
/// </remarks>
16+
/// <exception cref="IndexOutOfRangeException">越界异常</exception>
17+
public static void ShiftArrayItems<T>(T[] array, int index, int length, int offset) {
18+
if (index < 0) {
19+
throw new IndexOutOfRangeException("index不能小于0");
20+
}
21+
if (index + length > array.Length) {
22+
throw new IndexOutOfRangeException("指定的子数组越界");
23+
}
24+
if (index + offset < 0 || index + length + offset > array.Length) {
25+
throw new IndexOutOfRangeException("子数组位移越界");
26+
}
27+
if (length == 0 || offset == 0) {
28+
return;
29+
}
30+
31+
//将子数组Copy出来
32+
T[] tempArray = new T[length];
33+
Array.Copy(array, index, tempArray, 0, length);
34+
35+
if (offset > 0) {
36+
37+
Array.Copy(array, index + length, array, index, offset);
38+
Array.Copy(tempArray, 0, array, index + offset, length);
39+
}
40+
else {
41+
Array.Copy(array, index + offset, array, index + offset + length, -offset);
42+
Array.Copy(tempArray, 0, array, index + offset, length);
43+
}
44+
}
45+
}
46+
}

AlgorithmLib/RelationshipHelper.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,5 +80,37 @@ int[] SubFindRelationship(int currOriginalIdx, int matchedValForCurrOrgIdx, int[
8080

8181
return SubFindRelationship(0, 0, fragments);
8282
}
83+
84+
/// <summary>
85+
/// 找出能够合成整体的碎片集(这相当于是FindRelationship的“局部版”)
86+
/// </summary>
87+
/// <param name="whole">整体</param>
88+
/// <param name="fragmentgs">碎片源</param>
89+
/// <returns>返回null表示找不到合适的</returns>
90+
public static int[] FindFragments(int whole, int[] fragments) {
91+
int[] SubFindFragments(int subWhole, int[] subFragments) {
92+
if (subFragments.Length == 1) {
93+
return subWhole == subFragments[0] ? subFragments : null;
94+
}
95+
HashSet<int> hash = new HashSet<int>();
96+
for(int i=0; i<subFragments.Length; i++) {
97+
if (hash.Contains(subFragments[i])) {
98+
continue;
99+
}
100+
hash.Add(subFragments[i]);
101+
if (subWhole == subFragments[i]) {
102+
return new int[] { subFragments[i] };
103+
}
104+
else if (subWhole > subFragments[i]) {
105+
int[] res = SubFindFragments(subWhole - subFragments[i], RollArrayAndRemoveFirstByIndex(subFragments, i));
106+
if(res != null) {
107+
return PrependElementToArray(res, subFragments[i]);
108+
}
109+
}
110+
}
111+
return null;
112+
}
113+
return SubFindFragments(whole, fragments);
114+
}
83115
}
84116
}

AlgorithmLibDemo/AlgorithmLibDemo.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>netcoreapp3.1</TargetFramework>
5+
<TargetFramework>net6.0</TargetFramework>
66
</PropertyGroup>
77

88
<ItemGroup>

0 commit comments

Comments
 (0)