Traverse Matrix in Diagonal strips

前端 未结 16 1862
暖寄归人
暖寄归人 2020-11-28 02:55

I thought this problem had a trivial solution, couple of for loops and some fancy counters, but apparently it is rather more complicated.

So my question is, how woul

16条回答
  •  星月不相逢
    2020-11-28 03:49

    I thought this problem had a trivial solution, couple of for loops and some fancy counters

    Precisely.

    The important thing to notice is that if you give each item an index (i, j) then items on the same diagonal have the same value j+ni, where n is the width of your matrix. So if you iterate over the matrix in the usual way (i.e. nested loops over i and j) then you can keep track of the diagonals in an array that is addressed in the above mentioned way.

提交回复
热议问题