题目:

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

链接: http://leetcode.com/problems/spiral-matrix-ii/

题解:

和spiral matrix I基本一样,这回事生成一个spiral matrix。最近的问题是,题目不难,但要提高做题速度和准确度,这样才有时间能学习别的知识。

要学习和想学习的东西很多很多,像多线程,软件测试,QA, Python,Javascript,Design Patterns, OO Design, System Design, Operating Systems, Distributed Systems,Data Mining, Machine Learning以及真题。继续努力吧,刷题只是很小的一部分,不过连题也刷不顺的话,是没机会进好公司的。

还要好好学习学习时间管理,时间真的不够用。

Time Complexity - O(n * n), Space Complexity - O(1)。

  1. public class Solution {
  2. public int[][] generateMatrix(int n) {
  3. if(n <= 0)
  4. return new int[0][0];
  5. int[][] res = new int[n][n];
  6. int count = 1;
  7. int left = 0, right = n - 1, top = 0, bot = n - 1;
  8.  
  9. while(count <= n * n) {
  10. for(int i = left; i <= right; i++)
  11. res[top][i] = count++;
  12. top++;
  13.  
  14. if(count <= n * n) {
  15. for(int i = top; i <= bot; i++)
  16. res[i][right] = count++;
  17. right--;
  18. }
  19.  
  20. if(count <= n * n) {
  21. for(int i = right; i >= left; i--)
  22. res[bot][i] = count++;
  23. bot--;
  24. }
  25.  
  26. if(count <= n* n) {
  27. for(int i = bot; i >= top; i--)
  28. res[i][left] = count++;
  29. left++;
  30. }
  31. }
  32.  
  33. return res;
  34. }
  35. }

二刷:

跟一刷一样,也跟Spiral Matrix I一样,设置四个边界点然后一直转圈赋值就可以了。

Java:

Time Complexity - O(n * n), Space Complexity - O(1)

  1. public class Solution {
  2. public int[][] generateMatrix(int n) {
  3. if (n <= 0) {
  4. return new int[][] {};
  5. }
  6. int[][] res = new int[n][n];
  7. int totalElements = n * n, count = 0;
  8.  
  9. int left = 0, right = n -1, top = 0, bot = n - 1;
  10. while (count < totalElements) {
  11. for (int i = left; i <= right; i++) {
  12. res[top][i] = count + 1;
  13. count++;
  14. }
  15. top++;
  16. if (count < totalElements) {
  17. for (int i = top; i <= bot; i++) {
  18. res[i][right] = count + 1;
  19. count++;
  20. }
  21. right--;
  22. }
  23. if (count < totalElements) {
  24. for (int i = right; i >= left; i--) {
  25. res[bot][i] = count + 1;
  26. count++;
  27. }
  28. bot--;
  29. }
  30. if (count < totalElements) {
  31. for (int i = bot; i >= top; i--) {
  32. res[i][left] = count + 1;
  33. count++;
  34. }
  35. left++;
  36. }
  37. }
  38. return res;
  39. }
  40. }

题外话:

1/30/2016

今天蘑菇回国,很想念她,希望咳嗽早点好,然后多吃多玩,好好放松休息吧。

这两天群里的小伙伴们讨论得很有干劲,但有不少朋友可能是三分钟热度,希望能持之以恒,一起加油。另外,看到地理说刷了5遍还没找到工作以及刷了4遍还没找到实习的...压力山大啊...

发现了一本好书<Big Data: Principles and best practices of scalable realtime data systems>。 有机会要好好读一读。自己系统设计,包括一般设计方面的技术,思路等等都比较差。这就是以前几年沉溺在温床中,不思进取混日子的代价。要多思考多练习,多参加一些tech talk,不要总做井底之蛙。

三刷:

跟前面一样。就是先确定好n x n矩阵,以及总元素 totalElements = n * n。 设置一个count = 1,在count <= totalElements的情况下进行转圈赋值。

Java:

  1. public class Solution {
  2. public int[][] generateMatrix(int n) {
  3. if (n <= 0) return new int[][] {};
  4. int[][] matrix = new int[n][n];
  5. int left = 0, right = n - 1, top = 0, bot = n - 1;
  6. int count = 1, totalElements = n * n;
  7. while (count <= totalElements) {
  8. for (int i = left; i <= right; i++) matrix[top][i] = count++;
  9. top++;
  10. if (count <= totalElements) {
  11. for (int i = top; i <= bot; i++) matrix[i][right] = count++;
  12. right--;
  13. }
  14. if (count <= totalElements) {
  15. for (int i = right; i >= left; i--) matrix[bot][i] = count++;
  16. bot--;
  17. }
  18. if (count <= totalElements) {
  19. for (int i = bot; i >= top; i--) matrix[i][left] = count++;
  20. left++;
  21. }
  22. }
  23. return matrix;
  24. }
  25. }

相关题目:

http://www.cnblogs.com/yrbbest/p/5165084.html

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

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

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

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

  3. 【leetcode】59.Spiral Matrix II

    Leetcode59 Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 ...

  4. Leetcode#59 Spiral Matrix II

    原题地址 相比于Spiral Matrix(参见这篇文章)要简单一些,因为是方阵,所以代码简洁一些. 注意当n是奇数的时候,中心小块要单独赋值(代码21行) 代码: vector<vector& ...

  5. LeetCode OJ 59. Spiral Matrix II

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

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

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

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

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

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

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

  9. [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 ...

随机推荐

  1. tomcat6.0 数据库连接池配置问题

    tomcat6.0 数据库连接池配置问题: 连接池配好后,启动tomat后,输入项目系统的登录名和密码,报 Cannot create JDBC driver of class '' for conn ...

  2. C#学习笔记一

    c#学习笔记一 c#学习笔记一    1 1.    注释    3 1.1.    ///是文档注释,用于类和方法的说明    3 1.2.    #region #endregion可以折叠代码  ...

  3. MinGW-64 安装

    一.在mingw-w64官网下载mingw-w64在线安装包 二.点击mingw-w64进行安装,选择: Version:选最新版本 我这个是4.9.2 Architecture:x86_64 (64 ...

  4. gif修改背景透明

    1.用ImageReady打开,将选中所有帧,右键选择“恢复为背景”. 2.打开“颜色板”,点击左下角的惊叹号,用吸色器点击背景,颜色板自动选中了背景色,将其映射为透明. 3.文件->将优化结果 ...

  5. Aspose 导出excel小demo

    //转为pdf         private void CelltoPDF(string cellPath, string pdfPath)         {             Workbo ...

  6. 对frameset、frame、iframe的js操作

    框架编程概述一个HTML页面可以有一个或多个子框架,这些子框架以<iframe>来标记,用来显示一个独立的HTML页面.这里所讲的框架编程包括框架的自我控制以及框架之间的互相访问,例如从一 ...

  7. JS读取UserAgent信息并做判断

    JS读取UserAgent信息并做判断 userAgent信息可以由navigator.userAgent拿到.例子: <script type="text/javascript&qu ...

  8. mysql 的物理结构

    mysql 的物理结构 跟着小辉老师学来的mysql知识,由于本人记性不好,但又觉得它很重要故把它记了下来,方便自己以后回忆,也希望能对大家有所帮助. 以下内容来自 小辉 老师的mysql教程,和部分 ...

  9. error X3025:global variables are implicitly constant, enable compatibility mode to allow modification

    global variables are implicitly constant, enable compatibility mode to allow modification http://xbo ...

  10. 解决ubuntu中zip解压的中文乱码问题

    转自解决ubuntu中zip解压的中文乱码问题 在我的ubuntu12.10中,发现显示中文基本都是正常的,只有在解压windows传过来的zip文件时,才会出现乱码.所以,我用另一个方法解决中文乱码 ...