【Spiral Matrix】cpp
题目:
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].
代码:
class Solution {
public:
vector<int> spiralOrder(vector<vector<int>>& matrix) {
vector<int> ret;
const int m = matrix.size();
if (m<) return ret;
const int n = matrix[].size();
const int circle = std::min(n, m)/;
for ( int c=; c<circle; ++c )
{
// traversal a circle
// up row
for ( int col=c; col<n-c; ++col ) ret.push_back(matrix[c][col]);
// right col
for ( int row=c+; row<m-c-; ++row ) ret.push_back(matrix[row][n--c]);
// down row
for ( int col=n--c; col>=c; --col ) ret.push_back(matrix[m--c][col]);
// left col
for ( int row=m-c-; row>c; --row ) ret.push_back(matrix[row][c]);
}
// if odd
if ( std::min(n, m) & ){
if ( m>=n ){
for ( int row=circle; row<m-circle; ++row ) ret.push_back(matrix[row][circle]);
}
else{
for ( int col=circle; col<n-circle; ++col ) ret.push_back(matrix[circle][col]);
}
}
return ret;
}
};
tips:
1. 首先确定要绕几圈:取行和列中小的,除以2,得到绕几圈(如果是偶数正好绕完;奇数剩中间的一行或一列)
2. 按照题中给的顺序绕(上 右 下 左)
3. ‘绕’循环出来之后,判断行列中较小的那个是奇数还是偶数(位运算判断):如果是偶数则不用处理;如果是奇数,需要判断剩下的是‘一行’还是‘一列’(行列相等的情况可以归到剩下一行的情况中)
完毕。
===========================================
第二次过这道题,第一次没有想到分奇数偶数讨论;考虑了之后AC了。
class Solution {
public:
vector<int> spiralOrder(vector<vector<int>>& matrix) {
vector<int> ret;
if ( matrix.empty() ) return ret;
const int M = matrix.size(); // row
const int N = matrix[].size(); // column
const int C = min(M,N)/; // circle
for ( int i=; i<C; ++i )
{
// north
for ( int p=i; p<N-i; ++p ) ret.push_back(matrix[i][p]);
// east
for ( int p=i+; p<M-i-; ++p ) ret.push_back(matrix[p][N--i]);
// south
for ( int p=i; p<N-i; ++p ) ret.push_back(matrix[M--i][N--p]);
// west
for ( int p=i+; p<M-i-; ++p ) ret.push_back(matrix[M--p][i]);
}
if ( min(M,N) & )
{
if ( M<N )
{
for ( int i=C; i<N-C; ++i) ret.push_back(matrix[C][i]);
}
else
{
for ( int i=C; i<M-C; ++i ) ret.push_back(matrix[i][C]);
}
}
return ret;
}
};
【Spiral Matrix】cpp的更多相关文章
- 【Spiral Matrix II】cpp
题目: Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. ...
- 【Search a 2D Matrix】cpp
题目: Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the f ...
- 【Valid Sudoku】cpp
题目: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...
- hdu 4739【位运算】.cpp
题意: 给出n个地雷所在位置,正好能够组成正方形的地雷就可以拿走..为了简化题目,只考虑平行于横轴的正方形.. 问最多可以拿走多少个正方形.. 思路: 先找出可以组成正方形的地雷组合cnt个.. 然后 ...
- Hdu 4734 【数位DP】.cpp
题意: 我们定义十进制数x的权值为f(x) = a(n)*2^(n-1)+a(n-1)*2(n-2)+...a(2)*2+a(1)*1,a(i)表示十进制数x中第i位的数字. 题目给出a,b,求出0~ ...
- 【Maximal Rectangle】cpp
题目: Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones ...
- 【Sudoku Solver】cpp
题目: Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated b ...
- 【Rotate Image】cpp
题目: You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwis ...
- 【Python矩阵及其基础操作】【numpy matrix】
一.矩阵生成 1.numpy.matrix: import numpy as np x = np.matrix([ [1, 2, 3],[4, 5, 6] ]) y = np.matrix( [1, ...
随机推荐
- FTP无法连接可能是安全狗设置的原因
防火墙已添加某端口,但仍无法连接.可能是安全狗导致的. 如果安装了安全狗,请按照以下步骤设置:
- graphql 数据增删改查分页及关联操作(三)
说明: 接第二篇文章,代码也是在第二篇文章之上 本文只是针对mondodb来操作 一.添加相关的包 yarn add Mongoose 二.初始化Mongodb 修改server.ts 导入 impo ...
- webpack了解
一.理解webpack 什么是webpack? 是一个模块打包器.它的主要目标是将 JavaScript 文件打包在一起,打包后的文件用于在浏览器中使用,但它也能够胜任转换(transform).打包 ...
- Objectbox Box的getAll() 函数返回emptylist() 未判断导致崩溃
最近使用了Objectbox作为新项目的数据库后台,Greendao开发团队新力作,但是Objectbox算是比较新的一个东西,现在资料也不多. 今天跟大家分享一个关于Box类的getAll()函数的 ...
- Django Field lookups (字段查找)
字段查找是指定SQL WHERE子句的核心内容的方式. 它们被指定为QuerySet方法filter().exclude()和get()的关键字参数. 1.exact:精确查找.如果为比较提供的值为N ...
- mysql5.6之前需要账号的安全加固
mysql5.6之前需要账号的安全加固 从5.7开始就不需要了. delete from mysql.user where user!='root' or host='localhost'; flus ...
- percona-toolkit 工具集安装
下载地址: www.percona.com/downloads/percona-toolkit 安装方法一,源码安装: perl Makefile.PL make:make install ...
- pycharm中常用设置
当安装时检查版本过低 首先 pip --help 进入帮助,找到 复制,然后 pip install --disable-pip-version-check 要安装的包 这样就会跳过版本检测. 在py ...
- 前端小记4——高性能mobile web开发
1.高性能CSS3动画 与PC端场景需要相比,移动web端需要考虑的因素也相对复杂,重点考虑:流量.功耗与流畅度.在pc端上考虑更多的是流畅度,而mobile web中需要考虑网络流量的使用和耗电情况 ...
- MySQL-常用的存储引擎
MySQL-常用的存储引擎 存储引擎 事务 锁粒度 主要应用 忌用 MyISAM 不支持 支持并发插入的表级锁 select,insert 读写操作频繁 MRG_MYISAM 不支持 支持并发插入的表 ...