leetcode Spiral Matrix || leetcode Spiral Matrix II

leetcode Spiral Matrix

Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.

For example,
Given the following matrix:

[
[ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ]
]

You should return [1,2,3,6,9,8,7,4,5].

题目地址:leetcode Spiral Matrix

题意:给定一个矩阵,按题目要求求出序列。。。

思路:直接循环模拟即可

C++

Python

 

 

leetcode 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 ]
]

题目地址:leetcode Spiral Matrix II

题意:这题和上面一题的区别在于,这题是给n,求从1~n组从的序列

思路直接用上面的代码改改即可。

C++

Python

 

本博客若无特殊说明则由 hrwhisper 原创发布
转载请点名出处:细语呢喃 > leetcode Spiral Matrix || leetcode Spiral Matrix II
本文地址:https://www.hrwhisper.me/leetcode-spiral-matrix-or-spiral-matrix-ii/

打赏一杯咖啡钱呗

Leetcode . permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *