LeetCode(59)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 ]
]
分析
与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;
}
};
LeetCode(59)SPiral Matrix II的更多相关文章
- LeetCode(54)Spiral Matrix
题目 Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral ...
- 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 ...
- LeetCode(59):螺旋矩阵 II
Medium! 题目描述: 给定一个正整数 n,生成一个包含 1 到 n2 所有元素,且元素按顺时针顺序螺旋排列的正方形矩阵. 示例: 输入: 3 输出: [ [ 1, 2, 3 ], [ 8, 9, ...
- LeetCode(90):子集 II
Medium! 题目描述: 给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入: [1,2,2] 输出: [ [2], [1 ...
- LeetCode(219) Contains Duplicate II
题目 Given an array of integers and an integer k, find out whether there are two distinct indices i an ...
- LeetCode(137) Single Number II
题目 Given an array of integers, every element appears three times except for one. Find that single on ...
- 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 ...
- LeetCode (45) Jump Game II
题目 Given an array of non-negative integers, you are initially positioned at the first index of the a ...
- Leetcode(59)-Count Primes
题目: Description: Count the number of prime numbers less than a non-negative number, n. 思路: 题意:求小于给定非 ...
随机推荐
- 如何管理第三方接口token过期时间
背景: 随着微服务的盛行,做开发时不可避免的要涉及第三方接口,安全起见,这些接口都会需要一个token参数.而token一般都会有一个过期时间,比如2小时或者30分钟.那么如何在自己的应用中存储并管理 ...
- 《Windows核心编程系列》十二谈谈Windows内存体系结构
Windows内存体系结构 理解Windows内存体系结构是每一个励志成为优秀的Windows程序员所必须的. 进程虚拟地址空间 每个进程都有自己的虚拟地址空间.对于32位操作系统来说,它的地址空间是 ...
- 《windows核心编程系列》七谈谈用户模式下的线程同步
用户模式下的线程同步 系统中的线程必须访问系统资源,如堆.串口.文件.窗口以及其他资源.如果一个线程独占了对某个资源的访问,其他线程就无法完成工作.我们也必须限制线程在任何时刻都能访问任何资源.比如在 ...
- 进击的Python【第九章】:paramiko模块、线程与进程、各种线程锁、queue队列、生产者消费者模型
一.paramiko模块 他是什么东西? paramiko模块是用python语言写的一个模块,遵循SSH2协议,支持以加密和认证的方式,进行远程服务器的连接. 先来个实例: import param ...
- php微信自动发红包
<?phpheader('Content-type:text');define("TOKEN", "weixin");$wechatObj = new w ...
- js ajax 数组类型参数传递
若一个请求中包含多个值,如:(test.action?tid=1&tid=2&tid=3),参数都是同一个,只是指定多个值,这样请求时后台会发生解析错误,应先使用 tradititon ...
- 原创 SqlParameter 事务 批量数据插入
不错,很好,以后防注入批量事务提交虽然麻烦点研究了几个小时,但不会是问题了 SqlCommand cmd; HelpSqlServer helps = new HelpSqlServer(); //定 ...
- 解决::processDebugResourcesERROR: In<declare-styleable> FontFamilyFont编译报错
cordova编译时报错 错误信息 :processDebugResourcesERROR: In <declare-styleable> FontFamilyFont, unable t ...
- css3中content属性的应用
可以使用css3中content功能为html元素增减内容.content需要配合 E:before和E:after使用. 废话少说,看代码和效果说明: 第一种: css代码: #div1:befor ...
- php(三)使用PDO链接数据库
1.启动 mysql数据库,打开图形化控制界面 2.新建一个数据库 3.创建一个数据表 4.给数据表添加数据 id是数字类型的 类型选择int长度 11 username 等其他数据 会是字符串形 ...