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

For example,
Given n = 3,

You should return the following matrix:

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

54. Spiral Matrix 类似,这次给定一个整数n,以螺旋顺序填充元素到n^2的矩阵。

Java:

class Solution {
public int[][] generateMatrix(int n) {
int[][] res = new int[n][n];
int k = 1;
int top = 0, bottom = n - 1, left = 0, right = n - 1;
while (left < right && top < bottom) {
for (int j = left; j < right; j++) {
res[top][j] = k++;
}
for (int i = top; i < bottom; i++) {
res[i][right] = k++;
}
for (int j = right; j > left; j--) {
res[bottom][j] = k++;
}
for (int i = bottom; i > top; i--) {
res[i][left] = k++;
}
left++;
right--;
top++;
bottom--;
}
if (n % 2 != 0)
res[n / 2][n / 2] = k;
return res;
}
}

Python:

class Solution:
# @param matrix, a list of lists of integers
# @return a list of integers
def spiralOrder(self, matrix):
result = []
if matrix == []:
return result left, right, top, bottom = 0, len(matrix[0]) - 1, 0, len(matrix) - 1 while left <= right and top <= bottom:
for j in xrange(left, right + 1):
result.append(matrix[top][j])
for i in xrange(top + 1, bottom):
result.append(matrix[i][right])
for j in reversed(xrange(left, right + 1)):
if top < bottom:
result.append(matrix[bottom][j])
for i in reversed(xrange(top + 1, bottom)):
if left < right:
result.append(matrix[i][left])
left, right, top, bottom = left + 1, right - 1, top + 1, bottom - 1 return result 

C++:

class Solution {
public:
vector<vector<int> > generateMatrix(int n) {
vector<vector<int> > res(n, vector<int>(n, 1));
int val = 1, p = n;
for (int i = 0; i < n / 2; ++i, p -= 2) {
for (int col = i; col < i + p; ++col)
res[i][col] = val++;
for (int row = i + 1; row < i + p; ++row)
res[row][i + p - 1] = val++;
for (int col = i + p - 2; col >= i; --col)
res[i + p - 1][col] = val++;
for (int row = i + p - 2; row > i; --row)
res[row][i] = val++;
}
if (n % 2 != 0) res[n / 2][n / 2] = val;
return res;
}
};

  

类似题目:

[LeetCode] 54. Spiral Matrix 螺旋矩阵

All LeetCode Questions List 题目汇总

[LeetCode] 59. Spiral Matrix II 螺旋矩阵 II的更多相关文章

  1. [LeetCode] 885. Spiral Matrix III 螺旋矩阵之三

    On a 2 dimensional grid with R rows and C columns, we start at (r0, c0) facing east. Here, the north ...

  2. LeetCode 54. Spiral Matrix(螺旋矩阵)

    Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...

  3. LeetCode 59. Spiral Matrix II (螺旋矩阵之二)

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

  4. LeetCode: 59. Spiral Matrix II(Medium)

    1. 原题链接 https://leetcode.com/problems/spiral-matrix-ii/description/ 2. 题目要求 给定一个正整数n,求出从1到n平方的螺旋矩阵.例 ...

  5. Leetcode#59 Spiral Matrix II

    原题地址 相比于Spiral Matrix(参见这篇文章)要简单一些,因为是方阵,所以代码简洁一些. 注意当n是奇数的时候,中心小块要单独赋值(代码21行) 代码: vector<vector& ...

  6. PAT 1105 Spiral Matrix[模拟][螺旋矩阵][难]

    1105 Spiral Matrix(25 分) This time your job is to fill a sequence of N positive integers into a spir ...

  7. [leetcode]59. Spiral Matrix II螺旋遍历矩阵2

    Given a positive integer n, generate a square matrix filled with elements from 1 to n^2 in spiral or ...

  8. LeetCode OJ:Spiral MatrixII(螺旋矩阵II)

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

  9. LeetCode OJ:Spiral Matrix(螺旋矩阵)

    Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...

随机推荐

  1. Linux 修改文件目录权限

    修改文件目录权限 chmod​ chmod u+x b.txt chmod 777 a.txt 修改文件的所有者和所属组 ​ 修改所有者chown beifeng a.txt 修改所属组chgrp b ...

  2. gcc的__builtin_函数(注意前面是两个下划线)

    说明: GCC provides a large number of built-in functions other than the ones mentioned above. Some of t ...

  3. 《团队作业第三、四周》五阿哥小组Scrum 冲刺阶段---Day3

    <团队作业第三.四周>五阿哥小组Scrum 冲刺阶段---Day3 一.项目燃尽图 二.项目进展 20182310周烔今日进展: 主要任务一览:界面布局的设计 20182330魏冰妍今日进 ...

  4. 关于AndroidStudio项目app在手机上运行遇到登录网络问题的解决

    又得提到我熟悉的12月份末尾,依旧想着把自己遇到的问题给大家看看,顺便分享我的解决办法. 看过我第一个发的随笔就知道,我遇到过给项目app打包成apk的问题啊,虽然解决了,但是运行到手机上 就又出现了 ...

  5. ZOJ 2676 Network Wars(网络流+分数规划)

    传送门 题意:求无向图割集中平均边权最小的集合. 论文<最小割模型在信息学竞赛中的应用>原题. 分数规划.每一条边取上的代价为1. #include <bits/stdc++.h&g ...

  6. robot framework中如何为每个测试用例,测试集准备数据或销毁数据

    Suite Setup:在这个测试集的所有测试用例开始测试之前运行(类似于junit的@BeforeClass) Suite Teardown:在这个测试集的所有测试用例结束之后运行(类似于junit ...

  7. maven install

    1. install maven under ubuntu apt install maven 2 speed up package download vim ~/.m2/settings.xml & ...

  8. CSS行内元素

    一.典型代表 span a ,strong em del, ins 二.特点: 在一行上显示 不能直接设置宽高 元素的宽和高就是内容撑开的宽高. <style type="text/c ...

  9. Configure JSON.NET to ignore DataContract/DataMember attributes

    https://stackoverflow.com/questions/11055225/configure-json-net-to-ignore-datacontract-datamember-at ...

  10. 洛谷 P3905 道路重建 题解

    P3905 道路重建 题目描述 从前,在一个王国中,在\(n\)个城市间有\(m\)条道路连接,而且任意两个城市之间至多有一条道路直接相连.在经过一次严重的战争之后,有\(d\)条道路被破坏了.国王想 ...