【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, ...
随机推荐
- 【转载】#324 - A Generic Class Can Have More than One Type Parameter
A generic class includes one or more type parameters that will be substituted with actual types when ...
- 官方发布PHP语法规范
PHP语言已经存在了超过20年,显然是世界上最流行的编程语言之一.PHP绝对是互联网服务器端web编程的通用语. 虽然有广泛的user-documentation,PHP语言总是错过语言规范.这并不是 ...
- selenium项目--读取测试用例
读取测试用例 一直我们都没有考虑过读取测试用例的事,我们现在这样设计测试用例有两个好的点,在执行方法时,打印测试用例,方便知道执行的内容是什么,在报告展示时,把测试用例的结果展示出来 实现方案:目前我 ...
- Android(java)学习笔记66:Android Studio中build.gradle简介
1.首先我们直接上代码介绍: // Top-level build file where you can add configuration options common to all sub-pro ...
- 【UOJ139】【UER #4】被删除的黑白树(贪心)
点此看题面 大致题意: 请你给一棵树黑白染色,使每一个叶结点到根节点的路径上黑节点个数相同. 贪心 显然,按照贪心的思想,我们要让叶结点到根节点的路径上黑节点的个数尽量大. 我们可以用\(Min_i\ ...
- Next K Permutation
3457: Next K Permutation 时间限制: 1 Sec 内存限制: 128 MB提交: 4 解决: 4[提交] [状态] [讨论版] [命题人:admin] 题目描述 n 个数有 ...
- 正则表达式 /i /g /m /ig /gi
正则表达式中/i,/g,/ig,/gi,/m的区别和含义 /i (忽略大小写) /g (全文查找出现的所有匹配字符) /m (多行查找) / /ig(全文查找.忽略大小写)
- macOS Sierra系统偏好设置->安全性和隐私->通用中的“任何来源” 选项开与关
显示"任何来源"选项在控制台中执行: sudo spctl --master-disable 不显示"任何来源"选项(macOS 10.12默认为不显示)在控制 ...
- CSS中margin: 0 auto;样式没有生效
问题:有两个元素: A, B.两则是嵌套关系,A是B的父节点.A和B都是块元素.当在A上设置:margin: 0 auto的时候,B并没有在页面中居中. margin: 0 auto 为什么没有生效? ...
- Java面试不得不知的问题(一)
程序员面试 1. 面向对象的特征有哪些方面 · 抽象:抽象就是忽略一个主题中与当前目标无关的那些方面,以便更充分地注意与当前目标有关的方面.抽象并不打算了解全部问题,而只是选择其中的一部分, ...