Accumulation of subsequences of a sequence using C#/Linq

前端 未结 6 1629
醉梦人生
醉梦人生 2021-01-20 01:13

I\'m trying to find a better way of processing a sequence of numbers based on the following requirement: the value of sequence[i] is the sum of its own value pl

6条回答
  •  失恋的感觉
    2021-01-20 01:26

    Assuming you have access to LINQ:

    using System.Linq;
    
    List numbers = new List {10, 20, 30, 40};
    List runningTotals = new List(numbers.Count);
    
    numbers.Aggregate(0, (sum, value) => {
        sum += value; 
        runningTotals.Add(sum); 
        return sum;
    });
    

提交回复
热议问题