spiral-matrix-ii &i 生成顺时针序列
I:
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].
按顺时针取序列,因为序列不一定是正矩阵,所以需要每取完一个方向要当即--或++,并做判断是否需要再取下一个方向。
当取完left->right,需要top++表明上一行已取完,top->bottom需right--,right->left需先判断top<=bottom避免只有单行重复取了上行,而后bottom++,bottom->top需先判断left<=right避免重复取右列,而后left++;
class Solution {
public:
vector<int> spiralOrder(vector<vector<int> > &matrix) {
int m=matrix.size();
vector<int> res;
if(m==)
return res;
int n=matrix[].size();
int left=,right=n-,top=,bottom=m-;
int i=;
while(left<=right&&top<=bottom)
{
for(i=left;i<=right;++i)
res.push_back(matrix[top][i]);
top++;
for(i=top;i<=bottom;++i)
res.push_back(matrix[i][right]);
right--;
if(top<=bottom){
for(i=right;i>=left;--i)
res.push_back(matrix[bottom][i]);
}
bottom--;
if(left<=right){
for(i=bottom;i>=top;--i)
res.push_back(matrix[i][left]);
}
left++;
}
return res;
}
};
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 ]
]
注意左->右可遍历全,剩余两个方向遍历时需+1,最后一个方向掐头去尾避免重复遍历
class Solution {
public:
vector<vector<int> > generateMatrix(int n) {
vector<vector<int>> result(n,vector<int>(n));
if(n==)
return result;
int step = ;
int left = ; int right = n-; int top = ; int bottom = n-;
while(left<=right && top<=bottom)
{
// 左->右
for(int i=left;i<=right;i++) {
result[top][i] = step;
step++;
}
//上->下
for(int i=top+;i<=bottom;i++) {
result[i][right] = step;
step++;
}
//右->左
for(int i=right-;i>=left;i--) {
result[bottom][i] = step;
step++;
}
//下->上
for(int i=bottom-;i>top;i--) {
result[i][left] = step;
step++;
}
left++; right--; top++; bottom--;
}
return result;
}
};
spiral-matrix-ii &i 生成顺时针序列的更多相关文章
- 【leetcode】Spiral Matrix II
Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 to n2 in s ...
- 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 ...
- Spiral Matrix II
Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 to n2 in s ...
- leetcode 54. Spiral Matrix 、59. Spiral Matrix II
54题是把二维数组安卓螺旋的顺序进行打印,59题是把1到n平方的数字按照螺旋的顺序进行放置 54. Spiral Matrix start表示的是每次一圈的开始,每次开始其实就是从(0,0).(1,1 ...
- LeetCode: Spiral Matrix II 解题报告-三种方法解决旋转矩阵问题
Spiral Matrix IIGiven an integer n, generate a square matrix filled with elements from 1 to n2 in sp ...
- 【leetcode】59.Spiral Matrix II
Leetcode59 Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 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 ...
- C#LeetCode刷题之#59-螺旋矩阵 II(Spiral Matrix II)
目录 问题 示例 分析 问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3678 访问. 给定一个正整数 n,生成一 ...
- 【LeetCode】59. Spiral Matrix II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 维护四个边界和运动方向 保存已经走过的位置 日期 题 ...
- 59. Spiral Matrix II
题目: Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. ...
随机推荐
- 《UNIX环境高级编程(第3版)》
<UNIX环境高级编程(第3版)> 基本信息 原书名:Advanced Programming in the UNIX Environment (3rd Edition) (Addison ...
- Ubuntu下搭建Hyperledger Fabric v1.0环境
多次尝试才正常启动了Fabric,如遇到各种莫名错误,请参考如下一步步严格安装,特别用户权限需要注意. 一.安装Ubuntu16 虚拟机或双系统,虚拟机有VirtualBox或者VMware,Ub ...
- 【ContestHunter】【弱省胡策】【Round7】
Prufer序列+高精度+组合数学/DP+可持久化线段树 Magic 利用Prufer序列,我们考虑序列中每个点是第几个插进去的,再考虑环的连接方式,我们有$$ans=\sum_{K=3}^n N^{ ...
- Sutherland-Hodgeman多边形裁剪
原文地址:http://course.cug.edu.cn/cugFirst/computer_graphics/class/course/3-3-1-a.htm
- easyloader分析与使用
转载自:http://www.cnblogs.com/haogj/archive/2013/04/22/3036685.html 使用脚本库总要加载一大堆的样式表和脚本文件,在 easyui 中,除了 ...
- Javassist 字节码 简介 案例 MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- Java复习1-基本数据类型
数据类型 整形 type 存储 取值范围 int 4字节 -2 147 483 648 ~ 2 147 483 647 (超过20亿) short 2字节 -32 768 ~ 32 7677 long ...
- chrome中打开 swf下载的问题
https://helpx.adobe.com/cn/flash-player/kb/enabling-flash-player-chrome.html 1. 在地址栏中,键入 chrome://se ...
- Java归去来第2集:利用Eclipse创建Maven Web项目
一.前言 如果还不了解剧情,请返回第一集的剧情 Java归去来第1集:手动给Eclipse配置Maven环境 二.利用Eclipse创建Maven Web项目 选择File-New- ...
- retrofit+RXjava二次封装
接入说明:项目中已集成RXjava,RXandroid.Retrofit,为避免包冲突,不须要再次接入. 就可以直接使用RXjava,Retrofit的所有api. github地址:https:// ...