Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

54. Spiral Matrix #195

Open
Tcdian opened this issue Jun 5, 2020 · 1 comment
Open

54. Spiral Matrix #195

Tcdian opened this issue Jun 5, 2020 · 1 comment
Labels

Comments

@Tcdian
Copy link
Owner

Tcdian commented Jun 5, 2020

54. Spiral Matrix

给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素。

Example 1

Input:
[
 [ 1, 2, 3 ],
 [ 4, 5, 6 ],
 [ 7, 8, 9 ]
]
Output: [1,2,3,6,9,8,7,4,5]

Example 2

Input:
[
  [1, 2, 3, 4],
  [5, 6, 7, 8],
  [9,10,11,12]
]
Output: [1,2,3,4,8,12,11,10,9,5,6,7]
@Tcdian Tcdian added the Array label Jun 5, 2020
@Tcdian
Copy link
Owner Author

Tcdian commented Jun 5, 2020

Solution

  • JavaScript Solution
/**
 * @param {number[][]} matrix
 * @return {number[]}
 */
var spiralOrder = function(matrix) {
    if (matrix.length === 0 || matrix[0].length === 0) {
        return [];
    }
    const direction = [0, 1, 0, -1, 0];
    const visited = new Set();
    const reuslt = [];
    let patrolX = 0;
    let patrolY = -1;
    for (let i = 0; i < 4; i = (i + 1) % 4) {
        patrolX += direction[i];
        patrolY += direction[i + 1]
        while(
            patrolX >= 0
                && patrolX < matrix.length
                && patrolY >= 0
                && patrolY < matrix[0].length
                && !visited.has(`${patrolX}-${patrolY}`)
        ) {
            visited.add(`${patrolX}-${patrolY}`);
            reuslt.push(matrix[patrolX][patrolY]);
            patrolX += direction[i];
            patrolY += direction[i + 1];
        }
        if (visited.size === matrix.length * matrix[0].length) {
            break;
        } else {
            patrolX -= direction[i];
            patrolY -= direction[i + 1];
        }
    }
    return reuslt;
};
  • TypeScript Solution
function spiralOrder(matrix: number[][]): number[] {
    if (matrix.length === 0 || matrix[0].length === 0) {
        return [];
    }
    const direction = [0, 1, 0, -1, 0];
    const visited: Set<string> = new Set();
    const reuslt: number[] = [];
    let patrolX = 0;
    let patrolY = -1;
    for (let i = 0; i < 4; i = (i + 1) % 4) {
        patrolX += direction[i];
        patrolY += direction[i + 1]
        while(
            patrolX >= 0
                && patrolX < matrix.length
                && patrolY >= 0
                && patrolY < matrix[0].length
                && !visited.has(`${patrolX}-${patrolY}`)
        ) {
            visited.add(`${patrolX}-${patrolY}`);
            reuslt.push(matrix[patrolX][patrolY]);
            patrolX += direction[i];
            patrolY += direction[i + 1];
        }
        if (visited.size === matrix.length * matrix[0].length) {
            break;
        } else {
            patrolX -= direction[i];
            patrolY -= direction[i + 1];
        }
    }
    return reuslt;
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant