LeetCode 59: Spiral Matrix II
2013-09-02 13:52
309 查看
Difficulty: 3
Frequency: 2
Problem:
Given an integer n, generate a square matrix filled with elements from 1 to n2 in
spiral order.
For example,
Given n =
You should return the following matrix:
Solution:
class Solution {
public:
vector<vector<int> > generateMatrix(int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector<int> row(n,0);
vector<vector<int> > matrix;
for (int i = 0; i<n; i++)
matrix.push_back(row);
char next_move = 0;
int i_row = 0, i_col = 0;
for (int i = 1; i<=n*n; i++)
{
matrix[i_row][i_col] = i;
switch(next_move){
case 0:
if (i_col==n-1||matrix[i_row][i_col+1]!=0)
{
++i_row;
next_move = (next_move+1)%4;
}
else
++i_col;
break;
case 1:
if (i_row==n-1||matrix[i_row+1][i_col]!=0)
{
--i_col;
next_move = (next_move+1)%4;
}
else
++i_row;
break;
case 2:
if (i_col==0||matrix[i_row][i_col-1]!=0)
{
--i_row;
next_move = (next_move+1)%4;
}
else
--i_col;
break;
case 3:
if (i_row==0||matrix[i_row-1][i_col]!=0)
{
++i_col;
next_move = (next_move+1)%4;
}
else
--i_row;
break;
}
}
return matrix;
}
};
Notes:
Don't know if there is a mathematical formula about this problem.
Frequency: 2
Problem:
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 ] ]
Solution:
class Solution {
public:
vector<vector<int> > generateMatrix(int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector<int> row(n,0);
vector<vector<int> > matrix;
for (int i = 0; i<n; i++)
matrix.push_back(row);
char next_move = 0;
int i_row = 0, i_col = 0;
for (int i = 1; i<=n*n; i++)
{
matrix[i_row][i_col] = i;
switch(next_move){
case 0:
if (i_col==n-1||matrix[i_row][i_col+1]!=0)
{
++i_row;
next_move = (next_move+1)%4;
}
else
++i_col;
break;
case 1:
if (i_row==n-1||matrix[i_row+1][i_col]!=0)
{
--i_col;
next_move = (next_move+1)%4;
}
else
++i_row;
break;
case 2:
if (i_col==0||matrix[i_row][i_col-1]!=0)
{
--i_row;
next_move = (next_move+1)%4;
}
else
--i_col;
break;
case 3:
if (i_row==0||matrix[i_row-1][i_col]!=0)
{
++i_col;
next_move = (next_move+1)%4;
}
else
--i_row;
break;
}
}
return matrix;
}
};
Notes:
Don't know if there is a mathematical formula about this problem.
相关文章推荐
- leetcode-59 Spiral Matrix II
- LeetCode_59---Spiral Matrix II
- leetcode——59——Spiral Matrix II
- LeetCode-59-Spiral Matrix II(Array)-Medium
- leetcode 59. Spiral Matrix II
- leetcode[59]Spiral Matrix II
- [leetcode-59]spiral matrixII(java)
- Leetcode 59. Spiral Matrix II
- LeetCode------54. Spiral Matrix(螺旋二维矩阵)-----59. Spiral Matrix II(n的螺旋矩阵)
- leetcode 59. Spiral Matrix II
- LeetCode 59 Spiral Matrix II(螺旋矩阵II)(Array)
- 【LEETCODE】59-Spiral Matrix II
- LeetCode 59 - Spiral Matrix II
- LeetCode54/59 Spiral Matrix I/II
- leetcode 59. Spiral Matrix II
- leetcode 59. Spiral Matrix II
- LeetCode59 Spiral Matrix II
- LeetCode 59. Spiral Matrix II
- LeetCode 59 Spiral Matrix II
- Leetcode59 Spiral Matrix II