一天一道LeetCode系列

(一)题目

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 ]

]

(二)解题

思路参考: 【一天一道LeetCode】#54. Spiral Matrix

还是一样按圈赋值,每一圈的起点分别为(0,0),(1,1)…..


class Solution {

public:

    vector<vector<int>> generateMatrix(int n) {

        vector<vector<int>> ret;

        if(n==0) return ret;

        for(int i = 0 ; i < n ; i++)

        {

            vector<int> tmp(n,0);

            ret.push_back(tmp);

        }

        int count = 0 ; 

        int px = 0 , py = 0;//初始值

        int start = 0;//每一圈的起点

        while(start<n/2)

        {

            int i;

            for(i = py ; i<n - start; i++) {//从左往右

                ret[px][i] = ++count;

            }

            py = i-1;

            for(i = px+1 ; i<n - start ; i++){//从上往下

                ret[i][py] = ++count;

            }

            px = i-1;

            for(i = py-1 ; i>=start ; i--){//从右往左

                ret[px][i] = ++count;

            }

            py = i+1;

            for(i = px-1 ; i>=start + 1 ; i--){//从下往上

                ret[i][py] = ++count;

            }

            start++;

            px = py = start;//下一圈

        }

        if(n%2==1) ret[px][py] =++count;//n为奇数的时候需要考虑中心值

        return ret;

    }

};

【一天一道LeetCode】#59. Spiral Matrix II的更多相关文章

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

  2. LeetCode: 59. Spiral Matrix II(Medium)

    1. 原题链接 https://leetcode.com/problems/spiral-matrix-ii/description/ 2. 题目要求 给定一个正整数n,求出从1到n平方的螺旋矩阵.例 ...

  3. Leetcode#59 Spiral Matrix II

    原题地址 相比于Spiral Matrix(参见这篇文章)要简单一些,因为是方阵,所以代码简洁一些. 注意当n是奇数的时候,中心小块要单独赋值(代码21行) 代码: vector<vector& ...

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

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

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

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

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

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

  8. 【leetcode】59.Spiral Matrix II

    Leetcode59 Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 ...

  9. 【leetcode】Spiral Matrix II

    Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 to n2 in s ...

  10. Java for LeetCode 059 Spiral Matrix II

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

随机推荐

  1. Node.js 集群

    稳定性: 2 - 不稳定 单个 Node 实例运行在一个线程中.为了更好的利用多核系统的能力,可以启动 Node 集群来处理负载. 在集群模块里很容易就能创建一个共享所有服务器接口的进程. var c ...

  2. 详解BLE 空中包格式—兼BLE Link layer协议解析

    BLE有几种空中包格式?常见的PDU命令有哪些?PDU和MTU的区别是什么?DLE又是什么?BLE怎么实现重传的?BLE ACK机制原理是什么?希望这篇文章能帮你回答以上问题. 虽然BLE空中包(pa ...

  3. 在linux系统中I/O 调度的选择

        I/O 调度算法再各个进程竞争磁盘I/O的时候担当了裁判的角色.他要求请求的次序和时机做最优化的处理,以求得尽可能最好的整体I/O性能. 在linux下面列出4种调度算法 CFQ (Compl ...

  4. 20160209.CCPP体系详解(0019天)

    程序片段(01):01.字符串.c 内容概要:字符串 #include <stdio.h> #include <stdlib.h> #include <locale.h& ...

  5. Android ocr识别文字介绍(文字识别)

    最近在做身份证号码识别,在网上搜索的一番后发现目前开源的OCR中tesseract-ocr算是比较强大的了,它由HP于1985年到1995年间开发,后来由google直接负责,经过谷歌进一步开发后,目 ...

  6. Android Studio 2.2 新功能详解

    Tamic /文 -译 http://blog.csdn.net/sk719887916/article/details/52672688 Android的Studio 2.2 已经可以在官网下载了. ...

  7. Android安全升级的7.0: Nougat

    Tamic http://www.jianshu.com/users/3bbb1ddf4fd5/latest_articles 今年夏天以来,Google做了多种增强的安全性在Android的7.0N ...

  8. Java异常处理-----java异常体系

    再三思考后还是决定贴图,csdn的格式,我是真玩不转,对不起了各位,继续将就吧. 错误原因:内存溢出.需要的内存已经超出了java虚拟机管理的内存范围. 错误原因:找不到类文件. 错误(Error): ...

  9. CCSpriteBatchNode中存放元素的一点理解

    该对象只能包含基于CCSprite的对象,并且该要求适用于一切子孙对象.即加入CCSpriteBatchNode的任何对象都必须是CCSprite或其子类. 比如CCSpriteBatchNode包含 ...

  10. Java进阶(四十五)java 字节流与字符流的区别

    java 字节流与字符流的区别(转载)   字节流与和字符流的使用非常相似,两者除了操作代码上的不同之外,是否还有其他的不同呢?   实际上字节流在操作时本身不会用到缓冲区(内存),是文件本身直接操作 ...