**Solution 1**, annotate the matrix entries with coordinate information so that we can just sort them by that.

```
def findDiagonalOrder(self, matrix):
entries = [(i+j, (j, i)[(i^j)&1], val)
for i, row in enumerate(matrix)
for j, val in enumerate(row)]
return [e[2] for e in sorted(entries)]
```

I just saw that @_aig_ does it very similarly, but sorting coordinates. Not sure what I like better.

**Solution 2**, just walk over the matrix in the desired order. My `d`

is the diagonal number, i.e., `i+j`

. So I can compute `j`

as `d-i`

.

```
def findDiagonalOrder(self, matrix):
m, n = len(matrix), len(matrix and matrix[0])
return [matrix[i][d-i]
for d in range(m+n-1)
for i in range(max(0, d-n+1), min(d+1, m))[::d%2*2-1]]
```

Why the range `range(max(0, d-n+1), min(d+1, m))`

? Well I need `0 <= i < m`

and `0 <= j < n`

. As said above, `j`

is `d-i`

, so I have `0 <= d-i < n`

. Isolating `i`

gives me `i <= d`

and `i > d-n`

. Since we're dealing with integers, they're equivalent to `i < d+1`

and `i >= d-n+1`

. So my `i`

needs to be in the range [0, m) as well as in the range [d-n+1, d+1). And my range is simply the intersection of those two ranges.