题目:

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, 3 ],
[ 8, 9, 4 ],
[ 7, 6, 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)。

public class Solution {
public int[][] generateMatrix(int n) {
if(n <= 0)
return new int[0][0];
int[][] res = new int[n][n];
int count = 1;
int left = 0, right = n - 1, top = 0, bot = n - 1; while(count <= n * n) {
for(int i = left; i <= right; i++)
res[top][i] = count++;
top++; if(count <= n * n) {
for(int i = top; i <= bot; i++)
res[i][right] = count++;
right--;
} if(count <= n * n) {
for(int i = right; i >= left; i--)
res[bot][i] = count++;
bot--;
} if(count <= n* n) {
for(int i = bot; i >= top; i--)
res[i][left] = count++;
left++;
}
} return res;
}
}

二刷:

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

Java:

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

public class Solution {
public int[][] generateMatrix(int n) {
if (n <= 0) {
return new int[][] {};
}
int[][] res = new int[n][n];
int totalElements = n * n, count = 0; int left = 0, right = n -1, top = 0, bot = n - 1;
while (count < totalElements) {
for (int i = left; i <= right; i++) {
res[top][i] = count + 1;
count++;
}
top++;
if (count < totalElements) {
for (int i = top; i <= bot; i++) {
res[i][right] = count + 1;
count++;
}
right--;
}
if (count < totalElements) {
for (int i = right; i >= left; i--) {
res[bot][i] = count + 1;
count++;
}
bot--;
}
if (count < totalElements) {
for (int i = bot; i >= top; i--) {
res[i][left] = count + 1;
count++;
}
left++;
}
}
return res;
}
}

题外话:

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:

public class Solution {
public int[][] generateMatrix(int n) {
if (n <= 0) return new int[][] {};
int[][] matrix = new int[n][n];
int left = 0, right = n - 1, top = 0, bot = n - 1;
int count = 1, totalElements = n * n;
while (count <= totalElements) {
for (int i = left; i <= right; i++) matrix[top][i] = count++;
top++;
if (count <= totalElements) {
for (int i = top; i <= bot; i++) matrix[i][right] = count++;
right--;
}
if (count <= totalElements) {
for (int i = right; i >= left; i--) matrix[bot][i] = count++;
bot--;
}
if (count <= totalElements) {
for (int i = bot; i >= top; i--) matrix[i][left] = count++;
left++;
}
}
return matrix;
}
}

相关题目:

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. CentOS 6.4 安装搭建 Scrapy 0.22 环境

    一.安装Python2.7.6 更新CentOS lib库文件 yum -y update 安装开发工具包 yum groupinstall -y development 安装扩展包 yum inst ...

  2. jquery-easyui中datagrid扩展,隐藏显示表头功能

    今天,后台中需要新增一个功能,用户可以自由选择显示的列,之后保存到本地localStroage中.所以扩展了easyui中datagrid的onHeaderContextMenu方法. 使用方法: _ ...

  3. cadence PCB绘制步骤

    1 创建一个PCB文件  file -> new 2 创建一个板框  add -> line ,在 options 选型中选择好,板框为 长 4400mil 宽 3200 3 给PCB板框 ...

  4. Python之回调魔法

    Python中魔法(前后又下划线)会在对象的生命周期被回调. 借助这种回调, 可以实现AOP或者拦截器的思想. 在Python语言中提供了类似于C++的运算符重在功能:一下为Python运算符重在调用 ...

  5. ASP.NET MVC +EasyUI 权限设计(三)基础模块

    请注明转载地址:http://www.cnblogs.com/arhat 在上一章中呢,我们基本上搭建好了环境,那么本章我们就从基础模块开始写起.由于用户,角色,动作三个当中,都是依赖与动作的,所以本 ...

  6. Asp.Net生命周期系列二

    在上回书开始的时候我们提到博客园的IIS看了一眼我的请求后就直接交给ASP.NET去处理了,并且要求ASP.NET处理完之后返回HTML以供展示. 那么我们不仅要问: 1,    IIS肯定是没有眼睛 ...

  7. 如何自定义Liferay 7 portal的Log in登录界面

    前提: 1. Liferay portal 7 2. Liferay IDE 3.0.1 Liferay现有的工具中提供了很多修改portal的模板,以满足开发者的各种自定义需求. 修改的原理是利用M ...

  8. Unity Editor not displaying Android textures properly

    最近入门学习shader,语法倒没什么,有一个奇怪的问题,如果把编译平台从pc转换为android模式的话,如果你的shader 带 Normal Mapping 的 话,效果和android上的真机 ...

  9. PHP 性能分析与实验(二)——PHP 性能的微观分析

    [编者按]此前,阅读过了很多关于 PHP 性能分析的文章,不过写的都是一条一条的规则,而且,这些规则并没有上下文,也没有明确的实验来体现出这些规则的优势,同时讨论的也侧重于一些语法要点.本文就改变 P ...

  10. UNDERSTANDING CALLBACK FUNCTIONS IN JAVASCRIPT

    转自: http://recurial.com/programming/understanding-callback-functions-in-javascript/ Callback functio ...