Concise Js solution(easy to convert to Java) no need to consider reaching borders or not


  • 0
    C
    var findDiagonalOrder = function(matrix) {
        var m = matrix.length;
        if (!m) return [];
        var n = matrix[0].length;
        var result = [];
        var flag = true;
        for (var s = 0; s < m + n - 1; s++) {
            if (flag) {
                for (var i = m - 1; i >= 0; i--) {
                    if (s - i >= 0 && s - i < n) result.push(matrix[i][s - i]);
                }
            } else {
                for (var j = n - 1; j >= 0; j--) {
                    if (s - j >= 0 && s - j < m) result.push(matrix[s - j][j]);
                }
            }
            flag = !flag;
        }
        return result;
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.