54. Spiral Matrix && 59. Spiral Matrix II
Given a positive integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.
Example:
Input: 3
Output:
[
[ 1, 2, 3 ],
[ 8, 9, 4 ],
[ 7, 6, 5 ]
]
class Solution {
public:
vector<vector<int>> generateMatrix(int n) { vector<vector<int>> res(n,vector<int>(n,));
if (n == ) return res;
int i = ;
int rowS = ,rowE = n - ,colS = ,colE = n -;
while(i <= n * n)
{
for (int j = colS;j <= colE;++j)
{
res[rowS][j] = i++;
}
rowS++; for (int j = rowS;j <= rowE;++j)
{
res[j][colE] = i++;
}
colE--; for (int j = colE;j >= colS;--j)
{
res[rowE][j] = i++;
}
rowE--; for (int j = rowE;j >= rowS;--j)
{
res[j][colS] = i++;
}
colS++;
}
return res;
}
};
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.
Example 1:
Input:
[
[ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ]
]
Output: [1,2,3,6,9,8,7,4,5]
Example 2:
Input:
[
[1, 2, 3, 4],
[5, 6, 7, 8],
[9,10,11,12]
]
Output: [1,2,3,4,8,12,11,10,9,5,6,7]
class Solution {
public:
vector<int> spiralOrder(vector<vector<int>>& matrix) {
int m = matrix.size(), n = m ? matrix[].size() : , u = , d = m - , l = , r = n - , p = ;
vector<int> order(m * n);
while (u <= d && l <= r) {
for (int col = l; col <= r; col++) {
order[p++] = matrix[u][col];
}
if (++u > d) {
break;
}
for (int row = u; row <= d; row++) {
order[p++] = matrix[row][r];
}
if (--r < l) {
break;
}
for (int col = r; col >= l; col--) {
order[p++] = matrix[d][col];
}
if (--d < u) {
break;
}
for (int row = d; row >= u; row--) {
order[p++] = matrix[row][l];
}
if (l++ > r) {
break;
}
}
return order;
}
};
54. Spiral Matrix && 59. Spiral Matrix II的更多相关文章
- 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 54. Spiral Matrix 、59. Spiral Matrix II
54题是把二维数组安卓螺旋的顺序进行打印,59题是把1到n平方的数字按照螺旋的顺序进行放置 54. Spiral Matrix start表示的是每次一圈的开始,每次开始其实就是从(0,0).(1,1 ...
- leetcode@ [54/59] Spiral Matrix & Spiral Matrix II
https://leetcode.com/problems/spiral-matrix/ Given a matrix of m x n elements (m rows, n columns), r ...
- 59. Spiral Matrix && Spiral Matrix II
Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matri ...
- [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 ...
- 54. 59. Spiral Matrix
1. Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral ...
- LeetCode第[54]题(Java):Spiral Matrix
题目:螺旋矩阵 难度:Medium 题目内容: Given a matrix of m x n elements (m rows, n columns), return all elements of ...
- LeetCode 54. 螺旋矩阵(Spiral Matrix) 剑指offer-顺时针打印矩阵
题目描述 给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素. 示例 1: 输入: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, ...
- 矩阵螺旋遍历Spiral Matrix,Spiral Matrix2
SpiralMatrix: Given a matrix of m x n elements (m rows, n columns), return all elements of the matri ...
随机推荐
- Local Model Poisoning Attacks to Byzantine-Robust Federated Learning
In federated learning, multiple client devices jointly learn a machine learning model: each client d ...
- Selenium选择web元素
获取html片段可以用来做什么? 可以用来分割,也可以分析HTML文档 beautifulsoup用法? 安装beautifulsoup库: pip install beautifulsoup4 因为 ...
- 【Canvas】311- 解决 canvas 在高清屏中绘制模糊的问题
点击上方"前端自习课"关注,学习起来~ 一.问题分析 使用 canvas 绘制图片或者是文字在 Retina 屏中会非常模糊.如图: 因为 canvas 不是矢量图,而是像图片一样 ...
- JS实现链式调用 a().b().c()
function a() { this.b = function () { console.log('111') return this } this.c = function () { consol ...
- 基于 HTML5 WebGL 构建智能数字化城市 3D 全景
前言 自 2011 年我国城镇化率首次突破 50% 以来,<新型城镇化发展规划>将智慧城市列为我国城市发展的三大目标之一,并提出到 2020 年,建成一批特色鲜明的智慧城市.截至现今,全国 ...
- IUSEP研修报告
目录 Introduction Alberta - Edmonton University of Alberta IUSEP Schoolwork and Project Principle of F ...
- Leetcode 42 接雨水 双指针
地址 https://leetcode-cn.com/problems/trapping-rain-water/ 给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能 ...
- 解决visual studio换行(回车键)不能代码补全问题
打开工具--选项:将标红的位置改为true即可.
- 2019年12道RabbitMQ高频面试题你都会了吗?(含答案解析)
RabbitMQ 面试题 1.什么是 rabbitmq 2.为什么要使用 rabbitmq 3.使用 rabbitmq 的场景 4.如何确保消息正确地发送至 RabbitMQ? 如何确保消息接收方消费 ...
- SpringBoot微服务电商项目开发实战 --- Redis缓存雪崩、缓存穿透、缓存击穿防范
最近已经推出了好几篇SpringBoot+Dubbo+Redis+Kafka实现电商的文章,今天再次回到分布式微服务项目中来,在开始写今天的系列五文章之前,我先回顾下前面的内容. 系列(一):主要说了 ...