原题地址:https://oj.leetcode.com/problems/spiral-matrix-ii/

题意:

Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.

For example,
Given n = 3,

You should return the following matrix:

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

解题思路:和上题差不多的思路。

代码:

class Solution:
# @return a list of lists of integer
def generateMatrix(self, n):
if n == 0: return []
matrix = [[0 for i in range(n)] for j in range(n)]
up = 0; down = len(matrix)-1
left = 0; right = len(matrix[0])-1
direct = 0; count = 0
while True:
if direct == 0:
for i in range(left, right+1):
count += 1; matrix[up][i] = count
up += 1
if direct == 1:
for i in range(up, down+1):
count += 1; matrix[i][right] = count
right -= 1
if direct == 2:
for i in range(right, left-1, -1):
count += 1; matrix[down][i] = count
down -= 1
if direct == 3:
for i in range(down, up-1, -1):
count += 1; matrix[i][left] = count
left += 1
if count == n*n: return matrix
direct = (direct+1) % 4

[leetcode]Spiral Matrix II @ Python的更多相关文章

  1. LeetCode: Spiral Matrix II 解题报告-三种方法解决旋转矩阵问题

    Spiral Matrix IIGiven an integer n, generate a square matrix filled with elements from 1 to n2 in sp ...

  2. [LeetCode] Spiral Matrix II 螺旋矩阵之二

    Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...

  3. [Leetcode] spiral matrix ii 螺旋矩阵

    Given an integer n, generate a square matrix filled with elements from 1 to n 2 in spiral order. For ...

  4. Leetcode Spiral Matrix II

    Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...

  5. LeetCode Spiral Matrix II (技巧)

    题意: 从1开始产生连续的n2个数字,以螺旋的方式填满一个n*n的数组. 思路: 由于是填满一个矩阵,那么只需要每次都填一圈即可.应该注意特殊情况. 迭代: class Solution { publ ...

  6. Leetcode 54. Spiral Matrix & 59. Spiral Matrix II

    54. Spiral Matrix [Medium] Description Given a matrix of m x n elements (m rows, n columns), return ...

  7. 【leetcode】Spiral Matrix II

    Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 to n2 in s ...

  8. LeetCode:Spiral Matrix I II

    Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matri ...

  9. leetcode 54. Spiral Matrix 、59. Spiral Matrix II

    54题是把二维数组安卓螺旋的顺序进行打印,59题是把1到n平方的数字按照螺旋的顺序进行放置 54. Spiral Matrix start表示的是每次一圈的开始,每次开始其实就是从(0,0).(1,1 ...

随机推荐

  1. HTML Agility Pack:簡單好用的快速 HTML Parser

    HTML Agility Pack:簡單好用的快速 HTML Parser Codeplex 軟體套件(Package)資訊 套件名稱 HTML Agility Pack 作者 Simon Mouri ...

  2. SQL语句之 数据约束

    SQL语句之 数据约束 什么是数据约束 数据约束用来限制用户对数据的非法的修改操作. 1.约束字段的默认值 如果插入记录时,没有给某个字段赋值,那么我们可以设置它的默认值 关键字:default CR ...

  3. android 横竖屏 切换

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha 横竖屏切换 会重新凋用 activity的 生命周期,再次oncreat() 如果清单文 ...

  4. 1、QThreadPool线程池的使用,线程和Widget通过QMetaObject::invokeMethod交互。

    自定义一个QThreadPool,N个线程QRunnable,线程和Widget通过QMetaObject::invokeMethod交互. QRunnable非继承自QObject,所以不可以用信号 ...

  5. resteasy经验谈

    resteasy 是java体系中比较成熟的rest框架,也是jax-rs规范的实现之一,dubbox的REST服务框架,就是采用的resteasy实现,近日在实际项目中遇到了几个问题,记录于此:   ...

  6. oracle 删除字段中空格

    update  sales_report set region =  REGEXP_REPLACE(region,  '( ){1,}', '')

  7. [Go] Cookie 使用简介

    Golang 的 Cookie web 开发免不了要和 cookie 打交道.Go 的 http 库也提供了 cookie 的相关操作. type Cookie struct { Name strin ...

  8. C++中的vector&find_if

     <STL應用> vector & find_if 看到有人問有個名為C的struct如下 code: struct C { int v1; int v2; }; 應用在vecto ...

  9. delphi services允许跨域访问

    delphi services允许跨域访问 unit WebModuleUnit1; procedure TWebModule1.WebModule1DefaultHandlerAction(Send ...

  10. 下载企业级证书打包的app 出现“无法下载应用程序”的问题

    问题描述:在下载企业级证书打包的app 出现“无法下载应用程序”的问题 解决办法:原来是生成plist文件时,设置url犯了一个致命的低级错误.如下