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 ...
随机推荐
- WOE(证据权重)为何这样计算?
更多大数据分析.建模等内容请关注公众号<bigdatamodeling> 先简单回顾一下WOE的含义.假设x是类别变量或分箱处理过的连续变量,含R个类别或分段,取值为{C1, ..., C ...
- Linux sudo用户提权与日志审计
一.格式说明及常用配置选项 格式: 用户或组 主机=授权可以使用哪个用户的权限 可以执行的命令 User_Alias 用户定义别名(别名可以是用户,用户组(用户组前面要加%))例:User_Alias ...
- Atmel Studio 7.0 的使用方法
就在最近,Atmel终于推出了新版本IDE——Atmel Studio 7.0,该版本采用了微软最新的 Visual Studio 2015 平台,经过一段时间使用,Atmel Studio 7.0在 ...
- typeof 与 instanceof之间的区别
JS中会使用typeof 和 instanceof来判断一个变量是否为空或者是什么类型的. ES6规范中有7种数据类型,分别是基本类型和引用类型两大类 基本类型(简单类型.原始类型):String.N ...
- SVN重新定位操作流程
因服务器其他系统需要SVN原来使用的443端口无法继续使用需要更换,服务器SVN端口已更新为8443,现将个人电脑SVN文件夹路径重新定位流程展示如下: 确定即修改成功!
- 面试 3-5年Java开发后的感受
在上周,我密集面试了若干位Java后端的候选人,工作经验在3到5年间.我的标准其实不复杂:第一能干活,第二Java基础要好,第三最好熟悉些分布式框架,我相信其它公司招初级开发时,应该也照着这个标准来面 ...
- React: 研究Redux的使用
一.简介 在上一篇文章中,大概讲了下Flux设计模式的使用,在末尾顺便提了一些基于Flux的脚本库,其中Redux已经毋庸置疑地成为了众多脚本库的翘楚之一.是的,Redux是基于Flux开发的,Red ...
- Java 匿名类和lambda表达式
一.匿名内部类 一个匿名内部类是一个没有名字的内部类.它将进一步定义一个内部类以及创建一个内部类的实例. 内部类处理器可以使用匿名内部类进行代码简化. 匿名内部类的语法如下所示: new SuperC ...
- 松软科技课堂:JS HTML DOM
通过 HTML DOM,JavaScript 能够访问和改变 HTML 文档的所有元素. HTML DOM(文档对象模型) 当网页被加载时,浏览器会创建页面的文档对象模型(Document Objec ...
- SpannableStringBuilder实现图文混排
1.我的后面添加图片 ssb = new SpannableStringBuilder("我的后面添加图片: "); ssb.setSpan(, , Spannable.SPAN_ ...