leetcode之Spiral Matrix II
来源:互联网 发布:js 公式编辑器 编辑:程序博客网 时间:2024/04/29 11:56
这题吸收了别人做的容易理解的作法。写起来有条理多了。代码如下:
class Solution(object): def generateMatrix(self, n): """ :type n: int :rtype: List[List[int]] """ if n <= 0: return [] list1 = [] list2 = [] for i in range(n * n): list1.append(i + 1) # print list1 for i in range(n): list2.append([0] * n) # print list2 left = 0 right = n - 1 top = 0 button = n - 1 # print list1 while list1 != []: if left < right: for i in range(left, right): list2[top][i] = list1[0] del list1[0] for i in range(top, button): list2[i][right] = list1[0] del list1[0] for i in range(right, left, -1): list2[button][i] = list1[0] del list1[0] for i in range(button, top, -1): list2[i][left] = list1[0] del list1[0] if left == right: list2[left][top] = list1[0] break left += 1 right -= 1 top += 1 button -= 1 return list2用了上下左右来控制每次取的数。用来做1非常不错
0 0
- LeetCode之Spiral Matrix II
- LeetCode之Spiral Matrix II
- leetcode之Spiral Matrix II
- 【Leetcode】之Spiral Matrix II
- leetcode之Spiral Matrix 和Spiral Matrix II
- LeetCode: Spiral Matrix II
- LeetCode Spiral Matrix II
- [Leetcode] Spiral Matrix II
- LeetCode : Spiral Matrix II
- [LeetCode] Spiral Matrix II
- [LeetCode]Spiral Matrix II
- [leetcode]Spiral Matrix II
- LeetCode-Spiral Matrix II
- [leetcode] Spiral Matrix II
- LeetCode - Spiral Matrix II
- Leetcode: Spiral Matrix II
- 【LeetCode】Spiral Matrix II
- Leetcode Spiral Matrix II
- 适配器模式
- C# 颜色表
- 【Java】java集合框架
- 黑马程序员——java基础之多态
- 图像处理与计算机视觉:基础,经典以及最近发展(5)计算机视觉
- leetcode之Spiral Matrix II
- 初识JavaScript,Ajax,jQuery,并比较三者关系
- 读OpenCV之小侃高斯滤波
- 消消乐
- 火焰艺术字效果制作
- C++—STL学习笔记
- 狄克斯特拉算法求解村庄问题
- 文章标题
- poj3620Avoid The Lakes【dfs】