原题地址: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. 心跳包(HeartBeat)

    http://itindex.net/detail/52922-%E5%BF%83%E8%B7%B3-heartbeat-coderzh 几乎所有的网游服务端都有心跳包(HeartBeat或Ping) ...

  2. 使用GenericServlet实例

    使用GenericServlet实例 package com.kettas.servlet; import javax.servlet.* ; import java.io.* ; public cl ...

  3. 5,EasyNetQ-Send Receive

    而发布/订阅和请求/响应模式是位置透明的,因为您不需要指定消息的消费者所在的位置,发送/接收模式专门用于通过命名队列进行通信. 它也不会对可以通过队列发送的消息的类型做任何假设. 这意味着您可以通过同 ...

  4. Python学习——set集合的补充

    set 是一个无序且不重复的元素集合>>> num = {1,2,3,4,5} 1.add()添加一个元素 >>> num.add(6) >>> ...

  5. 简表-Java-Echart报表介绍

    Java后台报表尝试了很多,最终发现了一款,而且是开源的,简表地址:http://www.jatools.com/jor/.问题的引入:该报表支持嵌套,钻去,应对excel类似的报表,足够了.但是,报 ...

  6. CentOS下KVM增加磁盘/磁盘扩容/在线扩容

    一.磁盘镜像操作(适用于raw和qcow2格式) 1.创建镜像 qemu-img create -f qcow2(格式) /kvm/centos1_1.qcow2(路径) 5G(容量) 2.修改镜像容 ...

  7. px 与 dp, sp换算公式?(转)

    PPI = Pixels per inch,每英寸上的像素数,即 "像素密度" xhdpi: 2.0 hdpi: 1.5 mdpi: 1.0 (baseline) ldpi: 0. ...

  8. IAR EWARM __iar_program_start, __iar_data_init3, __iar_copy_init3, __iar_zero_init3

    #include <stdint.h> // The type of a pointer into the init table. typedef void const * table_p ...

  9. ubuntu安装LDAP

    参考文献: https://help.ubuntu.com/12.04/serverguide/openldap-server.html(最主要的) http://www.linuxidc.com/L ...

  10. tomcat点击startup.bat一闪而退的方法

    摘要 在摸索tomcat的配置的时候,发现在启动tomcat服务器的时候,点击startup一闪而退. 解决办法 分析闪退原因,简单做法,右键编辑startup.bat文件,在最后一行添加“pause ...