题解 | #蛇形矩阵#

蛇形矩阵

https://www.nowcoder.com/practice/649b210ef44446e3b1cd1be6fa4cab5e

let num = parseInt(readline())

/**
翻转矩阵得到:
1
2 3
4 5 6
7 8 9 10
*/
let res = [];
let count = 0;
for(let i = 0; i < num; i++) {
    res[i] = [];
    for(let j = 0; j <= i; j++) {
        res[i][j] = ++count;
    }
}
let resStr = "";
let index = 0;
// 每轮寻找当前轮所在数组开始的 倒数 第 N 个
for(let i = 0; i < num; i++) { // 1
    for(let j = i; j < res.length; j++) {
        resStr += res[j][res[j].length - 1 - i] + " "; // 
    }
    console.log(resStr);
    resStr = "";
}

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务