【一天一道LeetCode】#59. Spiral Matrix II
一天一道LeetCode系列
(一)题目
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 ]
]
(二)解题
思路参考: 【一天一道LeetCode】#54. Spiral Matrix
还是一样按圈赋值,每一圈的起点分别为(0,0),(1,1)…..
class Solution {
public:
vector<vector<int>> generateMatrix(int n) {
vector<vector<int>> ret;
if(n==0) return ret;
for(int i = 0 ; i < n ; i++)
{
vector<int> tmp(n,0);
ret.push_back(tmp);
}
int count = 0 ;
int px = 0 , py = 0;//初始值
int start = 0;//每一圈的起点
while(start<n/2)
{
int i;
for(i = py ; i<n - start; i++) {//从左往右
ret[px][i] = ++count;
}
py = i-1;
for(i = px+1 ; i<n - start ; i++){//从上往下
ret[i][py] = ++count;
}
px = i-1;
for(i = py-1 ; i>=start ; i--){//从右往左
ret[px][i] = ++count;
}
py = i+1;
for(i = px-1 ; i>=start + 1 ; i--){//从下往上
ret[i][py] = ++count;
}
start++;
px = py = start;//下一圈
}
if(n%2==1) ret[px][py] =++count;//n为奇数的时候需要考虑中心值
return ret;
}
};
【一天一道LeetCode】#59. Spiral Matrix II的更多相关文章
- [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#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]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 54. Spiral Matrix & 59. Spiral Matrix II
54. Spiral Matrix [Medium] Description Given a matrix of m x n elements (m rows, n columns), return ...
- leetcode 54. Spiral Matrix 、59. Spiral Matrix II
54题是把二维数组安卓螺旋的顺序进行打印,59题是把1到n平方的数字按照螺旋的顺序进行放置 54. Spiral Matrix start表示的是每次一圈的开始,每次开始其实就是从(0,0).(1,1 ...
- 【leetcode】59.Spiral Matrix II
Leetcode59 Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 ...
- 【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 ...
随机推荐
- k8s Kubernetes v1.10
#转移页面 http://www.cnblogs.com/elvi/p/8976305.html
- Android简易实战教程--第五十一话《使用Handler实现增加、减少、暂停计数》
转载博客请注明出处:道龙的博客 之前,写过一篇使用异步任务AysncTask实现倒计时的小案例,喜欢的话可以参考博客:Android简易实战教程--第三十三话< AsyncTask异步倒计时&g ...
- Errors occurred during the build. Errors running builder 'JavaScript Validator' on project '项目名'.
把JavaScript Validator去掉.去掉的方法是:选择一个项目--右键Properties--Builders(排第二)--点一下右侧会有四项--取消第一项"JavaScript ...
- leetcode 之 Single Number II
问题来源:Single Number II 问题描述:给定一个整数数组,除了一个整数出现一次之外,其余的每一个整数均出现三次,请找出这个出现一次的整数. 大家可能很熟悉另一个题目(Single Num ...
- 《An Industrial-Strength Audio Search Algorithm》译文
随着微信摇一摇逐渐被大众所广泛使用,听歌识曲功能也开始被关注.目前来看,像音乐雷达和微信摇一摇都采用了经典的shazam算法,为了使大家对shazam算法更加了解,我将其经典论文进行了翻译,希望对大家 ...
- Swift对象实例方法名混淆的解决
在Xcode7.x中,比如有以下一个类: class Foo{ func test(v:Int,before:Int)->Int{ return v + 1 } } 我可以直接这么做: let ...
- Android图表库MPAndroidChart(八)——饼状图的扩展:折线饼状图
Android图表库MPAndroidChart(八)--饼状图的扩展:折线饼状图 我们接着上文,饼状图的扩展,增加折现的说明,来看下我们要实现的效果 因为之前对MPAndroidChart的熟悉,所 ...
- Android TV开发总结(一)构建一个TV app前要知道的事儿
转载请把头部出处链接和尾部二维码一起转载,本文出自逆流的鱼yuiop:http://blog.csdn.net/hejjunlin/article/details/52792562 前言:近年来,智能 ...
- Oracle EBS各个模块日志收集的方法
MSCA(Mobile Supply Chain Application)日志的收集 Reference Note:338291.1 - Howto Enable WMS / MSCA Logging ...
- JQuery 网页选项卡制作
网页选项卡可以较好的利用有限的页面来展示更多的元素,而使用JQuery来制作网页选项卡也是一件非常简单的事情.今天就来分享一个网页选项卡的制作小技巧. 引入所需库 选项卡原理 业务核心 完整小例子 引 ...