LeetCode 58 Spiral Matrix II
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 ]
]
思路:与上一篇类似。仅仅要不越界就ok
public class Solution {
public int[][] generateMatrix(int n) {
if (n == 0)
return new int[0][0]; int[][] matrix = new int[n][n];
int x1 = 0;
int y1 = 0;
int x2 = matrix.length - 1;
int y2 = matrix[0].length - 1;
int i = 0, j = 1;
while (x1 <= x2 && y1 <= y2) {
// up row
for (i = y1; i <= y2; ++i, j++)
matrix[x1][i] = j;
// right column
for (i = x1 + 1; i <= x2; ++i, j++)
matrix[i][y2] = j;
// bottom row
for (i = y2 - 1; x2 != x1 && i >= y1; --i, j++)
matrix[x2][i] = j;
// left column
for (i = x2 - 1; y1 != y2 && i > x1; --i, j++)
matrix[i][y1] = j; x1++;
y1++;
x2--;
y2--;
}
return matrix;
}
}
版权声明:本文博主原创文章,博客,未经同意不得转载。
LeetCode 58 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]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 ...
随机推荐
- Web的工作机制
简要的介绍一下Web的工作机制,以便对开发JavaWeb项目有个更好的理解. 一.Web的概念 1.1 何为Web:Web是万维网(World Wide Web)的简称.Web出现以前, ...
- ProgressBar( 进度条) 组件
一. 加载方式 //class 加载方式<div class="easyui-progressbar"data-options="value:60" st ...
- GridView控件的光棒效应
// 光棒效应 protected void gvBookInfos_RowDataBound(object sender, GridViewRowEventArgs e) { if(e.Row.Ro ...
- JavaScript中函数参数的按值传递与按引用传递(即按地址传递)
首先声明一句:JavaScript中所有函数的参数都是按值传递的!不存在按引用传递! 在讲传递参数之前我们先来讲一下指针. 学过C指针的应该都知道,指针变量中保存的是一个地址,程序可以根据所保存的地址 ...
- sql server代理中作业执行SSIS包失败
RT,执行失败了,总是只提示一句“以xxxx用户身份执行失败”,很难找原因. 引用 http://bbs.csdn.net/topics/300059148 Sql2005如何用dtexec运行ssi ...
- Swift--基础(一)基本类型 符号 字符串(不熟的地方)
常量 变量 let age = 20 常量不可变 var num = 24 变量可变 let count:Int = 2 定义类型 Double(count) 类型转换 符号 1.?? let de ...
- ZOJ3557 How Many Sets II( Lucas定理)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud How Many Sets II Time Limit: 2 Seconds ...
- hdu5362 Just A String(dp)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Just A String Time Limit: 2000/1000 MS (J ...
- C++ 知识点1
typedef的陷阱 严格来说typedef并不是定义别名,而是定义类型,比如typedef int a;按照大部分书本说来,就是把a看做int,这种说法初学看来是正确的,也易于理解,但是遇到type ...
- 《图解CSS3》——笔记(二)
作者:大漠 勘误:http://www.w3cplus.com/book-comment.html 2014年7月15日15:58:11 第二章 CSS3选择器 2.1 认识CSS选择器 2.1. ...