liuguanyu / quiz-every-meeting

8 stars 0 forks source link

2018-05-28 daily quiz #9

Open liuzhengbo opened 6 years ago

liuzhengbo commented 6 years ago

出处:https://leetcode-cn.com/problems/spiral-matrix-ii/description/

给定一个正整数 n,生成一个包含 1 到 n2 所有元素,且元素按顺时针顺序螺旋排列的正方形矩阵。

示例:

输入: 3
输出:
[
 [ 1, 2, 3 ],
 [ 8, 9, 4 ],
 [ 7, 6, 5 ]
]
/**
 * @param {number} n
 * @return {number[][]}
 */
var generateMatrix = function(n) {

};
wuxueguang commented 6 years ago
function generateMatrix(n){
    let il = n - 1;
    let arrs = new Array(n);
    let timer = 0;
    let counter;

    for(let i = 0; i < arrs.length; i++){
        arrs[i] = [];
    }

    (function recur(){ 
        let steps = n - timer * 2 - 1;
        let i = 0;

        do{
            i == 0 
            ? (counter = timer == 0 ? 1 : arrs[timer][timer - 1] + 1)
            : counter++;
            //top
            arrs[timer][i + timer]           = counter;
            //right
            arrs[i + timer][il - timer]      = counter + steps;
            //bottom
            arrs[il - timer][il - timer - i] = counter + steps * 2;
            //left
            arrs[il - timer - i][timer]      = counter + steps * 3;
        }while(++i < steps);

        arrs[il - timer - i + 1][timer]  == n * n || (timer += 1, recur());
    }());

    return arrs;
}
liuguanyu commented 6 years ago

来个递归的


let generateMatrix = n => {
    let matrix = new Array(n).fill(0).map(el => new Array(n).fill(0)) 

    let _generateMatrix = (n, x=0, y=0, start=1) => {
        if (n <= 0){
           return
        }

            if (n == 1){
                  matrix[x][y] = start
           } 

           // 上
           for (let i = x; i < x + n - 1; ++i){
                matrix[y][i] = start++
           } 

           // 右
           for (let j = y; j < y + n-1; ++j){
            matrix[j][x+n-1] = start++
           }  

           // 下
           for (i = x+n-1; i > x; --i) {
            matrix[y+n-1][i] = start++
            }

            // 左
            for (j = y+n-1; j > y; --j) {
            matrix[j][x] = start++
            }  

             // 每走一圈,内圈数字即总行列数减2,数字为从外圈的结束数字的下一个开始
             return _generateMatrix(n-2, x+1, y+1, start)
    }

    _generateMatrix(n)

    return matrix
};
liuzhengbo commented 6 years ago

来个早年间java的

public class Solution {
    public int[][] generateMatrix(int n) {
        int i, j;
        int mat[][] = new int[n][n];
    int num = 1, finalNum = n * n, x = 0, y = n - 1;
    while (x < y) {
        for (i = x, j = x; j < y; j++) {
            mat[i][j] = num++;
        }
        for (i = x, j = y; i < y; i++) {
            mat[i][j] = num++;
        }
        for (i = y, j = y; j > x; j--) {
            mat[i][j] = num++;
        }
        for (i = y, j = x; i > x; i--) {
            mat[i][j] = num++;
        }
        x++;
        y--;
    }
    if (num == finalNum) {
        mat[x][y] = num;
    }
    return mat;
    }
}
chunpu commented 6 years ago

来个自动寻径版

var generateMatrix = function(n) {
    var arr = new Array(n).fill(null)
    arr = arr.map(() => new Array(n).fill(null))
    var directions = '→ ← ↑ ↓'.split(' ')
    var x = 0, y = 0, curr = 1, direction = directions[0]
    do {
        arr[x][y] = curr++
    } while (goNext())
    return arr

    function get(x, y) {
        if (x in arr) {
            return arr[x][y]
        }
    }

    function getDirection(direction, go) {
        var newX = x, newY = y
        if (direction === '→') newY = y + 1
        else if (direction === '←') newY = y - 1
        else if (direction === '↓') newX = x + 1
        else if (direction === '↑') newX = x - 1
        if (go) {
            x = newX
            y = newY
        }
        if (newX in arr) {
            return arr[newX][newY]
        }
    }

    function goNext() {
        var filtered = directions.filter(item => getDirection(item) === null)
        if (filtered.length === 0) {return false}
        if (filtered.includes(direction)) {
            getDirection(direction, true)
            return true
        } else {
            direction = filtered[0]
            return goNext()
        }
    }
}

console.table(generateMatrix(10))