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 ...
随机推荐
- ASP.NET Core 选项模式源码学习Options IOptionsMonitor(三)
前言 IOptionsMonitor 是一种单一示例服务,可随时检索当前选项值,这在单一实例依赖项中尤其有用.IOptionsMonitor用于检索选项并管理TOption实例的选项通知, IOpti ...
- 最新设计打样制作完成的FPGA视频开发板VIP—V101
设计目的:1.摄像头驱动(30w-500w mipi接口)2.VGA显示器驱动3.USB2.0视频采集4.tft液晶接口(ttl.lvds驱动)5.视频.图像处理(算法验证)6.各种视频接口处理(av ...
- leetcode -- 寻找两个有序数组的中位数
题目: 给定两个大小为 m 和 n 的有序数组 nums1 和 nums2. 请你找出这两个有序数组的中位数,并且要求算法的时间复杂度为 O(log(m + n)). 你可以假设 nums1 和 nu ...
- 【docker】使用学习
[docker]使用学习 目录 =========================================================== 1.docker 安装 2.mysql 安装 3 ...
- lede install unifi controller
requirement: sdb3 should be formated as ext4, DO not use f2fs/NTFS/exFAT. debootstrap --arch=amd64 s ...
- 你不知道的JavaScript(上)this和对象原型(一)
第一章 关于this 1.this 既不指向函数自身也不指向函数的词法作用域 2.this 实际上是在函数被调用时发生的绑定,它指向什么完全取决于函数在哪里被调用(调用位置). 第二章 this全面 ...
- CCF-CSP题解 201903-4 消息传递接口
求并行的各个进程,且进程内部顺序执行的情况下,会不会出现"死锁". 首先用\(%[^\n]\)将每个进程读入.最后过不了居然是因为\(str[\ ]\)开小了(悲喜交加.存储在\( ...
- 函数知识总结(js)
c语言中函数的形参必须定义类型,而且形参的个数和实参的个数必须相等.但是在js中形参不需要定义,在函数定义的小括号中只需要写形参名就可以了不用写var关键字,而且在函数调用时传入的实参可以和形参的个数 ...
- SpringBoot SpringCloud 热部署 热加载 热调试
疯狂创客圈 Java 高并发[ 亿级流量聊天室实战]实战系列 [博客园总入口 ] 架构师成长+面试必备之 高并发基础书籍 [Netty Zookeeper Redis 高并发实战 ] Crazy-Sp ...
- 设计模式之单例模式C#实现
前言 单例模式是老生常谈的一种设计模式,同时它是最简单也是最容易被忽视的一种设计模式. 下面是一些个人看法: (1) 单例类需要保证自己的唯一性,同时也需要避免被继承,即需要使用sealed修饰: ( ...