LeetCode(54)Spiral Matrix
题目
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].
分析
螺旋循环输出二维矩阵
AC代码
class Solution {
public:
vector<int> spiralOrder(vector<vector<int>>& matrix) {
if (matrix.empty())
return vector<int>();
vector<int> ret;
//输入矩阵行数
int m = matrix.size() - 1;
//输入矩阵的列数
int n = matrix[0].size() - 1;
for (int x = 0, y = 0; x <= m && y <= n; x++, y++)
{
//输出矩阵首行
for(int j=y ; j<=n ; ++j)
{
ret.push_back(matrix[x][j]);
}//while
//输出矩阵最右列
for (int i = x + 1; i <= m; ++i)
{
ret.push_back(matrix[i][n]);
}//while
//输出矩阵最底行
for (int j = n - 1; j >= y && x != m; --j)
{
ret.push_back(matrix[m][j]);
}
//输出矩阵最左列
for (int i = m - 1; i > x && y != n; --i)
{
ret.push_back(matrix[i][y]);
}
m--;
n--;
}//for
return ret;
}
};
LeetCode(54)Spiral Matrix的更多相关文章
- LeetCode(59)SPiral Matrix II
题目 Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. F ...
- LeetCode(54):螺旋矩阵
Medium! 题目描述: 给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素. 示例 1: 输入: [ [ 1, 2, 3 ], [ 4, 5, ...
- LeetCode(73)Set Matrix Zeroes
题目 Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. cli ...
- Qt 学习之路 2(54):剪贴板
Qt 学习之路 2(54):剪贴板 豆子 2013年6月8日 Qt 学习之路 2 2条评论 剪贴板的操作经常和前面所说的拖放技术在一起使用.大家对剪贴板都很熟悉.我们可以简单地把它理解成一个数据存储池 ...
- LeetCode(275)H-Index II
题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...
- LeetCode(220) Contains Duplicate III
题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...
- LeetCode(154) Find Minimum in Rotated Sorted Array II
题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...
- LeetCode(122) Best Time to Buy and Sell Stock II
题目 Say you have an array for which the ith element is the price of a given stock on day i. Design an ...
- LeetCode(116) Populating Next Right Pointers in Each Node
题目 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode * ...
随机推荐
- 基于.Net Core的API框架的搭建(1)
目标 我们的目标是要搭建一个API控制器的项目,API控制器提供业务服务. 一.开发框架搭建 1.开发前准备 开发前,我们需要下载如下软件,安装过程略: (1) 开发工具:VS2017 (2) 数据库 ...
- c语言程序设计案例教程(第2版)笔记(一)—零散、输入输出、最小公倍数、选择排序、冒泡排序
零散知识点: 非格式化输入输出:getchar().putchar() 格式化输入输出 :scanf().printf() 字符串输入输出 :gets() 或 scanf().puts() 或 ...
- Python爬虫库-Beautiful Soup的使用
Beautiful Soup 是一个可以从HTML或XML文件中提取数据的Python库,简单来说,它能将HTML的标签文件解析成树形结构,然后方便地获取到指定标签的对应属性. 如在上一篇文章通过爬虫 ...
- 配置yum源的步骤(阿里源)
配置yum源的步骤1.可以移除默认的yum仓库,也就是删除 /etc/yum.repos.d/底下所有的.repo文件(踢出国外的yum源) 1.配置yum源,找到阿里云的官方镜像源地址 https: ...
- 【BZOJ3309】DZY Loves Math(线性筛)
题目: BZOJ 3309 分析: 首先,经过一番非常套路的莫比乌斯反演(实在懒得写了),我们得到: \[\sum_{T=1}^n \sum_{d|T}f(d)\mu(\frac{T}{d})\lfl ...
- DP Codeforces Round #260 (Div. 1) A. Boredom
题目传送门 /* 题意:选择a[k]然后a[k]-1和a[k]+1的全部删除,得到点数a[k],问最大点数 DP:状态转移方程:dp[i] = max (dp[i-1], dp[i-2] + (ll) ...
- 彩色模型 分类: 图像处理 Matlab 2015-01-08 20:43 364人阅读 评论(0) 收藏
彩色模型(又称彩色空间或彩色系统)是描述色彩的一种方法,本质上,彩色模型就是坐标系统和子空间的规范,系统中的每种颜色由单个点来表示.下面介绍两种最常用的彩色模型. 一.RGB彩色模型: RGB模型是最 ...
- 关于k阶裴波那契序列的两种解法
在学校的anyview的时候,遇到了这个题: [题目]已知k阶裴波那契序列的定义为f(0)=0, f(1)=0, ..., f(k-2)=0, f(k-1)=1;f(n)=f(n-1)+f(n-2)+ ...
- 科普 eclipse中的Java build
在刚学eclipse的时候,build path是经常会用到的,但经常就是跟着教程走,额就不太懂这是干嘛的,然后今天看见极客视频里有相关的讲解,来记录一下. Build Path 是指定Java工程所 ...
- hdu 6012 Lotus and Horticulture 打标记
http://acm.hdu.edu.cn/showproblem.php?pid=6012 我们希望能够快速算出,对于每一个温度,都能够算出它在这n颗植物中,能得到多少价值. 那么,对于第i科植物, ...