GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — 螺旋矩阵 II · Issue #134 · sisterAn/JavaScript-Algorithms

螺旋矩阵 II #134

New issue
Copy link
New issue
Copy link
Open
Open
螺旋矩阵 II#134
Copy link
Labels
LeetCode美团

Description

@sisterAn
sisterAn
opened on Dec 1, 2020
Issue body actions

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

示例:

输入: 3
输出:
[
 [ 1, 2, 3 ],
 [ 8, 9, 4 ],
 [ 7, 6, 5 ]
]

leetcode

Activity

  1. sisterAn
    added
    LeetCode
    美团
    on Dec 1, 2020
  2. sisterAn commented on Dec 2, 2020

    @sisterAn
    sisterAn
    on Dec 2, 2020
    OwnerAuthor
    More actions
    const generateMatrix = (n) => {
        // 定义一个二维数组进行数据保存
        const result = []
        for (let i = 0; i < n; i++) {
            result.push(new Array(n))
        }
        let left = 0
        let right = n - 1
        let top = 0
        let bottom = n - 1
        let current = 1, max = n * n
        while(current <= max) {
            // 上面从左到右
            for (let i = left; i <= right; i++) {
                result[top][i] = current++
            }
            top ++
            // 右边从上到下
            for (let i = top; i <= bottom; i++) {
                result[i][right] = current++
            }
            right --
            // 下边从右到左
            for (let i = right; i >= left; i--) {
                result[bottom][i] = current++
            }
            bottom --
            // 左边从下到上
            for (let i = bottom; i >= top; i--) {
                result[i][left] = current++
            }
            left ++
        }
        return result
    }
    Reacted by Jet
    Reacted by Jet
    Reacted by Jet
    Reacted by Jet
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    LeetCode美团

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402775 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.