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 ...
随机推荐
- Codeforces 437E The Child and Polygon
http://codeforces.com/problemset/problem/437/E 题意:求一个多边形划分成三角形的方案数 思路:区间dp,每次转移只从一个方向转移(L,R连线的某一侧),能 ...
- emacs vim IDE
原本想花点时间来学习下Vim或者emacs,结果在网上搜索到这篇文章 骂战挺多的,但是也长见识 http://bbs.csdn.net/topics/390306165 下面是windows下的ema ...
- 转:ASP.Net MVC:校验、AJAX与过滤器
原文地址:http://blog.jobbole.com/85005/ 一.校验 — 表单不是你想提想提就能提 1.1 DataAnnotations(数据注解) 位于 System.Componen ...
- linux 版本家族
1. 简单的说,在桌面系统上,可分为Debian和RedHat两大分支,然后Debian这一分支到现在比较火的是Ubuntu, RedHat比较火的是Fedora.贴一下它们的版本历史: fedor ...
- 【转】Linux內核驅動之GPIO子系統(一)GPIO的使用 _蝸牛
原文网址:http://tc.chinawin.net/it/os/article-2512b.html 一 概述 Linux內核中gpio是最簡單,最常用的資源(和interrupt ,dma,ti ...
- 【转】Install SmartGit via PPA in Ubuntu 13.10/13.04/12.04/Linux Mint
原文网址:http://ubuntuhandbook.org/index.php/2013/09/install-smartgit-via-ppa-ubuntu-linux-mint/ This tu ...
- EBS导出键弹性域
select gl_flexfields_pkg.get_description_sql(gcc.chart_of_accounts_id, 1, gcc.segment1) || '-' || gl ...
- hdu4623:crime 数学优化dp
鞍山热身赛的题,也是去年多校原题 题目大意: 求n个数的排列中满足相邻两个数互质的排列的数量并取模 当时的思路就是状压dp.. dp[i][state] state用二进制记录某个数是否被取走,i ...
- 最快速的“高斯”模糊算法(附Android源码)
这是一个外国人的算法,本人是搬运工.参考:http://blog.ivank.net/fastest-gaussian-blur.html 1:高斯模糊算法(参考:http://www.rua ...
- C++读写CSV文件
前两天看了<Reading and Writing CSV Files in MFC>(http://www.codeproject.com/Articles/53759/Reading- ...