【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 ...
随机推荐
- 安卓NDK流程
定义wrap类,声明native函数,加载库 package com.ndk.hello; public class Classs { public native String say_hello() ...
- web前端的10个顶级CSS UI开源框架
随着CSS3和HTML5的流行,我们的WEB页面不仅需要更人性化的设计理念,而且需要更酷的页面特效和用户体验.作为开发者,我们需要了解一些宝贵的CSS UI开源框架资源,它们可以帮助我们更快更好地实现 ...
- Invalid MyEclipse License - Discontinuing this MyEclipse operation. 出现这个错误怎么改正?
Invalid MyEclipse License - Discontinuing this MyEclipse operation这句话的意思是无效的许可证-停用此MyEclipse操作入门就是你的 ...
- Nodejs事件监听模块
nodejs里面是不存在浏览器里面都冒泡,捕获这些行为的,所以Nodejs实现了events这个模块,里面大多数的模块都集成了这个模块,所以events是node模块里面最重要都一个模块,他对外只暴露 ...
- ios相关配置
Deployment Target,它控制着运行应用需要的最低操作系统版本.
- Mac下安装OpenCV3.0和Anaconda和环境变量设置
入手Mac几天了,想在Mac OS下玩玩OpenCV和keras,间歇捣鼓了两天,终于搞定zsh.OpenCV3.0以及Anaconda.OpenCV3.0刚发布不久,这方面的资料也不是很多,能够查到 ...
- BCB:如何在BCB中使用CodeGuard
www.educity.cn 发布者:xjxyj2006 来源:网络转载 发布日期:2013年12月13日 文章评论 发表文章 一. 为什么写这篇东西 自己在使用 BCB5 写一些程序时需要检查很多东 ...
- poj_2689_Prime Distance
The branch of mathematics called number theory is about properties of numbers. One of the areas that ...
- sql语句(Oracle和sqlserver)
查询表的首句:(Oracle) select * from (select a.*, rownum as rn from tab_name a order by col )where rn = 1 o ...
- 搭建一个简单的dns缓存服务器
环境:linux 软件:bind97,bind97-utils, bind97-libs ip:192.168.192.130:192.168.192.131 -------------------- ...