[LeetCode] 59. Spiral Matrix II 螺旋矩阵 II
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的更多相关文章
- [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 ...
- 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 ...
- LeetCode 59. Spiral Matrix II (螺旋矩阵之二)
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
- LeetCode: 59. Spiral Matrix II(Medium)
1. 原题链接 https://leetcode.com/problems/spiral-matrix-ii/description/ 2. 题目要求 给定一个正整数n,求出从1到n平方的螺旋矩阵.例 ...
- Leetcode#59 Spiral Matrix II
原题地址 相比于Spiral Matrix(参见这篇文章)要简单一些,因为是方阵,所以代码简洁一些. 注意当n是奇数的时候,中心小块要单独赋值(代码21行) 代码: vector<vector& ...
- PAT 1105 Spiral Matrix[模拟][螺旋矩阵][难]
1105 Spiral Matrix(25 分) This time your job is to fill a sequence of N positive integers into a spir ...
- [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 ...
- LeetCode OJ:Spiral MatrixII(螺旋矩阵II)
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
- 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 ...
随机推荐
- 【Calling Circles UVA - 247 】【Floyd + dfs】
用到的东西 Floyd算法(不考虑路径的长度,只关心两点之间是否有通路,可用于求有向图的传递闭包) STL map中的count用法 利用dfs输出同一个圈内的名字 题意 题目中给出 n 的人的名字, ...
- jmeter+nmon+crontab简单的执行接口定时压测
一.概述 临时接到任务要对系统的接口进行压测,上面的要求就是:压测,并发2000 在不熟悉系统的情况下,按目前的需求,需要做的步骤: 需要有接口脚本 需要能监控系统性能 需要能定时执行脚本 二.观察 ...
- Beta:凡事预则立
课程名称:软件工程1916|W(福州大学) 作业要求:项目Beta冲刺) 团队名称:葫芦娃队 作业目标:尽力完成 团队博客 队员学号 队员昵称 博客地址 041602421 der himmel ht ...
- Alpha冲刺(8/10)——2019.5.1
所属课程 软件工程1916|W(福州大学) 作业要求 Alpha冲刺(8/10)--2019.5.1 团队名称 待就业六人组 1.团队信息 团队名称:待就业六人组 团队描述:同舟共济扬帆起,乘风破浪万 ...
- 编程小白入门分享四:Vue的安装及使用快速入门
一.VUE简介 vue是一个JavaMVVM库,是一套用于构建用户界面的渐进式框架,是初创项目的首选前端框架.它是以数据驱动和组件化的思想构建的,采用自底向上增量开发的设计.它是轻量级的,它有很多独立 ...
- djiango-异步发送邮件--celery
安装 pip install celery==4.2.0 # celery4.x支持django1.11以上版本 试了好几个版本 就4.2.0能发送成功 1.项目目录里新建一个celery的包cele ...
- web自动化测试-获得验证信息
一.概述 1.在编写功能测试用例时,会假定一个预期结果,在执行用例过程中把得到的实际结果与预期结果进行比较,从而判断用例的通过与失败 2.自动化测试用例是由机器去执行,通常机器并不像人一样有思维和判断 ...
- Problem F. Wiki with String
Problem F. Wiki with StringInput file: standard input Time limit: 1 secondOutput file: standard outp ...
- webuploader之大文件分段上传、断点续传
文件夹数据库处理逻辑 public class DbFolder { JSONObject root; public DbFolder() { this.root = new JSONObject() ...
- 非旋转 treap
其实之前学过一次非旋转 treap,但是全忘光了,今天复习一下. 洛谷 P3369 [模板]普通平衡树 code: #include <bits/stdc++.h> #define N 1 ...