59. 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, 3 ],
[ 8, 9, 4 ],
[ 7, 6, 5 ]
]
题解:
和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的更多相关文章
- leetcode 54. Spiral Matrix 、59. Spiral Matrix II
54题是把二维数组安卓螺旋的顺序进行打印,59题是把1到n平方的数字按照螺旋的顺序进行放置 54. Spiral Matrix start表示的是每次一圈的开始,每次开始其实就是从(0,0).(1,1 ...
- 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 ...
- 【leetcode】59.Spiral Matrix II
Leetcode59 Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 ...
- Leetcode#59 Spiral Matrix II
原题地址 相比于Spiral Matrix(参见这篇文章)要简单一些,因为是方阵,所以代码简洁一些. 注意当n是奇数的时候,中心小块要单独赋值(代码21行) 代码: vector<vector& ...
- 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 ...
- 59. Spiral Matrix II(中等,同54题)
Given an integer \(n\), generate a square matrix filled with elements from 1 to \(n^2\) in spiral or ...
- 【一天一道LeetCode】#59. Spiral Matrix II
一天一道LeetCode系列 (一)题目 Given an integer n, generate a square matrix filled with elements from 1 to n2 ...
- LeetCode: 59. Spiral Matrix II(Medium)
1. 原题链接 https://leetcode.com/problems/spiral-matrix-ii/description/ 2. 题目要求 给定一个正整数n,求出从1到n平方的螺旋矩阵.例 ...
- [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 ...
随机推荐
- Configure Log Shipping
准备工作 两台装有的Windows Server 2012R2以及SQL Server 2012的服务器 下载评估版 Windows Server 2012 R2 下载 Microsoft SQL S ...
- [转]Oracle学习记录 九 Prc C学习
经过前面的了解,现在想用C语言来编程了,搜索了很多东西,后来决定先用Pro C来进行学习 在安装完Oracle数据库后就可以进行编程了,里面有一个命令proc就是对程序进行预编译的. 在这记一下,这是 ...
- java之其它命令
java编译命令 javac: javac -d <目录> 源文件.java 指定存放生成的class文件的路径命令行下编译带包名的java源文件: javac -d . XX.java ...
- java实现合并两个已经排序的列表
相对于C++来说,Java的最大特点之一就是没有令人困惑的指针,但是我们不可否认,在某些特定的情境下,指针确实算的上一把利刃.虽然Java中没有明确定义出指针,但是由于类的思想,我们可以使用class ...
- Unable to create the store directory. (Exception from HRESULT: 0x80131468)
一个ASP.NET的程序,使用了MS ReportViewer报告控件,在用该控件导出生成Excel文件时,先是提示行不能超过65535. 这个是由于Excel2003的行限制的原因.由于修改成用Ex ...
- Andriod WIFI驱动模块
一:什么是WIFI WIFI是一种无线连接技术,可用于手机.电脑.PDA等终端.WIFI技术产生的目的是改善基于IEEE802.11标准的无线网络产品之间的互通性,也就是说WIFI是基于802.11标 ...
- left join 过滤条件写在on后面和写在where 后面的区别
create table t1(id int, feild int);insert into t1 values(1 , 1);insert into t1 values(1 , 2);insert ...
- JSTL标签总结
一.JSTL简介: 1.JSP标准标签库JSTL(JSP Standard Tag Library)是一个JSP标签集合,它封装了JSP应用的通用核心功能. 2.JSTL支持通用的.结构化的任务.比如 ...
- SVN的405错误
错误1: 如果你没有阅读以下文字,活该你倒霉(这段文字是在googlecode添加新项目时生成的): Command-line access If you plan to make changes, ...
- Android ADB 端口占用问题解决方案
问题描述: The connection to adb is down, and a severe error has occured. You must restart adb and Eclips ...