题目:

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的更多相关文章

  1. 【Spiral Matrix II】cpp

    题目: Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. ...

  2. 【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 ...

  3. 【Valid Sudoku】cpp

    题目: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...

  4. hdu 4739【位运算】.cpp

    题意: 给出n个地雷所在位置,正好能够组成正方形的地雷就可以拿走..为了简化题目,只考虑平行于横轴的正方形.. 问最多可以拿走多少个正方形.. 思路: 先找出可以组成正方形的地雷组合cnt个.. 然后 ...

  5. 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~ ...

  6. 【Maximal Rectangle】cpp

    题目: Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones ...

  7. 【Sudoku Solver】cpp

    题目: Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated b ...

  8. 【Rotate Image】cpp

    题目: You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwis ...

  9. 【Python矩阵及其基础操作】【numpy matrix】

    一.矩阵生成 1.numpy.matrix: import numpy as np x = np.matrix([ [1, 2, 3],[4, 5, 6] ]) y = np.matrix( [1, ...

随机推荐

  1. jmeter参数化读取数据进行多次运行

    jmeter参数化数据,可以使用csv,还可以使用数据库的方式 1.使用csv读取数据 在线程组中,配置原件中,选择csv data set config 1.本地创建了16个数据,存为test.tx ...

  2. rsync安装配置实时同步

    一.简介 1.认识 Rsync(remote synchronize)是一个远程数据同步工具,可通过LAN/WAN快速同步多台主机间的文件.Rsync使用所谓的“Rsync算法”来使本地和远 程两个主 ...

  3. 线程 task pritce

    1.使用task类创建并执行简单任务: 使用task的构造函数来创建 任务,并调用start方法来启动任务,执行异步操作 aitAll用于等待提供的所有 System.Threading.Tasks. ...

  4. 一篇RxJava友好的文章(二)

    上一篇文章介绍了rxjava的基本用法,和一些常用的操作符,以及rxjava的链式操作带来的好处.由于rxjava非常的强大,让我如此的痴迷,我打算写五篇文章,专门讲解rxjava 常见的操作符和用法 ...

  5. C#后台动态添加Grid表格

    前面页面: <ScrollViewer x:Name=" BorderBrush="#25A0DA" VerticalScrollBarVisibility=&qu ...

  6. JQuery发起ajax请求,并在页面动态的添加元素

    页面html代码: <li> <div class="coll-tit"><span class="coll-icon">& ...

  7. 【例题收藏】◇例题·I◇ Snuke's Subway Trip

    ◇例题·I◇ Snuke's Subway Trip 题目来源:Atcoder Regular 061 E题(beta版) +传送门+ 一.解析 (1)最短路实现 由于在同一家公司的铁路上移动是不花费 ...

  8. spring-mybatis整合异常

    Failed to read artifact descriptor for XXXXXX:jar:XXXX.RELEAS 原因是maven的本地仓库没有设置好.在别处拷贝过来的项目会有自己的仓库位置 ...

  9. JQuery实现父级选择器(广告实现)

    效果图如下: HTML代码如下: <!DOCTYPE html> <html lang="en"> <head> <meta charse ...

  10. 交换机基础配置之跨交换机划分vlan

    我们以上面的拓扑图来进行实验 四台pc机都在同一网段 pc1和pc2在同一台交换机上 pc3和pc4在同一台交换机上 现在我们实验的目的就是将pc1和pc3划分到同一vlan pc2和pc4划分到同一 ...