题目

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;
}
};

GitHub测试程序源码

LeetCode(54)Spiral Matrix的更多相关文章

  1. LeetCode(59)SPiral Matrix II

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

  2. LeetCode(54):螺旋矩阵

    Medium! 题目描述: 给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素. 示例 1: 输入: [ [ 1, 2, 3 ], [ 4, 5, ...

  3. 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 ...

  4. Qt 学习之路 2(54):剪贴板

    Qt 学习之路 2(54):剪贴板 豆子 2013年6月8日 Qt 学习之路 2 2条评论 剪贴板的操作经常和前面所说的拖放技术在一起使用.大家对剪贴板都很熟悉.我们可以简单地把它理解成一个数据存储池 ...

  5. LeetCode(275)H-Index II

    题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...

  6. LeetCode(220) Contains Duplicate III

    题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...

  7. LeetCode(154) Find Minimum in Rotated Sorted Array II

    题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...

  8. 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 ...

  9. LeetCode(116) Populating Next Right Pointers in Each Node

    题目 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode * ...

随机推荐

  1. Luogu P1119 灾后重建 【floyd】By cellur925

    题目传送门 这道题我们很容易想到对于每次询问,都跑一遍最短路(spfa,虽然他已经死了).只需在松弛的时候加入当前相关的点是否已经修好的判断,果不其然的TLE了4个点. (然鹅我第一次用spfa跑的时 ...

  2. Qt - 锁屏界面加虚拟小键盘

    一.实现效果 鼠标点击"密码输入栏",弹出虚拟键盘,输入锁屏密码后,点击虚拟键盘外部区域,则会隐藏虚拟键盘,再点击登录,成功进入主界面. 二.虚拟键盘-程序设计 2.1 frmNu ...

  3. [ZJOI2011]道馆之战

    Description 口袋妖怪(又名神奇宝贝或宠物小精灵)红/蓝/绿宝石中的水系道馆需要经过三个冰地才能到达馆主的面前,冰地中的每一个冰块都只能经过一次.当一个冰地上的所有冰块都被经过之后,到下一个 ...

  4. 洛谷 P2061 [USACO07OPEN]城市的地平线City Horizon

    简化版的矩形面积并,不用线段树,不用离散化,代码意外的简单 扫描线,这里的基本思路就是把要求的图形竖着切几刀分成许多矩形,求面积并.(切法就是每出现一条与y轴平行的线段都切一刀) 对于每一个切出来的矩 ...

  5. Navicat无法连接Oracle数据库问题处理一例

    需要通过Navicat连接Oracle数据库进行数据迁移,发现无法连接,报如下错误信息: 按照百度中的说明配置了正确的oci. 此时又报如下错误: 问题解决: 经测试发现与软件的版本有关系,本机的Or ...

  6. 基于CentOS6.5下Suricata(一款高性能的网络IDS、IPS和网络安全监控引擎)的搭建(图文详解)(博主推荐)

    不多说,直接上干货! 为什么,要写这篇论文? 是因为,目前科研的我,正值研三,致力于网络安全.大数据.机器学习研究领域! 论文方向的需要,同时不局限于真实物理环境机器实验室的攻防环境.也不局限于真实物 ...

  7. Android 图片文件和Bitmap之间的转换

    String filePath="c:/01.jpg"; Bitmap bitmap=BitmapFactory.decodeFile(filePath); 如果图片过大,可能导致 ...

  8. spring.net应用

    经过一段时间的调试,终于把spring.net中关于aop的方面给做个了一个比较完整的Demo.包含异常日志和性能日志.spring.net和log4net配置. http://files.cnblo ...

  9. BotFramework学习-01

    微软在Build2016大会上表示,未来将是一个充满聊天机器人的世界,为此他们推出了微软Bot Framework,能够允许任何人制作自己的聊天机器人,微软则提供“cognitive microser ...

  10. iOS UI 顶级布局

    状态栏. 导航栏. tabbar. uiviewcontroller视图区域.