1.

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

  1. vector<int> spiralOrder(vector<vector<int>>& matrix) {
  2. vector<int> ans;
  3. int m = matrix.size();
  4. if(m < )
  5. return ans;
  6. int n = matrix[].size(), size = m*n, x1=, x2=m-, y1=, y2=n-, i, j, k=;
  7. while(k < size)
  8. {
  9. for(i=y1; i<=y2; i++)
  10. {
  11. ans.push_back(matrix[x1][i]);
  12. k++;
  13. }
  14. for(i=x1+; i<=x2; i++)
  15. {
  16. ans.push_back(matrix[i][y2]);
  17. k++;
  18. }
  19. for(i=y2-; x2>x1 && i>=y1; i--)
  20. {
  21. ans.push_back(matrix[x2][i]);
  22. k++;
  23. }
  24. for(i=x2-; y2>y1 && i>x1; i--)
  25. {
  26. ans.push_back(matrix[i][y1]);
  27. k++;
  28. }
  29. x1++; x2--; y1++; y2--;
  30. }
  31. return ans;
  32. }

注意:

下面和左面的边,即第三个和第四个for循环,要加条件x2>x1和y2>y1,否则会有重复元素。

2.

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. vector<vector<int>> generateMatrix(int n) {
  2. vector<vector<int>> ans(n, vector<int>(n, ));
  3. int size = n*n, x1=, x2=n-, y1=, y2=n-, i, j, k=;
  4. while(k < size)
  5. {
  6. for(i=y1; i<=y2; i++)
  7. ans[x1][i] = ++k;
  8. for(i=x1+; i<=x2; i++)
  9. ans[i][y2] = ++k;
  10. for(i=y2-; x2>x1 && i>=y1; i--)
  11. ans[x2][i] = ++k;
  12. for(i=x2-; y2>y1 && i>x1; i--)
  13. ans[i][y1] = ++k;
  14. x1++; x2--; y1++; y2--;
  15. }
  16. return ans;
  17. }

54. 59. Spiral Matrix的更多相关文章

  1. leetcode@ [54/59] Spiral Matrix & Spiral Matrix II

    https://leetcode.com/problems/spiral-matrix/ Given a matrix of m x n elements (m rows, n columns), r ...

  2. leetcode 54. Spiral Matrix 、59. Spiral Matrix II

    54题是把二维数组安卓螺旋的顺序进行打印,59题是把1到n平方的数字按照螺旋的顺序进行放置 54. Spiral Matrix start表示的是每次一圈的开始,每次开始其实就是从(0,0).(1,1 ...

  3. Leetcode 54. Spiral Matrix & 59. Spiral Matrix II

    54. Spiral Matrix [Medium] Description Given a matrix of m x n elements (m rows, n columns), return ...

  4. LeetCode: 59. Spiral Matrix II(Medium)

    1. 原题链接 https://leetcode.com/problems/spiral-matrix-ii/description/ 2. 题目要求 给定一个正整数n,求出从1到n平方的螺旋矩阵.例 ...

  5. 59. Spiral Matrix II(中等,同54题)

    Given an integer \(n\), generate a square matrix filled with elements from 1 to \(n^2\) in spiral or ...

  6. 【一天一道LeetCode】#59. Spiral Matrix II

    一天一道LeetCode系列 (一)题目 Given an integer n, generate a square matrix filled with elements from 1 to n2 ...

  7. [LeetCode] 59. Spiral Matrix II 螺旋矩阵 II

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

  8. 【LeetCode】59. Spiral Matrix II 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 维护四个边界和运动方向 保存已经走过的位置 日期 题 ...

  9. 59. Spiral Matrix && Spiral Matrix II

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

随机推荐

  1. P2158/bzoj2190 [SDOI2008]仪仗队

    P2158 [SDOI2008]仪仗队 欧拉函数 计算下三角的点数再*2+1 观察斜率,自行体会 #include<iostream> #include<cstdio> #in ...

  2. windows tomcat web应用以及eclipse console乱码解决方法

    在windows下,如果vm文件名为UTF-8格式,则显示乱码(velocity写出的不乱码): 改回GBK,则不再乱码.

  3. JS引擎深入分析

    转载自阮一峰:http://javascript.ruanyifeng.com/bom/engine.html 目录 JavaScript代码嵌入网页的方法 script标签:代码嵌入网页 scrip ...

  4. noip2010 真题练习 2017.2.18

    第一题比较简单,用exist数组判断是否在循环队列中,就可实现线性算法. Code #include<iostream> #include<cstdio> #include&l ...

  5. 标准IO与文件IO 的区别【转】

    本文转载自:http://blog.sina.com.cn/s/blog_63f31f3401013jrn.html 先来了解下什么是标准IO以及文件IO. 标准IO:标准I/O是ANSI C建立的一 ...

  6. ubuntu下git clone 提速

    环境:ubuntu16.04 方法:通过socks5代理并且使用http链接 步骤: 1.设置全局使用socks5代理,并且使用http传输 git config --global http.prox ...

  7. JavaScript 各种验证收集

    filter或者forEach函数,可能是因为你的浏览器还不够新,暂时不支持新标准的函数,你可以使用如下方式自己定义: if (!Array.prototype.forEach) { Array.pr ...

  8. 【第十九章】 springboot + hystrix(1)

    hystrix是微服务中用于做熔断.降级的工具. 作用:防止因为一个服务的调用失败.调用延时导致多个请求的阻塞以及多个请求的调用失败. 1.pom.xml(引入hystrix-core包) 1 < ...

  9. Linux批量更改文件后缀-转载

    一.rename解决 1.  Ubuntu系统下 rename 's//.c//.h/'  ./* 把当前目录下的后缀名为.c的文件更改为.h的文件 2.  CentOS5.5系统下 rename . ...

  10. [tixml]保存,读取

    保存: //xml的实体 TiXmlElement* rootElement = new TiXmlElement("spark"); rootElement->SetAtt ...