From 83c66fc158d37e6d197ccbe11457c9bd363e087e Mon Sep 17 00:00:00 2001 From: Sainnhe Park Date: Wed, 2 Nov 2022 20:19:54 +0800 Subject: [PATCH] s0006 --- include/s0006_zigzag_conversion.hpp | 20 +++++++++++ src/s0006_zigzag_conversion.cpp | 51 +++++++++++++++++++++++++++++ tests/s0006_zigzag_conversion.cpp | 27 +++++++++++++++ 3 files changed, 98 insertions(+) create mode 100644 include/s0006_zigzag_conversion.hpp create mode 100644 src/s0006_zigzag_conversion.cpp create mode 100644 tests/s0006_zigzag_conversion.cpp diff --git a/include/s0006_zigzag_conversion.hpp b/include/s0006_zigzag_conversion.hpp new file mode 100644 index 0000000..430a408 --- /dev/null +++ b/include/s0006_zigzag_conversion.hpp @@ -0,0 +1,20 @@ +#ifndef S0006_ZIGZAG_CONVERSION +#define S0006_ZIGZAG_CONVERSION + +#include + +using namespace std; + +class Solution { + public: + /** + * @brief Zigzag Conversion + * + * @param s the string to be converted + * @param numRows rows + * @return converted string + */ + string convert(string s, int numRows); +}; + +#endif diff --git a/src/s0006_zigzag_conversion.cpp b/src/s0006_zigzag_conversion.cpp new file mode 100644 index 0000000..61699d3 --- /dev/null +++ b/src/s0006_zigzag_conversion.cpp @@ -0,0 +1,51 @@ +#include "s0006_zigzag_conversion.hpp" + +// 简单地找规律 + +// n = 3 +// 0 4 8 12 2*(n-1) * col +// 1 3 5 ... (n-1) * col + 1 +// 2 6 10 2*(n-1) * col + (n-1) + +// n = 4 +// 0 6 12 2*(n-1)*col +// 1 5 7 11 2*(n-1)*col+row, 2*(n-1)*col-row +// 2 4 8 10 2*(n-1)*col+row, 2*(n-1)*col-row +// 3 9 2*(n-1)*col + (n-1) + +string Solution::convert(string s, int numRows) { + string r = ""; + int l = s.length(); + if (l == 1 || numRows == 1) { + return s; + } + for (int col = 0; 2 * (numRows - 1) * col < l; col++) { + r += s[2 * (numRows - 1) * col]; + } + if (numRows >= 3) { + for (int row = 1; row < numRows - 1; row++) { + for (int col = 0;; col++) { + if (col == 0) { + if (2 * (numRows - 1) * col + row < l) { + r += s[2 * (numRows - 1) * col + row]; + } + } else { + if (2 * (numRows - 1) * col - row < l) { + r += s[2 * (numRows - 1) * col - row]; + } else { + break; + } + if (2 * (numRows - 1) * col + row < l) { + r += s[2 * (numRows - 1) * col + row]; + } else { + break; + } + } + } + } + } + for (int col = 0; 2 * (numRows - 1) * col + (numRows - 1) < l; col++) { + r += s[2 * (numRows - 1) * col + (numRows - 1)]; + } + return r; +} diff --git a/tests/s0006_zigzag_conversion.cpp b/tests/s0006_zigzag_conversion.cpp new file mode 100644 index 0000000..b9fc807 --- /dev/null +++ b/tests/s0006_zigzag_conversion.cpp @@ -0,0 +1,27 @@ +#include "s0006_zigzag_conversion.hpp" + +#include + +TEST(Problem6, Case1) { + string s("PAYPALISHIRING"); + int rows = 3; + string r("PAHNAPLSIIGYIR"); + Solution solution; + EXPECT_TRUE(solution.convert(s, rows) == r); +} + +TEST(Problem6, Case2) { + string s("PAYPALISHIRING"); + int rows = 4; + string r("PINALSIGYAHRPI"); + Solution solution; + EXPECT_TRUE(solution.convert(s, rows) == r); +} + +TEST(Problem6, Case3) { + string s("AB"); + int rows = 4; + string r("AB"); + Solution solution; + EXPECT_TRUE(solution.convert(s, rows) == r); +}