-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzigzag_conversion.cpp
More file actions
62 lines (49 loc) · 1.58 KB
/
zigzag_conversion.cpp
File metadata and controls
62 lines (49 loc) · 1.58 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
6. Zigzag Conversion
Time Complexity: O(n) where n is length of string
Space Complexity: O(n) for storing rows
*/
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Solution {
public:
string convert(string s, int numRows) {
if (numRows == 1 || numRows >= s.length()) {
return s;
}
vector<string> rows(numRows);
int currentRow = 0;
bool goingDown = false;
for (char c : s) {
rows[currentRow] += c;
if (currentRow == 0 || currentRow == numRows - 1) {
goingDown = !goingDown;
}
currentRow += goingDown ? 1 : -1;
}
string result = "";
for (const string& row : rows) {
result += row;
}
return result;
}
};
// Test cases
int main() {
Solution solution;
// Example 1
cout << "Example 1: " << solution.convert("PAYPALISHIRING", 3) << endl; // Expected: "PAHNAPLSIIGYIR"
// Example 2
cout << "Example 2: " << solution.convert("PAYPALISHIRING", 4) << endl; // Expected: "PINALSIGYAHRPI"
// Example 3
cout << "Example 3: " << solution.convert("A", 1) << endl; // Expected: "A"
// Edge case: numRows = 2
cout << "Example 4: " << solution.convert("ABCDE", 2) << endl; // Expected: "ACEBD"
// Edge case: numRows >= len(s)
cout << "Example 5: " << solution.convert("ABC", 5) << endl; // Expected: "ABC"
// Test with numRows = 5
cout << "Example 6: " << solution.convert("ABCDEFGHIJKLMNO", 5) << endl;
return 0;
}