题目:

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. java对字符串进行加密和解密(以下是来自其他博主)

    背景:需要对读取数据库配置的文件进行加密,防止他人拿到数据,而对自己的代码,有要实现进行解密,网上给的加密方式,什么MD5,base64,还有等等,都太复杂,而且有些是单向的,只加密不解密,以下代码, ...

  2. C sharp #003# 面向对象编程基本构件

    饮水思源:金老师的自学网站 索引 类的属性 简化字段/属性的初始化 命名空间 程序集 类的属性 字段+get/set方法=属性 (之前都是把字段和属性混着用..) 经典写法: using System ...

  3. 20145238-荆玉茗 《Java程序设计》第一周学习总结

    20145238 <Java程序设计>第一周学习总结 教材学习内容总结 Java三大平台:由于java领域的应用越来越广,根据不同级别的应用开发区分了不同的应用版本,后正式更名为Java ...

  4. ioc 的好文章 转自 GavinJun

    https://www.cnblogs.com/fuchongjundream/p/3873073.html

  5. 尝试将 SCRIPT ompbox\private\ompmex 作为函数执行

    1.安装VS2010 2.配置ombox 在ombox路径下 mex -setup C++ 然后 make

  6. mayan 游戏真是毒瘤

    如题 真坑呀!!! 可算过了 我率先达到了氧气富有化 先是改变时没有fall.40分 然后是fall函数写慢了 tle 50分 . 上代码 #include<cstdio> #includ ...

  7. Maven 引入war工程【work】

    场景: 之前为了便于查看生产者项目缓存情况,做了一套界面,用来查看刷新缓存.然而最近发现消费者项目上也需要这套缓存界面,因此打算将这套界面代码迁移成独立的web项目,然后由生产者和消费者通过POM文件 ...

  8. scoped,会使设置UI组件库的样式识别不出来

    未设置 scoped 作用域:显示效果 设置作用域的效果:ui组件默认的值(你怎么设置都不管用)

  9. MySQL视图、事务

    view(视图):虚拟表主要用来看(查)数据基表的数据变化会在视图中体现出来 权限控制将多表查询的结果整合在视图中方便用户查看 create view v1 as select ...查询语句WITH ...

  10. scrapy--selenium(二)

    今天学习了很多,还是想给大家讲一讲正题:scrapy的动态加载AJax的网页爬取:selenium.让我们开始 三: 针对大型电商网站:京东网,因为比较有代表性,爬出来有点小成就.先给大家看下效果图. ...