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 * ...
随机推荐
- 数据结构 - 顺序队列的实行(C语言)
数据结构-顺序队列的实现 1 顺序队列的定义 线性表有顺序存储和链式存储,队列作为一种特殊的线性表,也同样存在这两种存储方式.我们先来看队列的顺序存储结构. 队列的顺序储存结构:用数组存储队列,为了避 ...
- 【洛谷2304_LOJ2134】[NOI2015]小园丁与老司机(动态规划_网络流)
题目: 洛谷 2304 LOJ 2134 (LOJ 上每个测试点有部分分) 写了快一天 -- 好菜啊 分析: 毒瘤二合一题 -- 注意本题(及本文)使用 \(x\) 向右,\(y\) 向上的「数学坐标 ...
- 1-最全CSS3选择器
一,CSS3 选择器分类 二,选择器语法 1,基本选择器语法 选择器 类型 功能描述 * 通配选择器 选择文档中所以HTML元素 E 元素选择器 选择指定类型的HTML元素 #id ID选择器 ...
- 使用 JSX 描述 UI 信息
这一节我们通过一个简单的例子讲解 React.js 描述页面 UI 的方式.把 src/index.js 中的代码改成: import React, { Component } from 'react ...
- Activiti工作流和shiro权限管理关系图
- MySQL DECIMAL数据类型
https://blog.csdn.net/zyz511919766/article/details/49335565
- Java基础教程(24)--集合
一.Java集合框架 集合,有时也称为容器,是一个用来存储和管理多个元素的对象.Java中的集合框架定义了一套规范,用来表示和操作集合,使具体操作与实现细节解耦.集合框架都包含下列内容: 接口:这 ...
- IIS 安装了.net framework 4.0/4.5 却找不到相应应用程序池
通常情况下是因为没注册造成的,有些安装包会自己帮你注册上有些不会,感觉略坑. 注册方法:在计算机中点击 开始菜单–>运行 拷贝以下内容运行一下即可. C:\WINDOWS\Microsoft.N ...
- iOS Programming UISplitViewController
iOS Programming UISplitViewController The iPad, on the other hand, has plenty of screen space to pr ...
- .NET Core MVC Web最最最基础的框架搭建
1. 使用VS创建.NET Core MVC Web项目 创建完成就是酱紫的了 2. 用NuGet把这些全部都安装了 Install-Package Microsoft.EntityFramework ...