Leetcode59 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 ]
]
回型打印整数n。

Tips:
dr dc控制指针的走向,当不满足
(newRow>=0 && newRow<n && newCol>=0 && newCol<n && !visit[newRow][newCol])这些条件时,指针就要改变方向。
package medium;

public class L59SpiralMatrixII {

    public int[][] generateMatrix(int n) {
int[][] ans = new int[n][n];
boolean[][] visit = new boolean[n][n];
int row = 0;
int col = 0;
int ind = 0;
int[] dr = { 0, 1, 0, -1 };
int[] dc = { 1, 0, -1, 0 };
for (int i = 0; i < n * n; i++) {
ans[row][col] = i + 1;
visit[row][col] = true;
int newRow=row+dr[ind];
int newCol=col+dc[ind];
if(newRow>=0 && newRow<n && newCol>=0 && newCol<n && !visit[newRow][newCol]){
row=newRow;
col=newCol;
}else{
ind=(ind+1)%4;
row+=dr[ind];
col+=dc[ind];
}
}
return ans;
}
public void print(int[][] ans,int n){
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
System.out.print(ans[i][j]+" ");
}
System.out.println();
}
}
public static void main(String[] args) {
L59SpiralMatrixII cc= new L59SpiralMatrixII();
int n=4;
int[][] ans= cc.generateMatrix(n);
cc.print(ans, n);
}
}

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

  1. 【LeetCode】59. Spiral Matrix II 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 维护四个边界和运动方向 保存已经走过的位置 日期 题 ...

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

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

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

  4. 【LeetCode】59.螺旋矩阵II

    59.螺旋矩阵II 知识点:数组: 题目描述 给你一个正整数 n ,生成一个包含 1 到 n2 所有元素,且元素按顺时针顺序螺旋排列的 n x n 正方形矩阵 matrix . 示例 输入:n = 3 ...

  5. 【LeetCode】54. Spiral Matrix 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 维护四个边界和运动方向 保存已经走过的位置 日期 题 ...

  6. 【leetcode】54.Spiral Matrix

    Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...

  7. 【LeetCode】885. Spiral Matrix III 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

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

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

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

随机推荐

  1. kubernetes常用基础命令

    创建资源对象 创建名为nginx-deploy的控制器资源对象 [root@master ~]# kubectl run nginx-deploy --image=nginx:1.12 --repli ...

  2. python教程(六)·字符串

    我们已经学习了字符串的使用方法,我们还学习了使用索引和分片操作字符串,经历了这么长的时间,相信大家也有所掌握:本节将讨论并学习字符串的格式化与字符串的常用方法 字符串格式化 字符串是序列的一种,所以所 ...

  3. 郁金香指标开源库的使用--(tulipindicators-0.8.4)

    瞎逛发现最新出了这么一个指标库,有100多种指标的函数库,文档写的比较好,重要的是作者一直在维护. 把它编成库,然后测试一下,可用于自动交易,策略交易等开发. 1.下载地址 https://githu ...

  4. 工作和面试中的gdb

    gdb是C/C++程序员必备的专业技能,工作中gdb最常用的场景有两个,一个是分析core文件,另一个是调试程序. 分析core文件的方法如下: 1.gdb 程序名 core文件名 2.bt或wher ...

  5. 96. Partition List [easy]

    Description Given a linked list and a value x, partition it such that all nodes less than x come bef ...

  6. 20155310《Java程序设计》实验五(网络编程与安全)实验报告

    20155310<Java程序设计>实验五(网络编程与安全)实验报告 一.实验内容及步骤 •任务一: 编写MyBC.java实现中缀表达式转后缀表达式的功能 编写MyDC.java实现从上 ...

  7. 20155321 2016-2017-2 《Java程序设计》第三周学习总结

    20155321 2016-2017-2 <Java程序设计>第三周学习总结 教材学习内容总结 4.1 类与对象 定义类用class关键字,建立实例用new关键字 一个原始码中可有多个类定 ...

  8. 20155325 2016-2017-2 《Java程序设计》第2周学习总结

    教材学习内容总结 上节课讲了些思维方法:git,vim的使用技巧,推荐了picpick截图软件. 第三章书本上涵盖了基本语法内容 由于在语法方面java和c有相似之处,所以我重点关注不同之处和易忽略之 ...

  9. 20155338 2016-2017-2 《Java程序设计》第10周学习总结

    20155338 2016-2017-2 <Java程序设计>第10周学习总结 教材学习内容总结 网络编程 · 网络编程就是在两个或两个以上的设备(例如计算机)之间传输数据.程序员所作的事 ...

  10. sql语句-6-更新数据