LeetCode OJ-- Spiral Matrix II
https://oj.leetcode.com/problems/spiral-matrix-ii/
螺旋矩阵,和题目一一样的思路,这个是产生n*n 矩阵。
#include <iostream>
#include <vector>
using namespace std; class Solution {
public:
vector<vector<int> > generateMatrix(int n) {
vector<vector<int> > ans;
if(n == )
return ans;
if(n ==)
{
vector<int> ansPiece;
ansPiece.push_back();
ans.push_back(ansPiece);
return ans;
}
ans.resize(n);
for(int i = ;i<n;i++)
ans[i].resize(n); int l1,l2,r2,p1;
l1 = ;
l2 = ;
r2 = n - ;
p1 = n -; int num = ;
while()
{
int i;
if(num>n*n)
break; for(i = l2; i <= r2; i++)
{
ans[l1][i] = num;
num++;
} if(l1+>p1)
break;
for(i = l1+;i<= p1;i++)
{
ans[i][r2] = num;
num++;
} if(r2-<l2)
break;
for(i = r2-;i>=l2;i--)
{
ans[p1][i] = num;
num++;
} if(p1-<l1+)
break;
for(i = p1-;i>=l1+;i--)
{
ans[i][l2] = num;
num++;
}
l1++;
l2++;
r2--;
p1--;
}
return ans;
}
};
LeetCode OJ-- Spiral Matrix II的更多相关文章
- 【leetcode】Spiral Matrix II
Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 to n2 in s ...
- Java for LeetCode 059 Spiral Matrix II
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
- [LeetCode] 59. Spiral Matrix II 螺旋矩阵 II
Given an integer n, generate a square matrix filled with elements from 1 to n^2 in spiral order. For ...
- LeetCode: 59. Spiral Matrix II(Medium)
1. 原题链接 https://leetcode.com/problems/spiral-matrix-ii/description/ 2. 题目要求 给定一个正整数n,求出从1到n平方的螺旋矩阵.例 ...
- 【leetcode】Spiral Matrix II (middle)
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
- Leetcode#59 Spiral Matrix II
原题地址 相比于Spiral Matrix(参见这篇文章)要简单一些,因为是方阵,所以代码简洁一些. 注意当n是奇数的时候,中心小块要单独赋值(代码21行) 代码: vector<vector& ...
- LeetCode 59. Spiral Matrix II (螺旋矩阵之二)
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
- LeetCode 58 Spiral Matrix II
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
- [leetcode]59. Spiral Matrix II螺旋遍历矩阵2
Given a positive integer n, generate a square matrix filled with elements from 1 to n^2 in spiral or ...
- LeetCode题目:Spiral Matrix II
原题地址:https://leetcode.com/problems/spiral-matrix-ii/ class Solution { public: vector<vector<in ...
随机推荐
- mongo 副本集+分片 配置
服务器规划如下: 副本集名称|服务器IP 192.168.56.111 192.168.56.112 192.168.56.113 shard1 3201 3201 3201 shard2 3202 ...
- python3 完全平方数(循环)
题目 一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少? 代码: for i in range(1,85): if 168 % i == 0: j = 168 ...
- hdu 5459
Problem Description I've sent Fang Fang around 201314 text messages in almost 5 years. Why can't she ...
- ACM-ICPC 2015 Shenyang Preliminary Contest B. Best Solver
The so-called best problem solver can easily solve this problem, with his/her childhood sweetheart. ...
- ACM训练联盟周赛 A. Teemo's bad day
65536K Today is a bad day. Teemo is scolded badly by his teacher because he didn't do his homework ...
- Angular Vue React 框架中的 CSS
框架中的 CSS Angular Vue React 三大框架 Angular Vue 内置样式集成 React 一些业界实践 Angular Angular . js (1.x):没有样式集成能力 ...
- VC6.0与Office2007~2010不兼容问题及解决方法
一.问题描述 启动打开文件对话框中,在 Visual C++ 使用的键盘快捷键或从文件菜单上将导致以下错误: 在 DEVSHL 中的访问冲突 (0xC0000005).在 0x5003eaed 的 D ...
- 求1+2+...+n 【微软面试100题 第十二题】
题目要求: 要求不能使用乘除法,for/while/if/else/switch/case等关键字以及条件判断语句(A?B:C). 参考资料:剑指offer第46题 题目分析: 方法1:利用类的静态成 ...
- Wordpress入门笔记
简单介绍一下wordpress个人操作,建议安装中文版. 登入后台管理者页面, 浏览器地址栏输入 (线上) http://XXXX.com/wp-login.php (本地) ht ...
- perl第三章 列表和数组
访问数组中的元素 $fred[0] $fred[1] $number=2.75; print $fred[$number-1] 结果就是print $fred[1] 特殊的数组索引1.对索 ...