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. [ 1, 2, 3 ],
  3. [ 4, 5, 6 ],
  4. [ 7, 8, 9 ]
  5. ]

You should return [1,2,3,6,9,8,7,4,5].

打印螺旋矩阵

逐个环的打印, 对于m *n的矩阵,环的个数是 (min(n,m)+1) / 2。对于每个环顺时针打印四条边。

注意的是:最后一个环可能只包含一行或者一列数据

  1. class Solution {
  2. public:
  3. vector<int> spiralOrder(vector<vector<int> > &matrix) {
  4. int m = matrix.size(), n;
  5. if(m != 0)n = matrix[0].size();
  6. int cycle = m > n ? (n+1)/2 : (m+1)/2;//环的数目
  7. vector<int>res;
  8.  
  9. int a = n, b = m;//a,b分别为当前环的宽度、高度
  10. for(int i = 0; i < cycle; i++, a -= 2, b -= 2)
  11. {
  12. //每个环的左上角起点是matrix[i][i],下面顺时针依次打印环的四条边
  13. for(int column = i; column < i+a; column++)
  14. res.push_back(matrix[i][column]);
  15. for(int row = i+1; row < i+b; row++)
  16. res.push_back(matrix[row][i+a-1]);
  17. if(a == 1 || b == 1)break; //最后一个环只有一行或者一列
  18. for(int column = i+a-2; column >= i; column--)
  19. res.push_back(matrix[i+b-1][column]);
  20. for(int row = i+b-2; row > i; row--)
  21. res.push_back(matrix[row][i]);
  22. }
  23. return res;
  24. }
  25. };

Spiral Matrix II

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

For example,
Given n = 3,

You should return the following matrix:

  1. [
  2. [ 1, 2, 3 ],
  3. [ 8, 9, 4 ],
  4. [ 7, 6, 5 ]
  5. ]

本质上和上一题是一样的,这里我们要用数字螺旋的去填充矩阵。同理,我们也是逐个环的填充,每个环顺时针逐条边填充                 本文地址

  1. class Solution {
  2. public:
  3. vector<vector<int> > generateMatrix(int n) {
  4. vector<vector<int> > matrix(n, vector<int>(n));
  5. int a = n;//a为当前环的边长
  6. int val = 1;
  7. for(int i = 0; i < n/2; i++, a -= 2)
  8. {
  9. //每个环的左上角起点是matrix[i][i],下面顺时针依次填充环的四条边
  10. for(int column = i; column < i+a; column++)
  11. matrix[i][column] = val++;
  12. for(int row = i+1; row < i+a; row++)
  13. matrix[row][i+a-1] = val++;
  14. for(int column = i+a-2; column >= i; column--)
  15. matrix[i+a-1][column] = val++;
  16. for(int row = i+a-2; row > i; row--)
  17. matrix[row][i] = val++;
  18. }
  19. if(n % 2)matrix[n/2][n/2] = val;//n是奇数时,最后一个环只有一个数字
  20. return matrix;
  21. }
  22. };

【版权声明】转载请注明出处:http://www.cnblogs.com/TenosDoIt/p/3774747.html

LeetCode:Spiral Matrix I II的更多相关文章

  1. LeetCode: Spiral Matrix II 解题报告-三种方法解决旋转矩阵问题

    Spiral Matrix IIGiven an integer n, generate a square matrix filled with elements from 1 to n2 in sp ...

  2. [LeetCode] Spiral Matrix II 螺旋矩阵之二

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

  3. [Leetcode] spiral matrix ii 螺旋矩阵

    Given an integer n, generate a square matrix filled with elements from 1 to n 2 in spiral order. For ...

  4. [LeetCode] Spiral Matrix 螺旋矩阵

    Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...

  5. Spiral Matrix I & II

    Spiral Matrix I Given an integer n, generate a square matrix filled with elements from 1 to n^2 in s ...

  6. [LeetCode]Spiral Matrix 54

    54.Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the ma ...

  7. LeetCode: Spiral Matrix 解题报告

    Spiral MatrixGiven a matrix of m x n elements (m rows, n columns), return all elements of the matrix ...

  8. [leetcode]Spiral Matrix II @ Python

    原题地址:https://oj.leetcode.com/problems/spiral-matrix-ii/ 题意: Given an integer n, generate a square ma ...

  9. Leetcode Spiral Matrix II

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

随机推荐

  1. WebBrowser 中遍历所有的frames

    枚举所有iframe的IWebBrowser2 // Get the IDispatch of the document. // LPDISPATCH lpDisp = NULL; lpDisp = ...

  2. 苹果会在明后年推出13寸屏iPad吗?

    摘要:苹果推大屏iPad的传闻由来已久,近日有国外媒体再次撰文称,这种大屏iPad不仅是苹果Mac继任者,同时也是Surface的有利竞争者……这真的可能吗?这只是分析师的捕风捉影,还是真有这种可能? ...

  3. [Chapter 3 Process]Practice 3.9 Describe the actions token by a kernel to content-switch between processes.

    3.9 Describe the actions token by a kernel to content-switch between processes. 答案: 内核在进行进程上下文切换时, 首 ...

  4. VMware12 安装 CentOS 6.5 64位

    前言:本人在配置Hadoop的过程中,需要搭建Cent OS 64 环境,借此,顺便将Cent OS 64 的安装在此记录,方便自己,也方便大家学习.本次是在VM12虚拟机中实现Cent OS 64 ...

  5. ORACLE升级的一些事

    一.SQL> @?/rdbms/admin/catupgrd.sql 说明:? 代表 ORACLE_HOME,在Linux中可能以 $ORACLE_HOME表示. @ 表示执行脚本 参考: ht ...

  6. Windows 8.1 应用再出发 - 几种更新的控件

    Windows 8.1 除了新增了很多很有用的控件外,还对一些控件做出了更新.接下来我们一起对这些更新的控件一一做出讲解. 1. FlipView 更新 翻转视图控件,在应用中常用作图片等内容的翻页/ ...

  7. 3.1 SharePreference

    SharePreferences是用来存储一些简单配置信息的一种机制,使用Map数据结构来存储数据,以键值对的方式存储,采用了XML格式将数据存储到设备中,路径为:/data/data/<pac ...

  8. iPad上的Cookie到底有多长?

    [故事背景]: 公司某个站点,特别依赖Cookie的使用,而且用的比较狠.在设计之初想当然地以为到达Cookie上限是猴年马月的事儿,没想到时过境迁,这个上限真的来了. 着手改吧,也不想投入太多.于是 ...

  9. [转]轻松解决oracle11g 空表不能exp导出的问题

    转自:http://www.2cto.com/database/201109/105931.html oracle11g的新特性,数据条数是0时不分配segment,所以就不能被导出. 解决方法: 1 ...

  10. Orchard Platform v1.8 发布

    发布说明: 1. 添加Json格式数据文件支持.2. 彻底删除了Settings, Modules, Themes模块.3. 删除了默认的ContentType,Site和User.4. 支持空库(无 ...