题目

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 ]

]

分析

与54题Spiral Matrix相似题,为一个二维矩阵进行螺旋状赋值

AC代码

class Solution {
public:
vector<vector<int>> generateMatrix(int n) {
vector<vector<int> > ret(n,vector<int>(n , 0));
if (n <= 0)
return ret; int index = 1 , row = n-1 , col = n-1;
for (int x = 0, y = 0; x <= row && y <= col; x++, y++)
{
//为矩阵首行赋值
for (int j = y; j <= col; ++j , index++)
ret[x][j] = index; //为矩阵最右列赋值
for (int i = x + 1; i <= row; ++i,index++)
ret[i][col] = index; //为矩阵最底行赋值
for (int j = col - 1; j >= y && x != row; --j, index++)
ret[row][j] = index; //为矩阵最左列赋值
for (int i = row - 1; i > x && y != col; --i, index++)
ret[i][y] = index; //为内旋子矩阵赋值
row--;
col--; }//for
return ret;
}
};

GitHub测试程序源码

LeetCode(59)SPiral Matrix II的更多相关文章

  1. LeetCode(54)Spiral Matrix

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

  2. LeetCode(113) Path Sum II

    题目 Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given ...

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

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

  4. LeetCode(90):子集 II

    Medium! 题目描述: 给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入: [1,2,2] 输出: [ [2], [1 ...

  5. LeetCode(219) Contains Duplicate II

    题目 Given an array of integers and an integer k, find out whether there are two distinct indices i an ...

  6. LeetCode(137) Single Number II

    题目 Given an array of integers, every element appears three times except for one. Find that single on ...

  7. LeetCode(73)Set Matrix Zeroes

    题目 Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. cli ...

  8. LeetCode (45) Jump Game II

    题目 Given an array of non-negative integers, you are initially positioned at the first index of the a ...

  9. Leetcode(59)-Count Primes

    题目: Description: Count the number of prime numbers less than a non-negative number, n. 思路: 题意:求小于给定非 ...

随机推荐

  1. 三分钟掌握docker基本指令

    一下内容是我用markdown编辑的,所以排版不是太ok~ # Docker ## 1.核心概念 docker主机(Host):安装了docker程序的机器.docker直接安装再操作系统上. doc ...

  2. Akka源码分析-CircuitBreaker(熔断器)

    熔断器,在很多技术栈中都会出现的一种技术.它是在分布式系统中提供一个稳定的阻止嵌套失败的机制. 该怎么理解呢?简单来说,在分布式环境中,如果某个计算节点出现问题,很容易出现失败的逆向传到或整个系统的雪 ...

  3. Luogu P1160队列安排【链表/老文搬家】By cellur925

    原文发表于2018-04-15 08:15:09,我的luogu博客qwq. 看到题以后,要求维护一个可在任意位置修改添加删除元素的序列,那么显然我们可以用到链表. 然而本蒟蒻不久前刚刚学会链表.链表 ...

  4. linux 磁盘 分区、格式化、挂载

    将容量结果易读的容量格式显示出来df -h 分区 初次接触仅分成两个分区(“/与Swap”)预留一个备用的剩余磁盘容量 磁盘分区 fdisk #df /找出磁盘文件名#fdisk /dev/hdc#m ...

  5. 《Windows核心编程系列》十谈谈同步设备IO与异步设备IO之异步IO

    同步设备IO与异步设备IO之异步IO介绍 设备IO与cpu速度甚至是内存访问相比较都是比较慢的,而且更不可预测.虽然如此,通过使用异步设备IO我们仍然能够创造出更高效的程序. 同步IO时,发出IO请求 ...

  6. 目标Python2+3

    很多时候你可能希望你开发的程序能够同时兼容Python2+和Python3+. 试想你有一个非常出名的Python模块被很多开发者使用着,但并不是所有人都只使用Python2或者Python3.这时候 ...

  7. Bryce1010的微机接口课设

    8086CPU知识回顾 8086 CPU 中寄存器总共为 14 个,且均为 16 位 . 即 AX,BX,CX,DX,SP,BP,SI,DI,IP,FLAG,CS,DS,SS,ES 共 14 个. 而 ...

  8. List与类之间的运用,即与javabean的应用

    package com.wh.Object; public class Goods { private String name; private double price; private int n ...

  9. iOS判断输入的字符串是否是纯数字

    主要用于判断输入到TextField的内容是不是数字,比如需要输入电话号码的时候. 网上查看了一些资料,一般都是通过协议. 以下内容来自:http://www.2cto.com/kf/201404/2 ...

  10. Android系统的启动流程

    手机启动后首先会通过执行BootLoader来启动Linux内核,BootLoader是所有嵌入式设备开机启动执行的第一行代码,linux内核在启动过程中会加载各种设备的驱动同时初始化数据结构,并且开 ...