forked from SjxSubham/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path6.Zigzag Conversion.cpp
More file actions
70 lines (57 loc) · 2.05 KB
/
6.Zigzag Conversion.cpp
File metadata and controls
70 lines (57 loc) · 2.05 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
class Solution {
public:
string convert(string s, int numRows) {
// Edge case: if numRows is 1 or greater than string length, return original string
if (numRows == 1 || numRows >= s.length()) {
return s;
}
// Create a vector to store characters for each row
vector<string> rows(numRows);
// Track current row and direction
int currentRow = 0;
bool goingDown = false;
// Iterate through each character in the string
for (char c : s) {
// Add character to current row
rows[currentRow] += c;
// Change direction when we hit top or bottom row
if (currentRow == 0 || currentRow == numRows - 1) {
goingDown = !goingDown;
}
// Move to next row based on direction
currentRow += goingDown ? 1 : -1;
}
// Concatenate all rows to get final result
string result;
for (string row : rows) {
result += row;
}
return result;
}
};
// Test cases
#include <iostream>
#include <vector>
using namespace std;
int main() {
Solution solution;
// Example 1
string s1 = "PAYPALISHIRING";
int numRows1 = 3;
cout << "Input: s = \"" << s1 << "\", numRows = " << numRows1 << endl;
cout << "Output: \"" << solution.convert(s1, numRows1) << "\"" << endl;
cout << "Expected: \"PAHNAPLSIIGYIR\"\n" << endl;
// Example 2
string s2 = "PAYPALISHIRING";
int numRows2 = 4;
cout << "Input: s = \"" << s2 << "\", numRows = " << numRows2 << endl;
cout << "Output: \"" << solution.convert(s2, numRows2) << "\"" << endl;
cout << "Expected: \"PINALSIGYAHRPI\"\n" << endl;
// Example 3
string s3 = "A";
int numRows3 = 1;
cout << "Input: s = \"" << s3 << "\", numRows = " << numRows3 << endl;
cout << "Output: \"" << solution.convert(s3, numRows3) << "\"" << endl;
cout << "Expected: \"A\"" << endl;
return 0;
}