LeetCode:螺旋矩阵||【59】

题目描述

给定一个正整数 n,生成一个包含 1 到 n2 所有元素,且元素按顺时针顺序螺旋排列的正方形矩阵。

示例:

输入: 3
输出:
[
[ 1, 2, 3 ],
[ 8, 9, 4 ],
[ 7, 6, 5 ]
]

题目分析

  这道题难度也是中等,是那道恶心题的衍生版,但是说实话难度小于1,且已经明示一定是正方形矩。我们直接改一下1的代码就可以了!并且效率是非常高的!

  

  这道题简直也丧心病狂☺!我们采用的方式是一圈一圈赋值

  答案将是从第一个外层按顺时针顺序填写所有元素,然后是第二个外层的元素,依此类推

  我们首先定义四个元素,r1,r2,c1,c2,这将框定一个范围,我们顺时针赋值这个范围边上的值,每次赋值以后再次缩小框

  好的问题来了?

  1.要赋值几个框?

    times=Math.min(长,宽)%2==0?Math.min(长,宽)/2:Math.min(长,宽)/2+1;

  2.顺时针赋值的横纵坐标变化规律?如图所示有颜色是要赋值的框

  

Java题解

public class SpiralMatrixII_59 {
public int[][] generateMatrix(int n) {
int[][] arr = new int[n][n];
int c1 = 0;
int c2 = n-1;
int r1 = 0;
int r2 = n-1;
int count =1 ;
int times = Math.min(n,n)%2==0?Math.min(n,n)/2:Math.min(n,n)/2+1;
for(int i=0;i<times;i++)
{
for (int c = c1; c <= c2; c++) arr[r1][c]=count++;
for (int r = r1 + 1; r <= r2; r++) arr[r][c2]=count++;
if (r1 < r2 && c1 < c2) {
for (int c = c2 - 1; c > c1; c--) arr[r2][c]=count++;
for (int r = r2; r > r1; r--) arr[r][c1]=count++;
}
r1++;
r2--;
c1++;
c2--;
}
return arr;
}
}

  

LeetCode:螺旋矩阵||【59】的更多相关文章

  1. LeetCode 59. Spiral Matrix II (螺旋矩阵之二)

    Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...

  2. [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 ...

  3. Java实现 LeetCode 59 螺旋矩阵 II

    59. 螺旋矩阵 II 给定一个正整数 n,生成一个包含 1 到 n2 所有元素,且元素按顺时针顺序螺旋排列的正方形矩阵. 示例: 输入: 3 输出: [ [ 1, 2, 3 ], [ 8, 9, 4 ...

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

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

  5. leetcode 54. 螺旋矩阵 及 59. 螺旋矩阵 II

    54. 螺旋矩阵 问题描述 给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素. 示例 1: 输入: [ [ 1, 2, 3 ], [ 4, 5, ...

  6. LeetCode(59):螺旋矩阵 II

    Medium! 题目描述: 给定一个正整数 n,生成一个包含 1 到 n2 所有元素,且元素按顺时针顺序螺旋排列的正方形矩阵. 示例: 输入: 3 输出: [ [ 1, 2, 3 ], [ 8, 9, ...

  7. LeetCode 59. 螺旋矩阵 II(Spiral Matrix II)

    题目描述 给定一个正整数 n,生成一个包含 1 到 n2 所有元素,且元素按顺时针顺序螺旋排列的正方形矩阵. 示例: 输入: 3 输出: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7 ...

  8. leetcode刷题-59螺旋矩阵2

    题目 给定一个正整数 n,生成一个包含 1 到 n^2 所有元素,且元素按顺时针顺序螺旋排列的正方形矩阵. 思路 与螺旋矩阵题完全一致 实现 class Solution: def generateM ...

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

随机推荐

  1. gulpfile.js(编译sass,压缩图片,自动刷新浏览器)

    var gulp = require('gulp'),     sass = require('gulp-sass'),     watch = require('gulp-watch'),      ...

  2. UI-1-UI入门

    课程要点: 创建一个iOS工程 AppDelegate类 UIKit框架以及UIWindow 在window上添加第一个试图UIView NSTimer(定时器) 创建一个iOS工程 PS:接下来简单 ...

  3. phpexcel图形图表(一)入门

    PHPExcel - Excel的PHP处理引擎 PHPExcel 提供了一系列的 PHP语言 类,让你可以轻松地读写操作以下格式的文件:.xls/.xlsx/.csv/.ods/Gnumeric/P ...

  4. web开发中经常使用的js

    将自己在web开发中经经常使用到的一些JS总结一下. 1.改动标签和表单的值 改动标签的值: var customer = document.getElementById("custm&qu ...

  5. x264_param_t结构体解释,设置及对应函数位置

    typedef struct x264_param_t {   /* CPU 标志位 */   unsigned int cpu;   int i_threads; /* 并行编码多帧 */   in ...

  6. HDU3062-Party(2-SAT)

    pid=3062">题目链接 思路:2-SAT的模版题 代码: #include <iostream> #include <cstdio> #include & ...

  7. C2 CompilerThread0 如果抓到的java线程dump里占用CPU最高的线程是这个,99%可能是因为服务重启了

    "C2 CompilerThread0" #5 daemon prio=9 os_prio=0 tid=0x00007f20c80b3800 nid=0x57c0 runnable ...

  8. linux终端常用命令

    常用的信息显示命令 命令#pwd 用于在屏幕上输出当前的工作目录. 命令#stat 用于显示指定文件的相关信息. 命令#uname -a 用于显示操作系统信息. 命令#hostname 用于显示当前本 ...

  9. 第五课 nodejs 路由实现并处理请求作出响应

    1创建一个http Server 文件server.js var http = require('http');var url = require('url');function start(rout ...

  10. Java格式化日期的三种方式

    1)借助DateFormat类: public String toString(Date d) { SimpleDateFormat sdf = new SimpleDateFormat(“yyyy- ...