forked from rost0413/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Spiral_Matrix.cpp
81 lines (73 loc) · 1.5 KB
/
Spiral_Matrix.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
Author: Weixian Zhou, ideazwx@gmail.com
Date: Jul 23, 2012
Problem: Spiral Matrix
Difficulty: easy
Source: http://www.leetcode.com/onlinejudge
Notes:
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].
Solution:
*/
#include <vector>
#include <set>
#include <climits>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cmath>
#include <cstring>
using namespace std;
class Solution {
public:
void nextStep(int dir, int x, int y, int &nx, int &ny) {
nx = x;
ny = y;
if (dir == 0) {
ny++;
} else if (dir == 1) {
nx++;
} else if (dir == 2) {
ny--;
} else {
nx--;
}
}
vector<int> spiralOrder(vector<vector<int> > &matrix) {
int m = matrix.size();
int n = (m != 0 ? matrix[0].size() : 0);
int x = 0, y = 0, dir = 0, nx, ny;
bool map[m][n];
vector<int> result;
if (m == 0) {
return result;
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
map[i][j] = true;
}
}
while (true) {
map[x][y] = false;
result.push_back(matrix[x][y]);
nextStep(dir, x, y, nx, ny);
if (!(0 <= nx && nx < m && 0 <= ny && ny < n && map[nx][ny])) {
dir = (dir + 1) % 4;
nextStep(dir, x, y, nx, ny);
if (!(0 <= nx && nx < m && 0 <= ny && ny < n && map[nx][ny])) {
break;
}
}
x = nx, y = ny;
}
return result;
}
};