This problem looks busy at first, but the accepted solution is built around one steady invariant. For 6. ZigZag Conversion, the solution in this repository is mainly a straightforward implementation solution.
Guide
What?
We want to turn the problem statement into a smaller set of decisions the computer can repeat safely. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: straightforward implementation.
Guide
When?
This is the kind of solution you want when the problem has structure hiding inside a messy-looking input. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are convert, rows.
Guide
Why?
The win comes from making each line carry responsibility: store the useful state, discard the rest, keep moving.
- The two-dimensional vector is the memory of the solution: grid state, DP state, or adjacency shape.
- The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.
Guide
How?
Walk through the solution in this order:
- Start from the smallest reliable state.
- Expand one legal move at a time.
- Cache, count, or merge information as soon as it becomes settled.
- Let the final stored value answer the original question.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- Time: O(N), space: O(N)
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01//Runtime: 116 ms, faster than 8.32% of C++ online submissions for ZigZag Conversion.
02//Memory Usage: 40 MB, less than 9.36% of C++ online submissions for ZigZag Conversion.
03class Solution {
04public:
05 string convert(string s, int numRows) {
06 if(numRows == 1) return s;
07
08 int n = s.size();
09
10 vector<vector<char>> zz(numRows, vector<char>(n, ' '));
11 int r = 0, c = 0;
12 //direction: false for down, true for up
13 bool d = false;
14
15 for(int i = 0; i < n; ++i){
16 // cout << "(" << r << ", " << c << ")" << endl;
17 zz[r][c] = s[i];
18 if(!d){
19 ++r;
20 }else{
21 --r;
22 ++c;
23 }
24
25 if(r == 0 || r == numRows-1)
26 d = !d;
27 }
28
29 string ans;
30
31 for(int r = 0; r < numRows; ++r){
32 for(int c = 0; c < n; ++c){
33 if(zz[r][c] != ' '){
34 ans += zz[r][c];
35 if(ans.size() == n) break;
36 }
37 }
38 if(ans.size() == n) break;
39 }
40
41 return ans;
42 }
43};
44
45//Approach 1: Sort by Row
46//Runtime: 12 ms, faster than 99.63% of C++ online submissions for ZigZag Conversion.
47//Memory Usage: 11 MB, less than 24.06% of C++ online submissions for ZigZag Conversion.
48//time: O(N), space: O(N)
49class Solution {
50public:
51 string convert(string s, int numRows) {
52 if(numRows == 1) return s;
53
54 int n = s.size();
55
56 vector<string> rows(min(numRows,n));
57 int r = 0;
58 bool down = true;
59
60 for(char c : s){
61 rows[r] += c;
62 r += down ? 1 : -1;
63 if(r == 0 || r == numRows-1)
64 down = !down;
65 }
66
67 string ans;
68 for(string& row : rows){
69 ans += row;
70 }
71 return ans;
72 }
73};
74
75//Approach 2: Visit by Row
76//Runtime: 12 ms, faster than 99.63% of C++ online submissions for ZigZag Conversion.
77//Memory Usage: 8.6 MB, less than 58.45% of C++ online submissions for ZigZag Conversion.
78//time: O(N), space: O(N)
79class Solution {
80public:
81 string convert(string s, int numRows) {
82 if(numRows == 1) return s;
83
84 int n = s.size();
85 int cycleLen = 2*(numRows - 1);
86 string ans;
87
88 /*
89 first row: k*cycleLen
90 last row: k*cycleLen+numRows-1
91 internal rows: k*cycleLen+i or (k+1)*cycleLen-i
92 */
93 for(int i = 0; i < numRows; ++i){
94 for(int k = 0; i + k * cycleLen < n; ++k){
95 ans += s[i+k*cycleLen];
96 if(i != 0 && i != numRows-1 && (k+1)*cycleLen-i < n){
97 ans += s[(k+1)*cycleLen-i];
98 }
99 }
100 }
101
102 return ans;
103 }
104};
Cost