https://leetcode.com/problems/zigzag-conversion/
The string is "PAYPALISHIRING"
written with a zigzag pattern on a given number of rows like this: (You may want to display this pattern In a fixed font for better legibility)
P A H NA p L S i i GY i R
And then read on line:"PAHNAPLSIIGYIR"
Write the code that would take a string and make this conversion given a number of rows:
String convert (string text, int nRows);
convert("PAYPALISHIRING", 3)
should return "PAHNAPLSIIGYIR"
.
Class Solution {public: string Convert (string s, int numrows) { if (numrows = = 1) return s; string* a = new string[numrows]; string ans; int n = 2 * numRows-2; for (int i = 0;i < S.size (); i++) { int r = i% n; int L = r% NumRows; if (L = = r) a[l] + = s[i]; Else A[numrows-l-2] + = S[i]; } for (int i = 0;i < numrows;i++) { ans + = a[i]; } return ans; };
Leetcode 6 ZigZag Conversion Simulation Difficulty: 0