【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 ...
随机推荐
- "提取位于北坡的各类用地面积信息"的程序设计与实现
"提取位于北坡的各类用地面积信息"的程序设计与实现 程序员:左正康 发表时间:2013/12/20 14:24 代号:黑眼圈的日子 第一步:导入dem ...
- C++11 新特性之 序列for循环
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/lr982330245/article/details/30971195 在C++中在C++中for循 ...
- 贪心,二叉树搜索,ZOJ(2315)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1315 解题报告: #include <stdio.h> ...
- hadoop中compare函数
在看hadoop 的二次排序的时候,改写了下, 加了第三个参数, 本来以为是在 public int compareTo(IntPair o) { System.out.println(" ...
- CentOS中配置php环境
1.安装apache: yum install httpd httpd-devel 2.安装mysql: yum install mysql mysql-server 3.安装php: yum ...
- 面试-Spring理解
转自http://hui.sohu.com/infonews/article/6331404387079946240 spring呢,是pivotal公司维护的一系列开源工具的总称,最为人所知的是sp ...
- linux命令详解-useradd,groupadd
linux命令详解-useradd,groupadd 我们在linux命令行中输入useradd: Options: -b, --base-dir BASE_DIR base direc ...
- jquery ajax参数
//默认请求参数 var _options = { url: null, // 请求连接地址 type: 'GET', // 请求类型(get,post) data: null, // post时请求 ...
- LeetCode47.Permutations II(剑指offer38-1)
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- Eclipse中文乱码解决方案
Eclipse中文乱码解决方案 1)第一个设置:window>perferences>general>workspace>text file encoding 2)Jsp编码问 ...