How would you display an array of integers as a set of ranges? (algorithm)

后端 未结 16 2947
我在风中等你
我在风中等你 2021-02-15 18:04

Given an array of integers, what is the simplest way to iterate over it and figure out all the ranges it covers? for example, for an array such as:

$numbers = ar         


        
16条回答
  •  滥情空心
    2021-02-15 18:15

    startRange = array[0];    
    for(i=0;i 1)
       {
         endRange = array[i];
         pushRangeOntoArray(startRange,endRange);
         i++;
         startRange = array[i]
         // need to check for end of array here
       }
    }
    

提交回复
热议问题