Algorithm for merging overlapping time periods
I. Use of non-sequencing:
Programme one:
1. File: Extract_chao.cs (CORE)
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceextract{ Public classExtract_chao {List<ExtractInfo> extractlist =NewList<extractinfo>(); Public voidMain () {varList =addextract (); Buidextract (list,0); for(inti =0; i < Extractlist.count; i++) { if(Extractlist[i] = =NULL)Continue; Console.WriteLine (Extractlist[i]. StartPoint+"-------"+Extractlist[i]. EndPoint); } } Private voidBuidextract (list<extractinfo> List,intnum) { for(inti =0; I < list. Count; i++) {
if (i==num) continue;
if(List[i]! =NULL&&list[num]!=NULL) { if(List[i]. StartPoint > List[num]. StartPoint && List[i]. StartPoint <=List[num]. EndPoint) {if(List[i]. EndPoint >List[num]. EndPoint) {varExtracttemp =NewExtractinfo () {StartPoint = List[num]. StartPoint, EndPoint =List[i]. EndPoint}; List[num]=extracttemp; } List[i]=NULL; } Else if(List[i]. EndPoint > List[num]. StartPoint && List[i]. EndPoint <List[num]. EndPoint) {if(List[i]. StartPoint <List[num]. StartPoint) {varExtracttemp =NewExtractinfo () {StartPoint = List[i]. StartPoint, EndPoint =List[num]. EndPoint}; List[num]=extracttemp; } List[i]=NULL; }}} num++; if(Num <list. Count) {buidextract (list, num); } Else{extractlist=list; } } PrivateList<extractinfo>addextract () {varList =NewList<extractinfo>(); List. ADD (NewExtractinfo () {StartPoint = -, EndPoint = A }); List. ADD (NewExtractinfo () {StartPoint = -, EndPoint = the }); List. ADD (NewExtractinfo () {StartPoint = +, EndPoint = the }); List. ADD (NewExtractinfo () {StartPoint = -, EndPoint = the }); List. ADD (NewExtractinfo () {StartPoint =5, EndPoint =Ten }); List. ADD (NewExtractinfo () {StartPoint = -, EndPoint = - }); List. ADD (NewExtractinfo () {StartPoint =6, EndPoint = - }); returnlist; } }}
2. File: ExtractInfo.cs (Entity)
Public class Extractinfo { publicdoublegetset;} Public Double Get Set ; } }
3. File: Program.cs (Entrance)
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceextract{classProgram {Static voidMain (string[] args) {Extract_chao Chao=NewExtract_chao (); Chao. Main (); Console.WriteLine ("OK"); Console.read (); } }}
4. Operation Result:
Two. Using the sorting algorithm:
Http://www.cnblogs.com/artwl/archive/2011/03/01/1968044.html
This algorithm is very good, but does not meet my needs: (The last end is equal to the next point of time) "according to their own needs to choose the solution"
For example, the following two period of time period:
5-10
10-20
The result of my demand is to merge: 5-20
The way of thinking decides procedure
---logic and thinking
Welcome everyone to share a better algorithm, alone music than the music!
Algorithm for merging overlapping time periods