随笔- 509  文章- 0  评论- 151  阅读- 22万 

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 };
复制代码

 

 posted on   zhuli19901106  阅读(232)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示