【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, ...
随机推荐
- *389. Find the Difference (string + map(26)) read problems carefully
Given two strings s and t which consist of only lowercase letters. String t is generated by random s ...
- 在vue中使用插槽 slot
插槽(slot)这个概念非常重要 插槽的使用场景1:在子组件里面显示父组件的dom <div id='root'> <child content = '<p>Dell&l ...
- Spring boot 异常处理配置
1. 新建Maven项目 exception 2. pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0&quo ...
- Maven中maven-source-plugin,maven-javadoc-plugin插件的使用:
摘要:今天领导说要把项目通过maven生产源码包和文档包并发布到自己的私服上,经过查看mavne官网发现有两个maven插件可以做到这些工作,一个是maven-source-plugin,另一个是ma ...
- elsevier期刊要求翻译
百度文库 http://wenku.baidu.com/view/e20a27e84afe04a1b071de4e.html 官网文档 http://www.elsevier.com/journals ...
- javascript中的作用域与作用域链
前几天,在写一段js代码时,出现了一些问题,调了很长时间也没有调通,其原因是,我在处理变量的作用域时错误地沿用了C++的作用域机制.因此我回炉了一次. 如果你使用过C++或java等一系列的面向对象的 ...
- Spring 注解中@Resource 和 @Authwired 的区别
@Resource的作用相当于@Autowired,只不过@Autowired按byType自动注入,而@Resource默认按 byName自动注入罢了.@Resource有两个属性是比较重要的,分 ...
- orale 10g和11g中的自动统计任务
orale 10g和11g中的自动统计任务 博客分类: 数据库相关/oracle 1) 先来看下oracle 10g中的自动统计任务的问题. 从Oracle Database 10g开始,Or ...
- 1801: [Ahoi2009]chess 中国象棋
Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 2520 Solved: 1524[Submit][Status][Discuss] Descripti ...
- 【动态规划】cf1034C. Region Separation
质因数分解套路的复杂度分析的动态规划 题目大意 有一颗$n$个节点有点权的树,初始整棵树为$1$号区域,要求满足下列规则: 除非$i$是最后一个等级,否则每一个$i$级区域都要被分成至少两个$i+1$ ...