diff --git a/119. Spiral Matrix/README.md b/119. Spiral Matrix/README.md new file mode 100644 index 0000000..3f68059 --- /dev/null +++ b/119. Spiral Matrix/README.md @@ -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=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`. diff --git a/119. Spiral Matrix/TEST.cc b/119. Spiral Matrix/TEST.cc new file mode 100644 index 0000000..4b753c6 --- /dev/null +++ b/119. Spiral Matrix/TEST.cc @@ -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> matrix = { + {1,2,3}, + {4,5,6}, + {7,8,9} + + }; + std::vector ret{1,2,3,6,9,8,7,4,5}; + REQUIRE(s.spiralOrder(matrix) == ret); + } + SECTION( "One line" ) + { + std::vector> matrix = {{6,9,7}}; + std::vector ret{6,9,7}; + REQUIRE(s.spiralOrder(matrix) == ret); + } + SECTION( "Two line" ) + { + std::vector> matrix = { + {1,2,3,4,5,6,7,8,9,10}, + {11,12,13,14,15,16,17,18,19,20} + }; + std::vector 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> matrix = {{7}, {9}, {6}}; + std::vector ret{7,9,6}; + REQUIRE(s.spiralOrder(matrix) == ret); + } +} diff --git a/119. Spiral Matrix/solution.h b/119. Spiral Matrix/solution.h new file mode 100644 index 0000000..441137e --- /dev/null +++ b/119. Spiral Matrix/solution.h @@ -0,0 +1,27 @@ +#include +using std::vector; + +class Solution { +public: + vector spiralOrder(vector > &matrix) { + vector 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=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; + } +};