Subsequence sum

后端 未结 5 1196
天命终不由人
天命终不由人 2021-01-30 18:23

Given an array of integers eg [1, 2, -3, 1] find whether there is a sub-sequence that sums to 0 and return it (eg [1, 2, -3] or [2,

5条回答
  •  死守一世寂寞
    2021-01-30 19:04

    A scala implementation:

    List(1,2,3,4).scan(0){_+_}
    

    the result will be List(0, 1, 3, 6, 10) .

    or you can:

    List(1,2,3,4).scan(0){_+_}.tail  
    

    get List(1, 3, 6, 10)

提交回复
热议问题