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 ...
随机推荐
- windows driver inf文件
原来修改了inf文件会导致签名过的驱动包哈希值不正确了啊.现在才知道. = = http://www.chiphell.com/thread-827956-1-1.html
- Android 得到当前已连接的wifi的信号强度
1.得到当前已连接的wifi信息 WifiManager wifi_service = (WifiManager)getSystemService(WIFI_SERVICE); WifiInfo wi ...
- Visio 下载,及密钥
Visio2010简体中文高级版(永久激活密钥:GR24B-GC2XY-KRXRG-2TRJJ-4X7DC) ed2k://|file|cn_visio_2010_x64_516562.exe|515 ...
- java cannot find the tag library descriptor for "http://java.sun.com/jsp/jstl/core"
出现java cannot find the tag library descriptor for "http://java.sun.com/jsp/jstl/core" 主要的愿 ...
- BZOJ1116: [POI2008]CLO
1116: [POI2008]CLO Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 565 Solved: 303[Submit][Status] ...
- BZOJ1662: [Usaco2006 Nov]Round Numbers
1662: [Usaco2006 Nov]Round Numbers Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 147 Solved: 84[Sub ...
- struct2(六) 为表单添加验证
简介 为表单添加验证 添加校验的方法: 1. first name 不能为null 2. Email address 不能为null 3. age 必须大于18岁 为了在用户提交的时候,能够校验这个表 ...
- SQL - 删掉数据库
ALTER DATABASE [DB_NAME]SET OFFLINEWITH ROLLBACK IMMEDIATEGODROP DATABASE [DB_NAME]GO
- poj 2046 Gap(bfs+hash)
Description Let's play a card game called Gap. You have cards labeled with two-digit numbers. The fi ...
- pyqt时间
# -*- coding: utf-8 -*-__author__ = 'Administrator'from PyQt4 import QtCore, QtGui class Help(QtGui. ...