Algorithm for merging overlapping time periods

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.