【Spiral Matrix II】cpp
题目:
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) {
vector<vector<int> > ret(n, vector<int>(n,));
int circle = n/;
int v = ;
for ( int c=; c<circle; ++c )
{
// up
for ( int col=c; col<n-c; ++col ) ret[c][col]=v++;
// right
for ( int row=c+; row<n-c-; ++row ) ret[row][n--c]=v++;
// down
for ( int col=n--c; col>=c; --col ) ret[n--c][col]=v++;
// left
for ( int row=n--c; row>c; --row) ret[row][c]=v++;
}
if ( n & )
{
ret[circle][circle]=v;
}
return ret;
}
};
tips:
按照Spiral Matrix的顺序走一遍元素,维护一个v每次运算后+1。
============================================
第二次过这道题,思路跟第一次一样。
class Solution {
public:
vector<vector<int>> generateMatrix(int n) {
vector<vector<int> > ret(n, vector<int>(n,));
if ( n< ) return ret;
int val = ;
for ( int i=; i<n/; ++i )
{
// north
for ( int p=i; p<n-i; ++p ) ret[i][p] = val++;
// east
for ( int p=i+; p<n--i; ++p ) ret[p][n--i] = val++;
// south
for ( int p=i; p<n-i; ++p ) ret[n--i][n--p] = val++;
// west
for ( int p=i+; p<n--i; ++p ) ret[n--p][i] = val++;
}
if ( n & ) ret[n/][n/] = val;
return ret;
}
};
【Spiral Matrix II】cpp的更多相关文章
- 【Word Break II】cpp
题目: Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where e ...
- 【Unique Paths II】cpp
题目: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. H ...
- 【Set Matrix Zeros】cpp
题目: Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. cl ...
- 【Path Sum II】cpp
题目: Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the give ...
- 【palindrome partitioning II】cpp
题目: Given a string s, partition s such that every substring of the partition is a palindrome. Return ...
- 【Jump Game II 】cpp
题目: Given an array of non-negative integers, you are initially positioned at the first index of the ...
- 【Combination Sum II 】cpp
题目: Given a collection of candidate numbers (C) and a target number (T), find all unique combination ...
- 【Word Ladder II】cpp
题目: Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) ...
- 【Single Num II】cpp
题目: Given an array of integers, every element appears three times except for one. Find that single o ...
随机推荐
- css样式设置高度不定文本垂直居中
使用css实现文本垂直居中,对于支持display: table的浏览器来说,是比较容易实现的,只需要对外层div设置为table,内层div设置为table-cell,并设置文本垂直居中即可.但对于 ...
- Poj(1182),种类并查集
题目链接:http://poj.org/problem?id=1182 再次熟练种类并查集,又积累点经验,和技巧,rank 0 2 1 先计算father[x] ,再更新rank[x]; #inclu ...
- Python 数据驱动 unittest + ddt
一数据驱动测试的含义: 在百度百科上的解释是: 数据驱动测试,即黑盒测试(Black-box Testing),又称为功能测试,是把测试对象看作一个黑盒子.利用黑盒测试法进行动态测试时,需要测试软件产 ...
- python对表格的使用
#!user/bin/env python # coding=utf- import xlrd def readExcelDataByName(filename, sheetName): '''读取E ...
- python 合并字符串
[root@chenbj python]# cat name.py #!/usr/bin/env python # _*_ coding:utf-8 _*_ first_name = "ch ...
- EF 连接 mysq l数据库 code first模式 的实践
准备工作: 1.下载vs2015 2.下载mysql2017 3.安装 开始: 1.创建 控制台文件 2.添加引用 Mysql.Data , Mysql.Data.Entity.EF6,Mysql.w ...
- 怎么让Sublime Text不自动打开最近的文件/项目
"hot_exit": false,"remember_open_files": false,
- block简介
ios4.0系统已开始支持block,在编程过程中,blocks被Obj-C看成是对象,它封装了一段代码,这段代码可以在任何时候执行.Blocks可以作为函数参数或者函数的返回值,而其本身又可以带输入 ...
- 【清真dp】cf1144G. Two Merged Sequences
成就:赛后在cf使用错误的贪心通过一题 成就:在cf上赛后提交hack数据 成就:在cf上赛后hack自己 题目大意 有一长度$n \le 2\times 10^5$的序列,要求判断是否能够划分为一个 ...
- 【nginx】root alias 区别,以及server root , location root 区别
nginx-root-alias-详解 最近在研究前后端分离站点配置在同一域名下,发现root,alias有区别,而且所有的root如果都放置在location下面访问无效的问题,才有此总结,本文只是 ...