-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy path6-zigzag-conversion.cpp
More file actions
108 lines (87 loc) · 2.76 KB
/
6-zigzag-conversion.cpp
File metadata and controls
108 lines (87 loc) · 2.76 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
Problem 6: ZigZag Conversion
https://leetcode.com/problems/zigzag-conversion/
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this:
P A H R
A P L S I I G
Y I R
And then read line by line: "PAHNAPLSIIGYIR"
Write the code that will take a string and make this conversion given a number of rows.
Example 1:
Input: s = "PAYPALISHIRING", numRows = 3
Output: "PAHNAPLSIIGYIR"
Example 2:
Input: s = "PAYPALISHIRING", numRows = 4
Output: "PINALSIGYAHRPI"
Example 3:
Input: s = "A", numRows = 1
Output: "A"
Constraints:
- 1 <= s.length <= 1000
- s consists of English letters (lower-case and upper-case), ',' and '.'.
- 1 <= numRows <= 1000
*/
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Solution {
public:
// Approach 1: Visit by Row - O(n) time, O(n) space
string convert(string s, int numRows) {
if (numRows == 1) return s;
vector<string> rows(min(numRows, (int)s.size()));
int curRow = 0;
bool goingDown = false;
for (char c : s) {
rows[curRow] += c;
if (curRow == 0 || curRow == numRows - 1) {
goingDown = !goingDown;
}
curRow += goingDown ? 1 : -1;
}
string result;
for (string row : rows) {
result += row;
}
return result;
}
// Approach 2: Visit by Row (Mathematical) - O(n) time, O(1) extra space
string convertMath(string s, int numRows) {
if (numRows == 1) return s;
string result;
int n = s.length();
int cycleLen = 2 * numRows - 2;
for (int i = 0; i < numRows; i++) {
for (int j = 0; j + i < n; j += cycleLen) {
result += s[j + i];
if (i != 0 && i != numRows - 1 && j + cycleLen - i < n) {
result += s[j + cycleLen - i];
}
}
}
return result;
}
};
// Test function
int main() {
Solution solution;
// Test cases
vector<pair<string, int>> testCases = {
{"PAYPALISHIRING", 3},
{"PAYPALISHIRING", 4},
{"A", 1},
{"AB", 1},
{"ABCDEFGHIJKLMNOP", 4}
};
cout << "Testing ZigZag Conversion:\n";
for (const auto& test : testCases) {
string result1 = solution.convert(test.first, test.second);
string result2 = solution.convertMath(test.first, test.second);
cout << "Input: \"" << test.first << "\", numRows: " << test.second
<< " -> Output: \"" << result1 << "\"\n";
cout << "Math approach: \"" << result2 << "\"\n";
cout << "Results match: " << (result1 == result2 ? "Yes" : "No") << "\n\n";
}
return 0;
}