Skip to content

Commit

Permalink
added 119
Browse files Browse the repository at this point in the history
  • Loading branch information
pezy committed Feb 25, 2015
1 parent 3cda2b7 commit a9e075b
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 0 deletions.
41 changes: 41 additions & 0 deletions 119. Spiral Matrix/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
m * n 的矩阵按螺旋顺序转为数组:

1 2 3
4 5 6 --> 1 2 3 6 9 8 7 4 5
7 8 9

我想到的是,直接用下标作为限制。m 行,n 列,即范围:

- row: 0 ~ m
- col: 0 ~ n

首先是列递增,(0,0), (0,1), (0,2). 然后行递增,(1,2), (2,2). 再后是列递减,(2,1), (2,0). 和行递减,(1,0).
最后进入下一螺旋:(1,1). 到此为止,全部数组都迭代出来。

可以发现的规律是,每一次螺旋,都按照以下顺序:

1. 列递增 (row 降到最低)
2. 行递增 (col 增到最高)
3. 列递减 (row 增到最高)
4. 行递减 (col 降到最低)

写成 `for` 循环的形式,则为:

```cpp
for (; nMin<mMax && nMin<nMax; --mMax, --nMax, ++mMin, ++nMin) {
for (int i=nMin; i<nMax; ++i)
ret.push_back(matrix[mMin][i]);
for (int i=mMin+1; i<mMax; ++i)
ret.push_back(matrix[i][nMax-1]);
for (int i=nMax-2; i>=nMin; --i)
ret.push_back(matrix[mMax-1][i]);
for (int i=mMax-2; i>mMin; --i)
ret.push_back(matrix[i][nMin]);
}
```

除此之外,需要增加一些边界条件判断:

- `n = matrix[0].size();` // 要提前判断 matrix.empty()
- 第三个 `for` 循环要避免重复,如仅有一行的情况,列递增之后,没有行递增,直接就列递减了,那必然重复。判断 `mMax-1 != mMin`.
- 第四个 `for` 循环同理,避免行重复,判断 `nMax-1 != nMin`.
40 changes: 40 additions & 0 deletions 119. Spiral Matrix/TEST.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#define CATCH_CONFIG_MAIN
#include "../Catch/single_include/catch.hpp"
#include "solution.h"

TEST_CASE("Spiral Matrix", "spiralOrder")
{
Solution s;
SECTION( "Example" )
{
std::vector<std::vector<int>> matrix = {
{1,2,3},
{4,5,6},
{7,8,9}

};
std::vector<int> ret{1,2,3,6,9,8,7,4,5};
REQUIRE(s.spiralOrder(matrix) == ret);
}
SECTION( "One line" )
{
std::vector<std::vector<int>> matrix = {{6,9,7}};
std::vector<int> ret{6,9,7};
REQUIRE(s.spiralOrder(matrix) == ret);
}
SECTION( "Two line" )
{
std::vector<std::vector<int>> matrix = {
{1,2,3,4,5,6,7,8,9,10},
{11,12,13,14,15,16,17,18,19,20}
};
std::vector<int> ret{1,2,3,4,5,6,7,8,9,10,20,19,18,17,16,15,14,13,12,11};
REQUIRE(s.spiralOrder(matrix) == ret);
}
SECTION( "One Column" )
{
std::vector<std::vector<int>> matrix = {{7}, {9}, {6}};
std::vector<int> ret{7,9,6};
REQUIRE(s.spiralOrder(matrix) == ret);
}
}
27 changes: 27 additions & 0 deletions 119. Spiral Matrix/solution.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include <vector>
using std::vector;

class Solution {
public:
vector<int> spiralOrder(vector<vector<int> > &matrix) {
vector<int> ret;
if (matrix.empty()) return ret;
int mMax = matrix.size();
int nMax = matrix[0].size();
int mMin = 0, nMin = 0;
ret.reserve(mMax*nMax);
for (; nMin<mMax && nMin<nMax; --mMax, --nMax, ++mMin, ++nMin) {
for (int i=nMin; i<nMax; ++i)
ret.push_back(matrix[mMin][i]);
for (int i=mMin+1; i<mMax; ++i)
ret.push_back(matrix[i][nMax-1]);
if (mMax-1 == mMin) break;
for (int i=nMax-2; i>=nMin; --i)
ret.push_back(matrix[mMax-1][i]);
if (nMax-1 == nMin) break;
for (int i=mMax-2; i>mMin; --i)
ret.push_back(matrix[i][nMin]);
}
return ret;
}
};

0 comments on commit a9e075b

Please sign in to comment.