Leetcode59. Spiral Matrix II螺旋矩阵2
给定一个正整数 n,生成一个包含 1 到 n2 所有元素,且元素按顺时针顺序螺旋排列的正方形矩阵。
示例:
输入: 3 输出: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ]
class Solution {
public:
vector<vector<int> > generateMatrix(int n)
{
vector<vector<int> > res(n, vector<int>(n, 0));
int left = 0;
int up = 0;
int right = n - 1;
int down = n - 1;
int cnt = 1;
while(up <= down && left <= right)
{
for(int i = left; i <= right; i++)
res[up][i] = cnt++;
up++;
if(up > down)
break;
for(int i = up; i <= down; i++)
res[i][right] = cnt++;
right--;
if(left > right)
break;
for(int i = right; i >= left; i--)
res[down][i] = cnt++;
down--;
if(up > down)
break;
for(int i = down; i >= up; i--)
res[i][left] = cnt++;
left++;
if(left > right)
break;
}
return res;
}
};
Leetcode59. Spiral Matrix II螺旋矩阵2的更多相关文章
- [Leetcode] spiral matrix 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 螺旋矩阵 II
Given an integer n, generate a square matrix filled with elements from 1 to n^2 in spiral order. For ...
- [LeetCode] Spiral Matrix II 螺旋矩阵之二
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
- leetcode-Spiral Matrix II 螺旋矩阵2之python大法好,四行就搞定,你敢信?
Spiral Matrix II 螺旋矩阵 Given an integer n, generate a square matrix filled with elements from 1 to n2 ...
- [LeetCode] 885. Spiral Matrix III 螺旋矩阵之三
On a 2 dimensional grid with R rows and C columns, we start at (r0, c0) facing east. Here, the north ...
- LeetCode 54. Spiral Matrix(螺旋矩阵)
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...
- PAT 1105 Spiral Matrix[模拟][螺旋矩阵][难]
1105 Spiral Matrix(25 分) This time your job is to fill a sequence of N positive integers into a spir ...
- [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(螺旋数组II)
Given a positive integer n, generate a square matrix filled with elements from 1 to n2 in spiral ord ...
随机推荐
- js 验证图片
var selectedImg = e.target.files[0]; //获取图片 var isPic = /^(image\/bmp|image\/gif|image\/jpeg|image\/ ...
- js遍历获取表格内数据方法
1.一般的表格结构如下 <table> <tr> <td>id</td> <td>name</td> </tr> & ...
- VS2010-MFC(对话框:非模态对话框的创建及显示)
转自:http://www.jizhuomi.com/software/162.html 前面已经说过,非模态对话框显示后,程序其他窗口仍能正常运行,可以响应用户输入,还可以相互切换.本节会将上一讲中 ...
- IDEA如何像Eclipse打开多个项目?
简述: 不能采用open方式,得采用 import module方式 (多个项目,可以不再同一个根目录下,真正的类似eclipse方式打开多个项目) 具体操作步骤: 1.选择一个maven项目,右键选 ...
- 北京信息科技大学校赛 题解 | AK记录贴
比赛链接:https://ac.nowcoder.com/acm/contest/940#question 花了一天时间全部解决,题目不难,全是基础题+模板题. A - kotori和糖果 链接:ht ...
- String类的trim() 方法
用于删除字符串的头尾空白符. 语法:public String trim() 返回值:删除头尾空白符的字符串. public class Test { public static void ma ...
- Java室内最短路径搜索(支持多楼层)
修改了上次的代码,现在支持室内的多楼层情况下的最短路径搜索,还是使用A*算法,把在GraphAdjList中VNode没有利用起来的data字段作为我们存储楼层属性的位置. 实际上是我偷懒了,正常情况 ...
- 使用HTTP代理
HTTP代理服务器可以比作客户端与Web服务器网站之间的一个信息中转站,客户端发送的HTTP请求和Web服务器返回的HTTP响应通过代理服务器转发给对方, 爬虫程序在爬取某些网站的时候也需要使用代理, ...
- hdu4764
hdu4764bash博弈主要是找准必胜状态,以及好好理解题意.这里的必胜状态是n-1,虽然是写的数比上一个大1到k,但是相当于这个人拿1到k,然后是累加的效果 #include<iostrea ...
- leetcode 75 Sorted Colors
两种解法 1)记录0和1的个数 然后按照记录的个数将0和1重新放入原数组,剩下的补2 2)双指针left,right left表示0~left-1都为0,即i之前都为0 right表示right+1~ ...