Efficient logic to get individual values ​​from a range of values ​​in C # 2.0

I have a list of ranges. Each range has a value from and to a value, that is, the value can be between this range. For example, if the range is (1,4)., Values ​​can be 1,2,3 and 4. Now I need to find different values ​​in this range list. The following is sample code.

class Program
{
    static void Main(string[] args)
    {
        List<Range> values = new List<Range>();
        values.Add(new Range(1, 2));
        values.Add(new Range(1, 3));
        values.Add(new Range(1, 4));
        values.Add(new Range(3, 5));
        values.Add(new Range(7, 10));
        values.Add(new Range(7, 8));

        // Expected Output from the range of values
        //1,2,3,4,5,7,8,9,10
    }
}
class Range
{
    public Range(int _form, int _to)
    {
        from = _from;
        to = _to;
    }
    private int from;

    public int From
    {
        get { return from; }
        set { from = value; }
    }

    private int to;

    public int To
    {
        get { return to; }
        set { to = value; }
    }

}

I can go through each range and find different values. but if someone can give an effective approach, it would be helpful.

+5
source share
3 answers
  • For a small number of intervals, a simple approach should do the trick.
  • , , .
  • , . Java.
+4

, ( , ):

  • MinVal MaxVal -
  • , Begins, Ends
  • , , : Begins from, Ends to
  • depth 0
  • BeginPointer EndPointer
  • : MinVal MaxVal, ( BeginPointer ) β†’ depth 1 BeginPointer ( "" )
  • depth 0 β†’ ,
  • β†’ depth 1 ( "" )

. (.. 1000, OP, . ).

+1

_from _to (1000, ), [1000] . true. (3,6), 3, 4, 5 6 true. , . .

+1

All Articles