diff --git a/99. ZigZag Conversion/README.md b/99. ZigZag Conversion/README.md new file mode 100644 index 0000000..3f641bb --- /dev/null +++ b/99. ZigZag Conversion/README.md @@ -0,0 +1,10 @@ +说实话,不太喜欢这种题。觉得娱乐性远大于对知识点考查。 + +况且解析字符串也百无聊奈。 + +这道题为了效率,肯定希望在一次迭代中完成对字符串的分组。而经过简单的分析,可以发现迭代的索引与分组的索引有非常规律的对应关系。 + +- 对(nRows-1)取余,可以区分行数。 +- 对(nRows-1)做除,可以区分当前是上升还是下降(偶数上升,奇数下降)。 + +主要策略就是这样了。代码总共五行,一看果然是 Easy 档次。 diff --git a/99. ZigZag Conversion/TEST.cc b/99. ZigZag Conversion/TEST.cc new file mode 100644 index 0000000..9f99a51 --- /dev/null +++ b/99. ZigZag Conversion/TEST.cc @@ -0,0 +1,10 @@ +#define CATCH_CONFIG_MAIN +#include "../Catch/single_include/catch.hpp" +#include "solution.h" + +TEST_CASE("ZigZag Conversion", "[convert]") +{ + Solution s; + REQUIRE( s.convert("PAYPALISHIRING", 3) == "PAHNAPLSIIGYIR" ); + REQUIRE( s.convert("ABCDE", 4) == "ABCED" ); +} diff --git a/99. ZigZag Conversion/solution.h b/99. ZigZag Conversion/solution.h new file mode 100644 index 0000000..68ea69d --- /dev/null +++ b/99. ZigZag Conversion/solution.h @@ -0,0 +1,18 @@ +#include +using std::string; +#include +#include + + +class Solution { +public: + string convert(string s, int nRows) { + if (s.empty() || nRows < 2) return s; + std::vector ret(nRows); + for (size_t i=0; i