DAY23:leetcode #59 Spiral Matrix II
来源:互联网 发布:php爬取网页图片实例 编辑:程序博客网 时间:2024/05/05 20:21
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.
For example,
Given n = 3
,
[ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ]]
Subscribe to see which companies asked this question
class Solution(object): def generateMatrix(self, n): """ :type n: int :rtype: List[List[int]] """ self.result = [] for i in range(n): self.result.append([0]*n) self.handleMatrix(0,1,n) return self.result def handleMatrix(self, start,startnum,n): if startnum > n**2: return for j in range(start, n-start): self.result[start][j] = startnum startnum += 1 for i in range(start + 1, n-start): self.result[i][n - start - 1] = startnum startnum += 1 for j in range(start, n - start - 1)[::-1]: self.result[n-start-1][j] = startnum startnum += 1 for i in range(start + 1, n - start - 1)[::-1]: self.result[i][start] = startnum startnum += 1 self.handleMatrix(start + 1,startnum,n)
思路基本与54类似,不再赘述
0 0
- DAY23:leetcode #59 Spiral Matrix II
- DAY23:leetcode #54 Spiral Matrix
- LeetCode 59: Spiral Matrix II
- [leetcode 59] Spiral Matrix II
- [LeetCode 59] Spiral Matrix II
- leetcode || 59、 Spiral Matrix II
- Spiral Matrix II - LeetCode 59
- Leetcode #59 Spiral Matrix II
- leetcode 59: Spiral Matrix II
- Leetcode#59||Spiral Matrix II
- leetcode 59:Spiral Matrix II
- 【LEETCODE】59-Spiral Matrix II
- 【leetcode】【59】Spiral Matrix II
- LeetCode 59 - Spiral Matrix II
- LeetCode 59 Spiral Matrix II
- 【LeetCode-59】Spiral Matrix II
- LeetCode 59 Spiral Matrix II
- Leetcode 59 Spiral Matrix II
- 高并发,高性能的一点调研
- java反射机制
- 信息学竞赛的常数优化、常见问题、代码风格等
- shutdown()及closesocket() 的区别
- android 系统 makefile文件(Android.mk)组织结构
- DAY23:leetcode #59 Spiral Matrix II
- python实现对于多个文件夹下的txt文件的特定字段进行提取,并生成新的txt
- 堕落机房(带权二分图完美最小匹配)
- MyEclipse丨配置
- 浅谈显示Intent和隐式Intent
- LeetCode No.423 Reconstruct Original Digits from English
- Windows查看端口占用命令
- 常量指针和指针常量
- 移动应用技术开发大作业——Android平台个人简历