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. 思路: 题意:求小于给定非 ...
随机推荐
- [Usaco2005 Nov]Asteroids
Description Bessie wants to navigate her spaceship through a dangerous asteroid field in the shape o ...
- Hdu 5348 MZL's endless loop (dfs)
题目链接: Hdu 5348 MZL's endless loop 题目描述: 给出一个无向图(有环,有重边),包含n个顶点,m条边,问能否给m条边指定方向,使每个顶点都满足abs(出度-入度)< ...
- QT5每日一学(二)编写QT多窗口程序
一.添加主窗口 1.首先打开Qt Creator,新建Qt Widgets Application,项目名称设置为windows,在类信息界面保持基类为QMainWindow.类名为MainWindo ...
- windows 迁移数据库
1) Prerequisites ---------------- - The copy of the datafiles must be done with the database clos ...
- Windowsforms 中对话框,流、文件操作
对话框: 1.颜色选择控件——ColorDialog //显示颜色选择器 colorDialog1.ShowDialog(); //把取到的颜色赋值给panel panel1.BackColor = ...
- 工作记录 angular页面操作 MD5加密
今天只是做页面,基于angularjs,有美工做的图打底,确实好用 密码保存,用到了C# MD5加密: https://www.cnblogs.com/healer007/p/5062189.html
- AJPFX关于异常和file类的总结
/** * 各位坛友注意啦!对我这个帖子有任何的疑惑的,可以尽管留帖提问,我会在看到的第一时间回贴,既然写得出这帖子,* 就要对看这帖子的人负责,所以有问题,尽管问!* * * 这块没学好的同学,可以 ...
- poj3368 Frequent values
思路: 转化为RMQ. 实现: #include <cstdio> #include <cstring> #include <algorithm> using na ...
- ibatis 的sqlMap 的xml 关注点
1.当有特殊字符时候需要保持原状 eg:特殊字符 <> 错误:t.name<>' ' 会报The content of elements must consist of ...
- Array和ArrayList之间的区别
. Array类型的变量在声明的同时必须进行实例化(至少得初始化数组的大小),而ArrayList可以只是先声明. . Array只能存储同构的对象,而ArrayList可以存储异构的对象. 同构的对 ...