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. 使用jdk生成自签发证书(过程总结)

    前言: 最近在做华为NB-IoT接口开发,需要用到双向认证,就去学了一下. 然后我将过程总结了一下. 相关华为论坛链接:http://developer.huawei.com/ict/forum/th ...

  2. 虚拟机与Linux的初体验

    很早的时候就知道虚拟机这个神奇东西的存在,但也仅仅是只闻其名,未见其身.后来在信息安全素质教育的这门课程上,为了做木马实验.暴力破解实验以及邮件窃取实验,这才比较直接的接触到了虚拟机.当我看着在另一个 ...

  3. 让div跟着鼠标移动

    朋友求助帖 具体实现代码 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&q ...

  4. Yii2 Gridview 动态显示行或列和action列

    我们知道Yii中的GridView组件是非常好用的. 某些情况要动态显示某列,这时候就要用到visible属性 'propString' => ['attribute' => 'prope ...

  5. idea 新建 maven项目遇到的一些问题

    idea创建好了maven项目之后,需要先在项目中添加 Web,这里创建Web时就会要求fix一个Artifacts,新建即可,然后面板设置默认即可(shift+ctrl+alt+s 打开面板): 然 ...

  6. C# 调用C++ dll 返回char*调用方式(StringBuilder乱码)

    // CDLLDemo.cpp : 定义 DLL 应用程序的导出函数. // #include "stdafx.h" #include "string.h" # ...

  7. DB知识点记录

    DB知识点记录 分页 SqlServer:ROW_NUMBER () over (ORDER BY ID) AS RN, MySql:limit Oracle:ROWNUM AS RN 数据表的基本结 ...

  8. ----------BMI指数小程序----------

    # 1.创建并输出菜单, 菜单是不可变的. 所以使用元组# menus = ("1, 录入", "2, 查询", "3, 删除", &quo ...

  9. html表单总结

    总结了下html表单: <!DOCTYPE html> <html lang="en"> <head> <meta charset=&qu ...

  10. socket编程为什么需要htonl(), ntohl(), ntohs(),htons() 函数-------转载

    在C/C++写网络程序的时候,往往会遇到字节的网络顺序和主机顺序的问题.这是就可能用到htons(), ntohl(), ntohs(),htons()这4个函数. 网络字节顺序与本地字节顺序之间的转 ...