题目:

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的更多相关文章

  1. 【Word Break II】cpp

    题目: Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where e ...

  2. 【Unique Paths II】cpp

    题目: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. H ...

  3. 【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 ...

  4. 【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 ...

  5. 【palindrome partitioning II】cpp

    题目: Given a string s, partition s such that every substring of the partition is a palindrome. Return ...

  6. 【Jump Game II 】cpp

    题目: Given an array of non-negative integers, you are initially positioned at the first index of the ...

  7. 【Combination Sum II 】cpp

    题目: Given a collection of candidate numbers (C) and a target number (T), find all unique combination ...

  8. 【Word Ladder II】cpp

    题目: Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) ...

  9. 【Single Num II】cpp

    题目: Given an array of integers, every element appears three times except for one. Find that single o ...

随机推荐

  1. 154. Find Minimum in Rotated Sorted Array II(Binary search)

    https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/description/ -- leetcode follo ...

  2. eplise中运行提示 A fatal error has been detected by the java runtime environment

    今天一同事出现运行项目时,提示 A fatal error has been detected by the java runtime environment,具体表现是使用我们框架,不能正常的打印日 ...

  3. 使用selenium grid与BrowserMobProxyServer联合使用

    背景:项目主要是做埋点数据,要使用 BrowserMobProxyServer,它相当于做一个代理,在你访问一个网页时,通过代理,获取打开网页的数据,对比你需要对比数据,所以这个工具提供获取页面请求的 ...

  4. POJ-2151 Check the difficulty of problems---概率DP好题

    题目链接: https://vjudge.net/problem/POJ-2151 题目大意: ACM比赛中,共M道题,T个队,pij表示第i队解出第j题的概率 问 每队至少解出一题且冠军队至少解出N ...

  5. ELF格式文件符号表全解析及readelf命令使用方法

    http://blog.csdn.net/edonlii/article/details/8779075 1. 读取ELF文件头: $ readelf -h signELF Header:  Magi ...

  6. spring教程(一):简单实现(转)

    转:https://www.cnblogs.com/Lemon-i/p/8398263.html  一.概念介绍 1. 一站式框架:管理项目中的对象.spring框架性质是容器(对象容器) 2. 核心 ...

  7. 2017.9.23 HTML学习总结----JavaScript实现输入验证

    定义:对于一个HTML页面中的表单,可以获取其中的各项表单域信息, 利用这些信息,可以判定个表单域所提供的输入值是否合法,是否 符合所要求的格式,这就是表单的输入验证. (1)需要验证的表单输入域的要 ...

  8. 20145238-荆玉茗 《Java程序设计》实验三

    20145238-荆玉茗-实验三 实验三 敏捷开发与XP实践 实验内容 XP基础 XP核心实践 相关工具 实验步骤 (一)敏捷开发与XP 软件工程是把系统的.有序的.可量化的方法应用到软件的开发.运营 ...

  9. 求最长上升子序列(Lis模板)

    实现过程 定义已知序列数组为dp[]:dp[1…8]=389,207,155,300,299,170,158,65 我们定义一个序列B,然后令 i = 1 to 8 逐个考察这个序列.此外,我们用一个 ...

  10. Django 入门案例开发

    Django是一个重量级的web开发框架,它提供了很多内部已开发好的插件供我们使用:这里不去描述 Django直接进入开发过程. Django入门案例分两部分:一.开发环境的配置:二.业务需求分析. ...