-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path6.cpp
45 lines (42 loc) · 1.1 KB
/
6.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <gtest/gtest.h>
#include <string>
using namespace std;
class Solution {
public:
string convert(string s, int numRows)
{
string result;
if (numRows == 1) {
return s;
}
for (int i = 0; i < s.size(); i++) {
if (i % ((numRows - 1) << 1) == 0) {
result += s[i];
}
}
for (int k = 1; k < numRows - 1; k++) {
for (int i = 0; i < s.size(); i++) {
if (i % ((numRows - 1) << 1) == k ||
i % ((numRows - 1) << 1) == ((numRows - 1) << 1) - k) {
result += s[i];
}
}
}
for (int i = 0; i < s.size(); i++) {
if (i % ((numRows - 1) << 1) == numRows - 1) {
result += s[i];
}
}
return result;
}
};
class Testing : public testing::Test {
public:
Solution s;
};
TEST_F(Testing, Case)
{
EXPECT_EQ(s.convert("PAYPALISHIRING", 3), "PAHNAPLSIIGYIR");
EXPECT_EQ(s.convert("PAYPALISHIRING", 4), "PINALSIGYAHRPI");
EXPECT_EQ(s.convert("A", 1), "A");
}