Spiral Matrix II 解答
Question
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 ]
]
Solution
Similar with Spiral Matrix.
public class Solution {
public int[][] generateMatrix(int n) {
int[][] result = new int[n][n];
int start = 1, x = 0, y = 0;
int m = n;
while (m > 0) {
if (m == 1) {
result[x][y] = start;
break;
}
for (int i = 0; i < m - 1; i++)
result[x][y++] = start++;
for (int i = 0; i < m - 1; i++)
result[x++][y] = start++;
for (int i = 0; i < m - 1; i++)
result[x][y--] = start++;
for (int i = 0; i < m - 1; i++)
result[x--][y] = start++;
x++;
y++;
m -= 2;
}
return result;
}
}
Spiral Matrix II 解答的更多相关文章
- 【leetcode】Spiral Matrix II
Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 to n2 in s ...
- 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 ...
- Spiral Matrix II
Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 to n2 in s ...
- leetcode 54. Spiral Matrix 、59. Spiral Matrix II
54题是把二维数组安卓螺旋的顺序进行打印,59题是把1到n平方的数字按照螺旋的顺序进行放置 54. Spiral Matrix start表示的是每次一圈的开始,每次开始其实就是从(0,0).(1,1 ...
- LeetCode: Spiral Matrix II 解题报告-三种方法解决旋转矩阵问题
Spiral Matrix IIGiven an integer n, generate a square matrix filled with elements from 1 to n2 in sp ...
- 【leetcode】59.Spiral Matrix II
Leetcode59 Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 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] Spiral Matrix II 螺旋矩阵之二
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
- 【leetcode】Spiral Matrix II (middle)
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
随机推荐
- 最简单的内核模块hello world
[root@rt2m09617.sqa.tbc /home/ahao.mah/main] #cat hello.c // Defining __KERNEL__ and MODULE allows u ...
- <s:iterator> 序号
<s:iterator />的序号,解决这个问题有两种办法. 方法一:通过set标签实现: <s:set name="a" value=1/> <s: ...
- JavaScript函数 bind call apply区别
1. apply calll 在JavaScript中 call 和 apply 都是为了改变某个函数运行时上下文而存在的, 换句话说就是为了改变函数内部的this的指向. 这里我们有一个新的对象 b ...
- ListVeiw新增记录及 滚动条移动到指定位置
C# 自带的ListView控件的滚动条移动到指定位置. lvwList为ListView控件 lvwList.EnsureVisible(lvwList.Items.Count - 1); 新增记录 ...
- 《第一行代码》学习笔记14-UI(3)
1. (1)所有控件都是直接或间接继承自View,所用的所有布局都是直接或间接继承自ViewGroup的. (2)View是Android中一种最基本的UI组件,可以在屏幕上绘制一块矩形区域,并能响应 ...
- Silverlight Visifire控件 .net后台控制aspx页面控件的显示与隐藏,动态给控件赋值,选定默认值的设定
.net后台代码: 控件的显示与隐藏: this.dateStart.Visibility = Visibility.Collapsed;//不显示控件 this.dateYear.Visibilit ...
- php的一些小笔记-文件函数(2)
---恢复内容开始--- copy 文件的复制 echo copy('test.php','test1.php'); 如果成功的返回true,反之返回false 如何在多层目录中复制文件呢?也就是根据 ...
- WP Super Cache 安装与设置方法
1.首先,永久连接不能使用默认格式 2.修改永久链接格式,中文推荐采用 /%post_id%.html (这下你知道我的.orz哪里来了吧) 如果你和我一样蛋疼愿意为每篇文章写一个英语的post sl ...
- 启动两个tomcat
因为项目的种种原因,必须启动两个tomcat测试 于是复制tomcat,改端口,报错,到日志看,发现shutdow端口也需要改 总结 server.xml改两个地方的端口 <Server por ...
- 函数reduce,lambda,filter
#比较时间差,判断执行有时. import time def panduan(x): if x%5==0 and x%7==0: return True else: return False star ...