【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 ...
随机推荐
- Arduino-定义串口
在一个老外写的代码中找到了一个非常好的定义串口的方法! Arduino用下面这种方法定义串口可以方便的把协议应用的任意的端口,大大提高了代码的修改性和移植性. 以下是范例: ...
- 前端高质量知识(三)-JS变量对象详解
在JavaScript中,我们肯定不可避免的需要声明变量和函数,可是JS解析器是如何找到这些变量的呢?我们还得对执行上下文有一个进一步的了解. 在上一篇文章中,我们已经知道,当调用一个函数时(激活), ...
- nginx里面的rewrite配置
哎,我需要静静,刚刚在去怎么优化dom层级,发现更新完代码,层级又蹭蹭蹭的往上涨,顿时没脾气了,还是把昨天的nginx配置总结下,增加点动力,昨天前天两天都在搞这个问题,也是搞的没脾气,网上查了很多资 ...
- hbase的coprocessor使用(转)
http://www.360doc.com/content/13/0320/09/4675893_272623864.shtml
- e.preventdefault() 别滥用
有的时候我们会为事件回调函数添加一个参数(通常是e),并在函数中加入e.preventdefault():以取消默认行为.由于习惯,我顺手将它写到了一个checkbox的change事件中.由于不同的 ...
- 第48章 MDK的编译过程及文件类型全解—零死角玩转STM32-F429系列
第48章 MDK的编译过程及文件类型全解 全套200集视频教程和1000页PDF教程请到秉火论坛下载:www.firebbs.cn 野火视频教程优酷观看网址:http://i.youku.co ...
- JAVA / MySql 编程—— 第三章 高级查询(一)
1. 修改表: (1)修改表名语法: ALTER TABLE <旧表名> RENAME [ TO ] <新表名>: 注意:其中[TO]为可选参数,使用与否不影响结 ...
- 【转摘】TFS上分支和标签的用法
引用路径:http://blog.csdn.net/cxzhq2002/article/details/8518250 什么时候用分支: 例如为某个客户定制的专用版本,和主干的特性有很大差别.不具通 ...
- python学习之列表和元组
配置环境:python 3.6 python编辑器:pycharm,代码如下: #!/usr/bin/python # -*- coding: UTF-8 -*- # list:是一种有序的集合,可以 ...
- McNay Art Museum【McNay艺术博物馆】
McNay Art Museum When I was 17, I read a magazine artice about a museum called the McNay, once the h ...