[LeetCode]题解(python):059-Spiral Matrix II
题目来源
https://leetcode.com/problems/spiral-matrix-ii/
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.
题意分析
Input: n:integer
Output:a matrix decribed as list[list[]]
Conditions:输入一个大小,得到一个方形矩阵,然后形成蛇形矩阵
题目思路
本题与54题属于一个类别,都是蛇形遍历,这里直接先生成一个初始化的matrix,然后遍历一遍,利用累加变量来赋值
AC代码(Python)
__author__ = 'YE' class Solution(object):
def generateMatrix(self, n):
"""
:type n: int
:rtype: List[List[int]]
"""
if n == 0:
return []
matrix = []
for i in range(n):
l = [0 for j in range(n)]
matrix.append(l) up = 0
left = 0
down = len(matrix) - 1
right = len(matrix[0]) - 1 direct = 0 res = [] count = 1 while True:
if direct == 0:
for i in range(left, right + 1):
matrix[up][i] = count
count += 1
up += 1
if direct == 1:
for i in range(up, down + 1):
matrix[i][right] = count
count += 1
right -= 1
if direct == 2:
for i in range(right, left - 1, -1):
matrix[down][i] = count
count += 1
down -= 1
if direct == 3:
for i in range(down, up -1, -1):
matrix[i][left] = count
count += 1
left += 1
if up > down or left > right:
return matrix
direct = (direct + 1) % 4 print(Solution().generateMatrix(3))
[LeetCode]题解(python):059-Spiral Matrix II的更多相关文章
- Java for LeetCode 059 Spiral Matrix II
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
- 059. Spiral Matrix II
题目链接:https://leetcode.com/problems/spiral-matrix-ii/description/ Given a positive integer n, generat ...
- LeetCode(59)SPiral Matrix II
题目 Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. F ...
- LeetCode 题解 Search a 2D Matrix II。巧妙!
[ [1, 4, 7, 11, 15], [2, 5, 8, 12, 19], [3, 6, 9, 16, 22], [10, 13, 14, 17, 24], [18, 21, 23, 26, 30 ...
- 059 Spiral Matrix II 旋转打印矩阵 II
给出正整数 n,生成正方形矩阵,矩阵元素为 1 到 n2 ,元素按顺时针顺序螺旋排列.例如,给定正整数 n = 3,应返回如下矩阵:[ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6 ...
- LeetCode: Spiral Matrix II 解题报告-三种方法解决旋转矩阵问题
Spiral Matrix IIGiven an integer n, generate a square matrix filled with elements from 1 to n2 in sp ...
- 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 ...
- 【leetcode】Spiral Matrix II
Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 to n2 in s ...
- leetcode 54. Spiral Matrix 、59. Spiral Matrix II
54题是把二维数组安卓螺旋的顺序进行打印,59题是把1到n平方的数字按照螺旋的顺序进行放置 54. Spiral Matrix start表示的是每次一圈的开始,每次开始其实就是从(0,0).(1,1 ...
- 【leetcode】59.Spiral Matrix II
Leetcode59 Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 ...
随机推荐
- Count the Trees[HDU1131]
Count the Trees Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- android 屏幕截取,pull到pc端
1.当需要截取手机屏幕时,进行批命令点击,截取adb pull到电脑端,或者进行接口上传到服务器. GetScreen.bat @ECHO OFF :: read config.cfg SETLOCA ...
- 【BZOJ】1798: [Ahoi2009]Seq 维护序列seq(线段树)
http://www.lydsy.com/JudgeOnline/problem.php?id=1798 之前写了个快速乘..........................20多s...... 还好 ...
- 在openGL中绘制图形
点的绘制.: glVertex*():星号表示函数要有后缀 该函数 需要放在glBegin函数和glEnd函数之间,glBegin函数的向量指定绘制图元的类型,而glEnd函数没有参数,例如: glB ...
- 使用CSS3实现百叶窗
<!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <title&g ...
- MATLAB的使用总结
Log scale %# some random data x = .^(:); y = rand(size(x)); plot(log2(x), y) %# plot on log2 x-scale ...
- 移动端 :meta标签1万个作用
meta标签 <meta charset="utf-8"> <meta http-equiv="Content-Type" content=& ...
- nginx支持flv MP4 扩展nginx_mod_h264_streaming,nginx-rtmp-module-master,yamdi
./configure \ --prefix=/usr/local/nginx \ --sbin-path=/usr/local/nginx/sbin/nginx \ --conf-path=/usr ...
- 《Java核心技术卷二》笔记(二)文件操作和内存映射文件
文件操作 上一篇已经总结了流操作,其中也包括文件的读写.文件系统除了读写以为还有很多其他的操作,如复制.移动.删除.目录浏览.属性读写等.在Java7之前,一直使用File类用于文件的操作.Java7 ...
- Gitolite配置管理和GIT基本操作
简述公司版gitolite的项目配置与管理 1. 基于秘钥对的管理 1.1 客户端(需要访问代码库的机器)生成秘钥对,采用RSA加密ssh-keygen -t rsa -f path_to_store ...