方法分享:有序集合分段
生活随笔
收集整理的這篇文章主要介紹了
方法分享:有序集合分段
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
方法分享:有序集合分段
2 {
3 List<List<string>> rtn = new List<List<string>>();
4 List<string> strList = null;//Char List
5 string start = string.Empty;//Start Char
6 string end = string.Empty;//End Char
7 bool isFind = false;
8 bool isFindEnd = false;//Find End Char
9
10 for (int i = 0; i < stringList.Count; i++)
11 {
12 start = stringList[i];
13 //If exit char
14 foreach (var item in midList)
15 {
16 if (start == item[0])
17 {
18 if (isFindEnd == true)
19 {
20 end = start;
21 }
22 isFind = true;
23 start = item[item.Count - 1];
24 break;
25 }
26 }
27
28 if (isFind == true)
29 {
30 isFind = false;
31 //Adjust index
32 for (int j = 0; j < stringList.Count; j++)
33 {
34 if (stringList[j] == start)
35 {
36 i = j - 1;
37 break;
38 }
39 }
40 if (isFindEnd == true)
41 {
42 strList.Add(end);
43 rtn.Add(strList);
44 isFindEnd = false;
45 end = string.Empty;
46 }
47 }
48 else
49 {
50 if (isFindEnd == true)
51 {
52 //Add the last
53 if (start == stringList[stringList.Count - 1])
54 {
55 strList.Add(start);
56 rtn.Add(strList);
57 }
58 }
59 else
60 {
61 strList = new List<string>();
62 strList.Add(start);
63 isFindEnd = true;
64 }
65 }
66 }
67
68 return rtn;
69 }
1、方法功能描述
有序集合:(A、B、C、D、E、F、G、H、I、J),已經有分段(只有首尾):(A,D),(G、H)。
實現功能:找到其他的分段 (D、G)、(H、J)。
例如比賽軌道,從A地到J地,中間需要經過B、C、D、E、F、G、H、I、J,把軌道分為幾段(只有首尾):(A、D)、(D、G)、(G、H)、(H、J)。現在已經有分段(A、D)、(D、G),怎么找到分段(G、H)、(H、J)?
2、代碼說明及描述
(1)思想
循環字符串,逐個與子字符串的第一個字符進行比對,
如果子字符串列表中不存在,則添加子字符串,并且把子字符串的第一個字符設為該字符串;
如果子字符串中存在該字符,則取得子字符串的最后一個字符,并且循環跳過該段。
(2)C#代碼
1 private List<List<string>> FillMidList(List<string> stringList, List<List<string>> midList)2 {
3 List<List<string>> rtn = new List<List<string>>();
4 List<string> strList = null;//Char List
5 string start = string.Empty;//Start Char
6 string end = string.Empty;//End Char
7 bool isFind = false;
8 bool isFindEnd = false;//Find End Char
9
10 for (int i = 0; i < stringList.Count; i++)
11 {
12 start = stringList[i];
13 //If exit char
14 foreach (var item in midList)
15 {
16 if (start == item[0])
17 {
18 if (isFindEnd == true)
19 {
20 end = start;
21 }
22 isFind = true;
23 start = item[item.Count - 1];
24 break;
25 }
26 }
27
28 if (isFind == true)
29 {
30 isFind = false;
31 //Adjust index
32 for (int j = 0; j < stringList.Count; j++)
33 {
34 if (stringList[j] == start)
35 {
36 i = j - 1;
37 break;
38 }
39 }
40 if (isFindEnd == true)
41 {
42 strList.Add(end);
43 rtn.Add(strList);
44 isFindEnd = false;
45 end = string.Empty;
46 }
47 }
48 else
49 {
50 if (isFindEnd == true)
51 {
52 //Add the last
53 if (start == stringList[stringList.Count - 1])
54 {
55 strList.Add(start);
56 rtn.Add(strList);
57 }
58 }
59 else
60 {
61 strList = new List<string>();
62 strList.Add(start);
63 isFindEnd = true;
64 }
65 }
66 }
67
68 return rtn;
69 }
3、說明
這個功能在做項目的過程中有時會遇到,個人感覺寫的有點繁瑣,提供拙見供大家探討。
posted on 2011-08-20 13:41?daixinet.com 閱讀(...) 評論(...) 編輯 收藏轉載于:https://www.cnblogs.com/sshoub/archive/2011/08/20/2147114.html
總結
以上是生活随笔為你收集整理的方法分享:有序集合分段的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: (转)c3p0配置大全
- 下一篇: 22个小故事