LeetCode: 59. Spiral Matrix II(Medium)
1. 原题链接
https://leetcode.com/problems/spiral-matrix-ii/description/
2. 题目要求
给定一个正整数n,求出从1到n平方的螺旋矩阵。例如n为3,构成的螺旋矩阵如下图所示

3. 解题思路
该题与54题Spiral Matrix的解题思路大致相同,同样是一个while循环内,确定螺旋矩阵一个圈上的所有元素。
采用一个计数器count,count初始为1,每确定一个位置上的元素,count加1,
4. 代码实现
public class SpiralMatrixII59 {
public static void main(String[] args) {
for(int []n:generateMatrix(3)){
for(int x:n)
System.out.print(x+" ");
System.out.println();
}
}
/**
* t t t t t
* l ... r
* l r
* l ... r
* b b b b r
*/
public static int[][] generateMatrix(int n) {
int[][] res = new int[n][n];
int left = 0, right = n - 1;
int top = 0, bottom = n - 1;
int count = 1;
while (left <=right) {
// 确定t所在边上的元素
for (int i = left; i <= right; i++) {
res[top][i] = count++;
}
top++;
// 确定r所在边上的元素
for (int i = top; i <= bottom; i++) {
res[i][right] = count++;
}
right--;
// 确定b所在边上的元素
for(int i= right;i>=left;i--){
res[bottom][i]=count++;
}
bottom--;
// 确定l所在边上的元素
for(int i =bottom;i>=top;i--){
res[i][left]=count++;
}
left++;
}
return res;
}
}
LeetCode: 59. Spiral Matrix II(Medium)的更多相关文章
- 【leetcode】Spiral Matrix II (middle)
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
- LeetCode 59. Spiral Matrix II (螺旋矩阵之二)
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
- [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 ...
- Leetcode#59 Spiral Matrix II
原题地址 相比于Spiral Matrix(参见这篇文章)要简单一些,因为是方阵,所以代码简洁一些. 注意当n是奇数的时候,中心小块要单独赋值(代码21行) 代码: vector<vector& ...
- LeetCode Spiral Matrix II (技巧)
题意: 从1开始产生连续的n2个数字,以螺旋的方式填满一个n*n的数组. 思路: 由于是填满一个矩阵,那么只需要每次都填一圈即可.应该注意特殊情况. 迭代: class Solution { publ ...
- [leetcode]59. Spiral Matrix II螺旋遍历矩阵2
Given a positive integer n, generate a square matrix filled with elements from 1 to n^2 in spiral or ...
- LeetCode: 63. Unique Paths II(Medium)
1. 原题链接 https://leetcode.com/problems/unique-paths-ii/description/
- 【leetcode】Single Number II (medium) ★ 自己没做出来....
Given an array of integers, every element appears three times except for one. Find that single one. ...
- 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 ...
随机推荐
- 理解Underscore的设计架构
在一个多月的毕业设计之后,我再次开始了Underscore的源码阅读学习,断断续续也写了好些篇文章了,基本把一些比较重要的或者个人认为有营养的函数都解读了一遍,所以现在学习一下Underscore的整 ...
- Codeforces Round #435 (Div. 2)【A、B、C、D】
//在我对着D题发呆的时候,柴神秒掉了D题并说:这个D感觉比C题简单呀!,,我:[哭.jpg](逃 Codeforces Round #435 (Div. 2) codeforces 862 A. M ...
- JAVA中日期 yyyy-MM-dd HH:mm:ss和yyyy-MM-dd hh:mm:ss的区别
JAVA中日期 yyyy-MM-dd HH:mm:ss和yyyy-MM-dd hh:mm:ss的区别 : HH:24小时制 hh:12小时制 package time; import java.tex ...
- Gradle入门实战(Windows版)
Installation Gradle runs on all major operating systems and requires only a Java JDK or JRE version ...
- 计算机网络概述4_性能指标之时延,时延带宽积,往返时间RTT,利用率
发送时延(传输时延):从发送分组的第一个比特算起,到该分组的最后一个比特发送完毕所需的时间. 总结:
- mac端抓包工具——Charles使用
一.简介 Charles(http://www.charlesproxy.com/)是在Mac 下常用的截取网络封包的工具.Charles 通过将自己设置成系统的网络访问代理服务器,使得所有的网络访问 ...
- Google Map中的瓦片
一.墨卡托投影google map使用的是EPSG:900913标准墨卡托投影(等角圆术地图投影)y = R*ln(tan(pi/4 + a/2))x = R*b当y等于piR时,投影图正好为一个正方 ...
- 二十五、详述 IntelliJ IDEA 提交代码前的 Code Analysis 机制
在我们用 IntelliJ IDEA 向 SVN 或者 Git 提交代码的时候,IntelliJ IDEA 提供了一个自动分析代码的功能,即Perform code analysis: 如上图所示,当 ...
- ARM 汇编指令集 特点5:ARM 多级指令流水线
1.为增加处理器指令流 的速度,ARM使用多级流水线. 就是举个例子: mov r1,#0 ,这条指令 分几个人做,一个人从存储器取指令,解码指令中用到的寄存器,寄存器运算. 这样三步 :如果一个人做 ...
- Gradle Goodness: Unpacking an Archive
To create an archive with Gradle is easy. We have several tasks like Zip, Tar, Jar, War and Ear to c ...