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 order.
For example,
Given the following matrix:
- [
- [ 1, 2, 3 ],
- [ 4, 5, 6 ],
- [ 7, 8, 9 ]
- ]
You should return [1,2,3,6,9,8,7,4,5]
.
- vector<int> spiralOrder(vector<vector<int>>& matrix) {
- vector<int> ans;
- int m = matrix.size();
- if(m < )
- return ans;
- int n = matrix[].size(), size = m*n, x1=, x2=m-, y1=, y2=n-, i, j, k=;
- while(k < size)
- {
- for(i=y1; i<=y2; i++)
- {
- ans.push_back(matrix[x1][i]);
- k++;
- }
- for(i=x1+; i<=x2; i++)
- {
- ans.push_back(matrix[i][y2]);
- k++;
- }
- for(i=y2-; x2>x1 && i>=y1; i--)
- {
- ans.push_back(matrix[x2][i]);
- k++;
- }
- for(i=x2-; y2>y1 && i>x1; i--)
- {
- ans.push_back(matrix[i][y1]);
- k++;
- }
- x1++; x2--; y1++; y2--;
- }
- return ans;
- }
注意:
下面和左面的边,即第三个和第四个for循环,要加条件x2>x1和y2>y1,否则会有重复元素。
2.
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.
For example,
Given n = 3
,
You should return the following matrix:
- [
- [ 1, 2, 3 ],
- [ 8, 9, 4 ],
- [ 7, 6, 5 ]
- ]
- vector<vector<int>> generateMatrix(int n) {
- vector<vector<int>> ans(n, vector<int>(n, ));
- int size = n*n, x1=, x2=n-, y1=, y2=n-, i, j, k=;
- while(k < size)
- {
- for(i=y1; i<=y2; i++)
- ans[x1][i] = ++k;
- for(i=x1+; i<=x2; i++)
- ans[i][y2] = ++k;
- for(i=y2-; x2>x1 && i>=y1; i--)
- ans[x2][i] = ++k;
- for(i=x2-; y2>y1 && i>x1; i--)
- ans[i][y1] = ++k;
- x1++; x2--; y1++; y2--;
- }
- return ans;
- }
54. 59. Spiral Matrix的更多相关文章
- 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 ...
- leetcode 54. Spiral Matrix 、59. Spiral Matrix II
54题是把二维数组安卓螺旋的顺序进行打印,59题是把1到n平方的数字按照螺旋的顺序进行放置 54. Spiral Matrix start表示的是每次一圈的开始,每次开始其实就是从(0,0).(1,1 ...
- 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: 59. Spiral Matrix II(Medium)
1. 原题链接 https://leetcode.com/problems/spiral-matrix-ii/description/ 2. 题目要求 给定一个正整数n,求出从1到n平方的螺旋矩阵.例 ...
- 59. Spiral Matrix II(中等,同54题)
Given an integer \(n\), generate a square matrix filled with elements from 1 to \(n^2\) in spiral or ...
- 【一天一道LeetCode】#59. Spiral Matrix II
一天一道LeetCode系列 (一)题目 Given an integer n, generate a square matrix filled with elements from 1 to n2 ...
- [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 ...
- 【LeetCode】59. Spiral Matrix II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 维护四个边界和运动方向 保存已经走过的位置 日期 题 ...
- 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 ...
随机推荐
- P2158/bzoj2190 [SDOI2008]仪仗队
P2158 [SDOI2008]仪仗队 欧拉函数 计算下三角的点数再*2+1 观察斜率,自行体会 #include<iostream> #include<cstdio> #in ...
- windows tomcat web应用以及eclipse console乱码解决方法
在windows下,如果vm文件名为UTF-8格式,则显示乱码(velocity写出的不乱码): 改回GBK,则不再乱码.
- JS引擎深入分析
转载自阮一峰:http://javascript.ruanyifeng.com/bom/engine.html 目录 JavaScript代码嵌入网页的方法 script标签:代码嵌入网页 scrip ...
- noip2010 真题练习 2017.2.18
第一题比较简单,用exist数组判断是否在循环队列中,就可实现线性算法. Code #include<iostream> #include<cstdio> #include&l ...
- 标准IO与文件IO 的区别【转】
本文转载自:http://blog.sina.com.cn/s/blog_63f31f3401013jrn.html 先来了解下什么是标准IO以及文件IO. 标准IO:标准I/O是ANSI C建立的一 ...
- ubuntu下git clone 提速
环境:ubuntu16.04 方法:通过socks5代理并且使用http链接 步骤: 1.设置全局使用socks5代理,并且使用http传输 git config --global http.prox ...
- JavaScript 各种验证收集
filter或者forEach函数,可能是因为你的浏览器还不够新,暂时不支持新标准的函数,你可以使用如下方式自己定义: if (!Array.prototype.forEach) { Array.pr ...
- 【第十九章】 springboot + hystrix(1)
hystrix是微服务中用于做熔断.降级的工具. 作用:防止因为一个服务的调用失败.调用延时导致多个请求的阻塞以及多个请求的调用失败. 1.pom.xml(引入hystrix-core包) 1 < ...
- Linux批量更改文件后缀-转载
一.rename解决 1. Ubuntu系统下 rename 's//.c//.h/' ./* 把当前目录下的后缀名为.c的文件更改为.h的文件 2. CentOS5.5系统下 rename . ...
- [tixml]保存,读取
保存: //xml的实体 TiXmlElement* rootElement = new TiXmlElement("spark"); rootElement->SetAtt ...