LeetCode_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 ]
]
class Solution {
public:
vector<vector<int> > generateMatrix(int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector<vector<int>> res(n,vector<int>(n));
int lays = (n+)/;
bool single = n%;
int num = ;
for(int lay = ; lay < lays; lay ++){
int topRow = lay;
int rightColumn = n - - lay;
//process the top
for(int i = lay; i <= rightColumn; i++)
res[topRow][i] = num++;
//process the right Column
for(int i = lay+; i <= rightColumn ;i++)
res[i][rightColumn] = num++;
if(lay == lays - && single )
continue;
//process the bottom
for(int i = rightColumn - ; i>= lay; i--)
res[rightColumn][i] = num++;
for(int i = rightColumn - ; i> lay ;i--)
res[i][lay] = num++;
}
return res;
}
};
LeetCode_Spiral Matrix II的更多相关文章
- 【leetcode】Spiral Matrix II
Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 to n2 in s ...
- 59. Spiral Matrix && Spiral Matrix II
Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matri ...
- leetcode-Spiral Matrix II 螺旋矩阵2之python大法好,四行就搞定,你敢信?
Spiral Matrix II 螺旋矩阵 Given an integer n, generate a square matrix filled with elements from 1 to n2 ...
- Search a 2D Matrix | & II
Search a 2D Matrix II Write an efficient algorithm that searches for a value in an m x n matrix, ret ...
- Spiral Matrix II
Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 to n2 in s ...
- LintCode 38. Search a 2D Matrix II
Write an efficient algorithm that searches for a value in an m x n matrix, return the occurrence of ...
- leetcode 54. Spiral Matrix 、59. Spiral Matrix II
54题是把二维数组安卓螺旋的顺序进行打印,59题是把1到n平方的数字按照螺旋的顺序进行放置 54. Spiral Matrix start表示的是每次一圈的开始,每次开始其实就是从(0,0).(1,1 ...
- leetcode 74. Search a 2D Matrix 、240. Search a 2D Matrix II
74. Search a 2D Matrix 整个二维数组是有序排列的,可以把这个想象成一个有序的一维数组,然后用二分找中间值就好了. 这个时候需要将全部的长度转换为相应的坐标,/col获得x坐标,% ...
- 【LeetCode】240. Search a 2D Matrix II
Search a 2D Matrix II Write an efficient algorithm that searches for a value in an m x n matrix. Thi ...
随机推荐
- 智能卡安全机制比较系列(二)DS SmartCard
DS Smart Card是飞利浦公司自己开发的一款CPU卡产品,在早期芯片厂商开发自己的COS并进行推广很普遍,现在像英飞凌(前西门子半导体)以及恩智普(前飞利浦半导体)几乎很少推广自己的COS,大 ...
- js实现编码,解码
<p><script type="text/javascript">// <![CDATA[var decToHex = function(str) ...
- 【转】解决java.lang.IllegalStateException: The content of the adapter has changed but ListView...的问题
原文网址:http://blog.csdn.net/ueryueryuery/article/details/20607845 我写了一个Dialog,Dialog中有一个ListView,想要点Li ...
- java开发经验分享(四)
四. 关于测试 1. 在整个项目计划中,测试时间安排的合理性,对测试阶段的情况应作充分预计,不可为了赶发布点而忽略质量. 2. 务必清楚产品包.更新包.bug包的提交规范.具体请参照<开发规范手 ...
- NET分布式缓存Memcached测试体验
原文地址:http://onlyonewt.blog.sohu.com/160168896.html 一直在学习关注大访问量网站的缓存是如何实现,之前看过Memcached的资料,忙于没有时间来真正测 ...
- zoj3640:概率(期望)dp
题目大意:有一个吸血鬼,初始攻击力为f,每天随机走到n个洞里面,每个洞有一个c[i],如果他的攻击力f>c[i] 则可以花费t[i] 的时间逃走,否则则花费一天时间使自己的攻击力增加c[i],求 ...
- hdu2769:枚举+同余方程
题意:有一个随机数生成器 x[i+1]=(a*x[i]+b)%10001 已知 x1,x3,x5...求 x2,x4,x6...... x的个数为 2n (n<=10000) a,b也在 0 ...
- c语言指针点滴1
#include <stdio.h> #include <stdlib.h> void main() { int *p = NULL;//指针开始最好都初始化为空 if(p = ...
- JavaScript获取元素样式
原生的JavaScript获取写在标签内部的样式很简单: <div class="test" id="test" style="width:10 ...
- 针对淡入淡出的定时轮播效果js
如果不使用jquery的fadeIn和fadeOut的接口和不适用animate情况下,如果要做用js实现淡入淡出轮播效果,我所想到的办法就是使用css3新特性transition(注意好兼容性). ...