LeetCode - Spiral Matrix

Spiral Matrix

2013.12.21 01:37

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:

  Given an m X n matrix, traverse it in a clockwise spiral order. My solution is to simulate the traversal. Two variables are required, the "position" and the "direction".

  Starting from the grid (0, 0), going right, the traversal keeps moving forward in one direction, until it hits a boundary or a traversed grid.

  The direction changes in a loop order: right->down->left->up.

  In this manner, all grids will be visited exactly once. Time complexity is O(m * n), space complexity is O(1).

Accepted code:

 1 // 1TLE, 1WA, 1AC, still not careful enough~

 2 class Solution {

 3 public:

 4     vector<int> spiralOrder(vector<vector<int> > &matrix) {

 5         // IMPORTANT: Please reset any member data you declared, as

 6         // the same Solution instance will be reused for each test case.

 7         int x, y, x1, y1;

 8         int dir;

 9         int m, n;

10         int **a;

11         int i, j;

12         int cc;

13         

14         result.clear();

15         m = matrix.size();

16         if(m <= 0){

17             return result;

18         }

19         n = matrix[0].size();

20         if(n <= 0){

21             return result;

22         }

23         

24         a = new int*[m];

25         for(i = 0; i < m; ++i){

26             a[i] = new int[n];

27         }

28         for(i = 0; i < m; ++i){

29             for(j = 0; j < n; ++j){

30                 a[i][j] = 0;

31             }

32         }

33         cc = m * n;

34         

35         x = y = 0;

36         dir = 0;

37         // 1TLE here, must check $cc immediately after push_back()

38         while(true){

39             a[x][y] = 1;

40             // 1WA here, it's $matrix[x][y], not $a[x][y]

41             result.push_back(matrix[x][y]);

42             --cc;

43             if(cc <= 0){

44                 break;

45             }

46             while(true){

47                 x1 = x + dd[dir][0];

48                 y1 = y + dd[dir][1];

49                 if(

50                     (x1 < 0 || x1 > m - 1) ||

51                     (y1 < 0 || y1 > n - 1) ||

52                     a[x1][y1] == 1

53                 ){

54                     dir = (dir + 1) % 4;

55                 }else{

56                     x = x1;

57                     y = y1;

58                     break;

59                 }

60             }

61         }

62         

63         for(i = 0; i < m; ++i){

64             delete[] a[i];

65         }

66         delete[] a;

67         

68         return result;

69     }

70 private:

71     int dd[4][2] = {

72         {0, 1}, {1, 0}, {0, -1}, {-1, 0}

73     };

74     vector<int> result;

75 };

 

你可能感兴趣的:(LeetCode)