回答編集履歴

2

コードの見直し

2018/07/17 14:21

投稿

pepperleaf
pepperleaf

スコア6383

test CHANGED
File without changes

1

コードの見直し

2018/07/17 14:21

投稿

pepperleaf
pepperleaf

スコア6383

test CHANGED
@@ -1,3 +1,59 @@
1
+ 書換えてみました。
2
+
3
+ 他の方ほど、シンプルでありませんが、一応。
4
+
5
+
6
+
7
+ ```C#
8
+
9
+ List<HashSet<int>> hashList = new List<HashSet<int>>() {A, B, C, D, E, F }; // 全リスト
10
+
11
+ List<HashSet<int>> orgList = new List<HashSet<int>>();
12
+
13
+ bool exitFlag = false;
14
+
15
+ while (!exitFlag) { // 重複データがある間、ループする
16
+
17
+ orgList = hashList;
18
+
19
+ hashList = new List<HashSet<int>>();
20
+
21
+ exitFlag = true;
22
+
23
+ // orgList --> hashList へコピーしながら、検査
24
+
25
+ foreach (var item in orgList) {
26
+
27
+ bool found = false;
28
+
29
+ foreach (var newitem in hashList) {
30
+
31
+ if (item.Overlaps(newitem)) { // 重複データがあるか
32
+
33
+ newitem.UnionWith(item); // あれば、マージ
34
+
35
+ found = true;
36
+
37
+ break;
38
+
39
+ }
40
+
41
+ }
42
+
43
+ if (!found) hashList.Add(item); // 重複が見つからないので、追加
44
+
45
+ exitFlag &= !found; // 重複があったか?
46
+
47
+ }
48
+
49
+ }
50
+
51
+ ```
52
+
53
+
54
+
55
+ -------- 最初のコード ----------------------------
56
+
1
57
  以下のコードではどうでしょうか?
2
58
 
3
59
 
@@ -14,11 +70,13 @@
14
70
 
15
71
  hash.ExceptWith(allSet); // 重複データを除く
16
72
 
17
- foreach (var item in hash) { // 確認済みデータに追加
73
+ ~~foreach (var item in hash) { // 確認済みデータに追加
18
74
 
19
75
  allSet.Add(item);
20
76
 
21
- }
77
+ }~~
78
+
79
+ allSet.UnionWith(hash); // これで良かった。
22
80
 
23
81
  }
24
82