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 * ...
随机推荐
- YCOJ中国邮递员问题
题目: Description 一个邮递员从邮局出发,需要去 n - 2个城市送信,送完信件以后回家. 邮局在城市 1,家在城市 n,任意两个城市之间都有道路,但是这些道路是单向,也就是说 a 到 b ...
- iOS APNs远程推送流程精简版
1.去Apple Developer Center里创建应用的信息,指定APP ID(Bundle ID),配置里开启推送功能(Push Notifications). 后续步骤需要用到这个应用的包名 ...
- JavaScript--DOM浏览器窗口可视区域大小
浏览器窗口可视区域大小 获得浏览器窗口的尺寸(浏览器的视口,不包括工具栏和滚动条)的方法: 一.对于IE9+.Chrome.Firefox.Opera 以及 Safari: • window.inn ...
- matlab学习路线
知乎:matlab学习路线 MATLAB数学教学视频
- Android Dialogs(2)最好用DialogFragment创建Dialog
Creating a Dialog Fragment You can accomplish a wide variety of dialog designs—including custom layo ...
- python的des和3des加解密
1.加密: pyDes.des(key, [mode], [IV], [pad], [padmode]) pyDes.triple_des(key, [mode], [IV], [pad], [pad ...
- [转]linq to sql (Group By/Having/Count/Sum/Min/Max/Avg操作符)
本文转自:http://www.cnblogs.com/jack-liang/archive/2011/03/22/1991554.html Group By/Having操作符 适用场景:分组数据, ...
- gulp构建工具学习汇总
前端脚手架____gulp配置文件------- https://pan.baidu.com/s/1eSs7COy 1:有了package.json 直接 npm install自动下载相应的npm包 ...
- 依赖注入(IOC) 详解
https://blog.csdn.net/qq_27093465/article/details/52547290 https://blog.csdn.net/qq_27093465/article ...
- Hadoop YARN学习之监控集群监控Nagios(4)
doop YARN学习之监控集群监控Nagios(4) 1. Nagios是一个流行的开源监控工具,可以用来监控Hadoop集群. 2. 监控基本的Hadoop服务 调试好脚本后命名为chek_res ...