id int64 1 3.58k | problem_description stringlengths 516 21.8k | instruction int64 0 3 | solution_c dict |
|---|---|---|---|
6 | <p>The string <code>"PAYPALISHIRING"</code> is written in 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>
<pre>
P A H N
A P L S I I G
Y I R
</pre>
<p>And then read line by line: <code>"PAHNAPLSIIGYIR"</code></p>
<p>Write the code that will take a string and make this conversion given a number of rows:</p>
<pre>
string convert(string s, int numRows);
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 3
<strong>Output:</strong> "PAHNAPLSIIGYIR"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 4
<strong>Output:</strong> "PINALSIGYAHRPI"
<strong>Explanation:</strong>
P I N
A L S I G
Y A H R
P I
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "A", numRows = 1
<strong>Output:</strong> "A"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists of English letters (lower-case and upper-case), <code>','</code> and <code>'.'</code>.</li>
<li><code>1 <= numRows <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string convert(string s, int numRows) {\n string sol = \"\";\n vector<string> rows;\n for(int i=0; i<numRows; i++)\n rows.push_back(\"\");\n int row=0;\n if(numRows==1) return s;\n for(int i=0; i<s.size(); i++)\n {\n row = i%(2*numRows-2);\n if(row >= numRows) \n row = 2*numRows -2 -row;\n rows[row].push_back(s[i]);\n }\n\n for(int i=0; i<numRows; i++)\n sol+=rows[i];\n \n return sol;\n }\n};",
"memory": "17956"
} |
6 | <p>The string <code>"PAYPALISHIRING"</code> is written in 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>
<pre>
P A H N
A P L S I I G
Y I R
</pre>
<p>And then read line by line: <code>"PAHNAPLSIIGYIR"</code></p>
<p>Write the code that will take a string and make this conversion given a number of rows:</p>
<pre>
string convert(string s, int numRows);
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 3
<strong>Output:</strong> "PAHNAPLSIIGYIR"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 4
<strong>Output:</strong> "PINALSIGYAHRPI"
<strong>Explanation:</strong>
P I N
A L S I G
Y A H R
P I
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "A", numRows = 1
<strong>Output:</strong> "A"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists of English letters (lower-case and upper-case), <code>','</code> and <code>'.'</code>.</li>
<li><code>1 <= numRows <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string convert(string s, int numRows) {\n vector<vector<char>> arr(numRows + 1, vector<char>(0));\n int i = 0;\n while(i < s.size())\n {\n for(int j = 0; j < numRows; ++j)\n {\n if(i < s.size())\n {\n arr[j].push_back(s[i]);\n i++;\n }\n }\n for(int j = numRows - 2; j > 0; --j)\n {\n if(i < s.size())\n {\n arr[j].push_back(s[i]);\n i++;\n }\n }\n }\n i = 0;\n std::stringstream result;\n for (const auto& row : arr) {\n for (char c : row) {\n result << c;\n }\n }\n return result.str();\n }\n};",
"memory": "18368"
} |
6 | <p>The string <code>"PAYPALISHIRING"</code> is written in 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>
<pre>
P A H N
A P L S I I G
Y I R
</pre>
<p>And then read line by line: <code>"PAHNAPLSIIGYIR"</code></p>
<p>Write the code that will take a string and make this conversion given a number of rows:</p>
<pre>
string convert(string s, int numRows);
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 3
<strong>Output:</strong> "PAHNAPLSIIGYIR"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 4
<strong>Output:</strong> "PINALSIGYAHRPI"
<strong>Explanation:</strong>
P I N
A L S I G
Y A H R
P I
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "A", numRows = 1
<strong>Output:</strong> "A"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists of English letters (lower-case and upper-case), <code>','</code> and <code>'.'</code>.</li>
<li><code>1 <= numRows <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string convert(string s, int numRows) {\n if(numRows == 1) {\n return s;\n }\n vector<vector<char>> resultVec(numRows, vector<char>{});\n\n int index = 0;\n\n int rowIndex = 0;\n\n // 0 -> vertical\n // 1 -> cross\n int mode = 0;\n\n while(index < s.size()) {\n if(mode == 0) {\n if(rowIndex < numRows) {\n resultVec[rowIndex].push_back(s[index]);\n rowIndex++;\n index++;\n } else {\n mode = 1;\n rowIndex = numRows - 2;\n }\n } else {\n if(rowIndex >= 0) {\n resultVec[rowIndex].push_back(s[index]);\n rowIndex--;\n index++;\n } else {\n mode = 0;\n rowIndex = 1;\n }\n }\n }\n\n stringstream resultstream;\n\n for(int i = 0; i < resultVec.size(); i++) {\n for(char c : resultVec[i]) {\n resultstream << c;\n }\n }\n\n return resultstream.str();\n }\n};",
"memory": "18368"
} |
6 | <p>The string <code>"PAYPALISHIRING"</code> is written in 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>
<pre>
P A H N
A P L S I I G
Y I R
</pre>
<p>And then read line by line: <code>"PAHNAPLSIIGYIR"</code></p>
<p>Write the code that will take a string and make this conversion given a number of rows:</p>
<pre>
string convert(string s, int numRows);
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 3
<strong>Output:</strong> "PAHNAPLSIIGYIR"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 4
<strong>Output:</strong> "PINALSIGYAHRPI"
<strong>Explanation:</strong>
P I N
A L S I G
Y A H R
P I
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "A", numRows = 1
<strong>Output:</strong> "A"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists of English letters (lower-case and upper-case), <code>','</code> and <code>'.'</code>.</li>
<li><code>1 <= numRows <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string convert(string s, int numRows) {\n map<int,string>m;\n int k=0;\n int c=0;\n if(numRows==1 || numRows>=s.length()) return s;\n for(int i=0;i<s.size();i++){\n if(k<numRows && c==0){\n m[k]+=s[i];\n ++k;\n if (k==numRows){\n c=1;\n }\n }\n else if(c==1){\n k-=2;\n m[k]+=s[i];\n k++;\n \n if(k==1){\n c=0;\n \n }\n }\n \n \n \n }\n string sk;\n for(auto &it:m){\n sk+=it.second;\n }\n return sk;\n\n }\n};",
"memory": "18781"
} |
6 | <p>The string <code>"PAYPALISHIRING"</code> is written in 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>
<pre>
P A H N
A P L S I I G
Y I R
</pre>
<p>And then read line by line: <code>"PAHNAPLSIIGYIR"</code></p>
<p>Write the code that will take a string and make this conversion given a number of rows:</p>
<pre>
string convert(string s, int numRows);
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 3
<strong>Output:</strong> "PAHNAPLSIIGYIR"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 4
<strong>Output:</strong> "PINALSIGYAHRPI"
<strong>Explanation:</strong>
P I N
A L S I G
Y A H R
P I
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "A", numRows = 1
<strong>Output:</strong> "A"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists of English letters (lower-case and upper-case), <code>','</code> and <code>'.'</code>.</li>
<li><code>1 <= numRows <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string convert(string s, int numRows) {\n int idx = 0; int d=1;\n if (numRows == 1 || numRows >= s.length()) {\n return s;\n }\n vector<vector<char>> arr(numRows);\n for(auto c : s){\n arr[idx].push_back(c);\n if(idx == 0){\n d = 1;\n }\n else if (idx == numRows-1){\n d = -1;\n }\n idx +=d;\n }\n string ans = \"\";\n for (const auto row : arr) {\n for (char c : row) {\n ans += c;\n }\n }\n return ans;\n }\n};",
"memory": "18781"
} |
6 | <p>The string <code>"PAYPALISHIRING"</code> is written in 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>
<pre>
P A H N
A P L S I I G
Y I R
</pre>
<p>And then read line by line: <code>"PAHNAPLSIIGYIR"</code></p>
<p>Write the code that will take a string and make this conversion given a number of rows:</p>
<pre>
string convert(string s, int numRows);
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 3
<strong>Output:</strong> "PAHNAPLSIIGYIR"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 4
<strong>Output:</strong> "PINALSIGYAHRPI"
<strong>Explanation:</strong>
P I N
A L S I G
Y A H R
P I
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "A", numRows = 1
<strong>Output:</strong> "A"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists of English letters (lower-case and upper-case), <code>','</code> and <code>'.'</code>.</li>
<li><code>1 <= numRows <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n std::string convert(std::string s, int numRows) {\n if(numRows < 2) return s;\n int i = 0;\n std::string res{};\n //int arrSize = s.size() % numRows > 0 ? s.size() / numRows + 1 : s.size() / numRows;\n std::vector<std::vector<char>> arr;\n arr.reserve(numRows);\n int j = 0;\n for (; j < numRows&& i < s.length(); ++j, ++i) {\n arr.push_back({s[i]});\n }\n while (i < s.size()) {\n for(j = j = arr.size() - 2; j > 0&& i < s.length(); --j) {\n arr[j].push_back(s[i++]);\n\n }\n for(; j < numRows&& i < s.length(); ++j) {\n arr[j].push_back(s[i++]);\n }\n /*for(j = j - 2; j > 0&& i < s.length(); --j) {\n arr[j].push_back(s[i++]);\n\n }*/\n //--j;\n }\n for (auto arr1 : arr) {\n for (auto arr2 : arr1) {\n res += arr2;\n }\n }\n return res;\n}\n};",
"memory": "19193"
} |
6 | <p>The string <code>"PAYPALISHIRING"</code> is written in 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>
<pre>
P A H N
A P L S I I G
Y I R
</pre>
<p>And then read line by line: <code>"PAHNAPLSIIGYIR"</code></p>
<p>Write the code that will take a string and make this conversion given a number of rows:</p>
<pre>
string convert(string s, int numRows);
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 3
<strong>Output:</strong> "PAHNAPLSIIGYIR"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 4
<strong>Output:</strong> "PINALSIGYAHRPI"
<strong>Explanation:</strong>
P I N
A L S I G
Y A H R
P I
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "A", numRows = 1
<strong>Output:</strong> "A"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists of English letters (lower-case and upper-case), <code>','</code> and <code>'.'</code>.</li>
<li><code>1 <= numRows <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string convert(string s, int numRows) {\n if(numRows == 1 || numRows >= s.length())\n {\n return s;\n }\n vector<vector<char>> v(numRows);\n int i = 0, n = s.length(), k = 0, dir = 1;\n string st = \"\";\n while(i<n)\n {\n v[k].push_back(s[i]);\n i++;\n if(k == 0)\n {\n dir = 1;\n }\n else if(k == numRows-1)\n {\n dir = -1;\n }\n k += dir;\n }\n for(auto it : v)\n {\n for(auto ch : it)\n {\n st += ch;\n }\n }\n return st;\n }\n};",
"memory": "19193"
} |
6 | <p>The string <code>"PAYPALISHIRING"</code> is written in 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>
<pre>
P A H N
A P L S I I G
Y I R
</pre>
<p>And then read line by line: <code>"PAHNAPLSIIGYIR"</code></p>
<p>Write the code that will take a string and make this conversion given a number of rows:</p>
<pre>
string convert(string s, int numRows);
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 3
<strong>Output:</strong> "PAHNAPLSIIGYIR"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 4
<strong>Output:</strong> "PINALSIGYAHRPI"
<strong>Explanation:</strong>
P I N
A L S I G
Y A H R
P I
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "A", numRows = 1
<strong>Output:</strong> "A"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists of English letters (lower-case and upper-case), <code>','</code> and <code>'.'</code>.</li>
<li><code>1 <= numRows <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string convert(string s, int numRows) {\n vector<int>rows;\n int n=s.size();\n int i=0;\n int t=1;\n while(i<n)\n {\n t=1;\n while(t<=numRows)\n {\n rows.push_back(t);\n t++;\n i++;\n }\n t=numRows-1;\n while(t>1)\n {\n rows.push_back(t);\n t--;\n i++;\n }\n }\n vector<vector<char>> mapped(numRows+1);\n for(int i=0;i<n;i++)\n {\n int ind=rows[i];\n mapped[ind].push_back(s[i]);\n }\n string ans=\"\";\n for(int i=1;i<=numRows;i++)\n {\n for(auto it:mapped[i])\n {\n ans+=it;\n }\n }\n // cout<<rows<<endl;\n return ans;\n }\n};",
"memory": "19606"
} |
6 | <p>The string <code>"PAYPALISHIRING"</code> is written in 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>
<pre>
P A H N
A P L S I I G
Y I R
</pre>
<p>And then read line by line: <code>"PAHNAPLSIIGYIR"</code></p>
<p>Write the code that will take a string and make this conversion given a number of rows:</p>
<pre>
string convert(string s, int numRows);
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 3
<strong>Output:</strong> "PAHNAPLSIIGYIR"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 4
<strong>Output:</strong> "PINALSIGYAHRPI"
<strong>Explanation:</strong>
P I N
A L S I G
Y A H R
P I
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "A", numRows = 1
<strong>Output:</strong> "A"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists of English letters (lower-case and upper-case), <code>','</code> and <code>'.'</code>.</li>
<li><code>1 <= numRows <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string convert(string s, int numRows) {\n vector<vector<int>> zig(numRows);\n int r = 0;\n int d = numRows > 1 ? 1 : 0;\n for(int i=0;i<s.size();i++){\n zig[r].push_back(s[i]);\n if(r == numRows - 1){\n d *= -1;\n }\n if(r == 0 && d < 0){\n d *= -1;\n }\n \n r += d;\n }\n\n string str = \"\";\n for(vector<int> v : zig){\n for(int i : v){\n str += i;\n }\n }\n return str;\n }\n};",
"memory": "19606"
} |
6 | <p>The string <code>"PAYPALISHIRING"</code> is written in 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>
<pre>
P A H N
A P L S I I G
Y I R
</pre>
<p>And then read line by line: <code>"PAHNAPLSIIGYIR"</code></p>
<p>Write the code that will take a string and make this conversion given a number of rows:</p>
<pre>
string convert(string s, int numRows);
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 3
<strong>Output:</strong> "PAHNAPLSIIGYIR"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 4
<strong>Output:</strong> "PINALSIGYAHRPI"
<strong>Explanation:</strong>
P I N
A L S I G
Y A H R
P I
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "A", numRows = 1
<strong>Output:</strong> "A"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists of English letters (lower-case and upper-case), <code>','</code> and <code>'.'</code>.</li>
<li><code>1 <= numRows <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string convert(string s, int numRows) {\n \n int n=s.size();\n if(numRows==1) return s;\n vector<string> vec;\n vector<int> idx;\n for(int i=0; i<n; i++) idx.push_back(i);\n for(int i=0; i<numRows; i++) vec.push_back(\"\");\n int i=0, t=1, k=0;\n bool x=false;\n // cout<<n<<endl;\n for(int i=0; i<n; i++){\n vec[k]+=s[i];\n if(k==0 || k==numRows-1){\n x=!x;\n }\n if(x) k++;\n else k--;\n }\n\n string ans=\"\";\n for(auto i: vec) ans+=i;\n return ans;\n }\n};",
"memory": "20018"
} |
6 | <p>The string <code>"PAYPALISHIRING"</code> is written in 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>
<pre>
P A H N
A P L S I I G
Y I R
</pre>
<p>And then read line by line: <code>"PAHNAPLSIIGYIR"</code></p>
<p>Write the code that will take a string and make this conversion given a number of rows:</p>
<pre>
string convert(string s, int numRows);
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 3
<strong>Output:</strong> "PAHNAPLSIIGYIR"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 4
<strong>Output:</strong> "PINALSIGYAHRPI"
<strong>Explanation:</strong>
P I N
A L S I G
Y A H R
P I
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "A", numRows = 1
<strong>Output:</strong> "A"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists of English letters (lower-case and upper-case), <code>','</code> and <code>'.'</code>.</li>
<li><code>1 <= numRows <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\r\npublic:\r\n string convert(string s, int numRows) {\r\n unsigned n = s.size();\r\n if (numRows == 1 || numRows >= n) {\r\n return s;\r\n }\r\n vector<vector<char>> rows(numRows);\r\n unsigned m = 2 * (numRows - 1);\r\n for (unsigned i = 0; i < n; i++) {\r\n unsigned j = i % m;\r\n if (j >= numRows) {\r\n j = m - j;\r\n }\r\n rows[j].push_back(s[i]);\r\n }\r\n stringstream ss;\r\n for (auto &buf : rows) {\r\n buf.push_back('\\0');\r\n ss << buf.data();\r\n }\r\n return ss.str();\r\n }\r\n};",
"memory": "20018"
} |
6 | <p>The string <code>"PAYPALISHIRING"</code> is written in 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>
<pre>
P A H N
A P L S I I G
Y I R
</pre>
<p>And then read line by line: <code>"PAHNAPLSIIGYIR"</code></p>
<p>Write the code that will take a string and make this conversion given a number of rows:</p>
<pre>
string convert(string s, int numRows);
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 3
<strong>Output:</strong> "PAHNAPLSIIGYIR"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 4
<strong>Output:</strong> "PINALSIGYAHRPI"
<strong>Explanation:</strong>
P I N
A L S I G
Y A H R
P I
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "A", numRows = 1
<strong>Output:</strong> "A"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists of English letters (lower-case and upper-case), <code>','</code> and <code>'.'</code>.</li>
<li><code>1 <= numRows <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string convert(string s, int numRows) {\n string result;\n if(numRows == 1 || s.size() <= numRows)\n return s;\n unordered_map<int, vector<char>>test;\n int reverse = 1;\n int begin = 0;\n for(int i = 0;i<s.size();i++)\n {\n begin += reverse;\n if(begin == 1)\n reverse = 1;\n else if(begin == numRows)\n reverse = -1;\n test[begin].push_back(s.at(i));\n }\n //for(int i = 0;i<test[2].size();i++)\n //cout << test[2].at(i) <<endl;\n // int k = 0;\n // cout << test[1].at(0) << endl;\n // for(auto &a:test)\n // for(auto &b : a.second)\n // {\n // s.at(k++) = b;\n // }\n\n int k = 0;\n for(int i = 1;i<=numRows;i++)\n {\n for(int j = 0;j<test.at(i).size();j++)\n {\n //s.at((i-1)*numRows+j) = test.at(i).at(j);\n s.at(k++) = test.at(i).at(j);\n //result.push_back(test.at(i).at(j));\n } \n }\n\n return s;\n }\n};",
"memory": "20431"
} |
6 | <p>The string <code>"PAYPALISHIRING"</code> is written in 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>
<pre>
P A H N
A P L S I I G
Y I R
</pre>
<p>And then read line by line: <code>"PAHNAPLSIIGYIR"</code></p>
<p>Write the code that will take a string and make this conversion given a number of rows:</p>
<pre>
string convert(string s, int numRows);
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 3
<strong>Output:</strong> "PAHNAPLSIIGYIR"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 4
<strong>Output:</strong> "PINALSIGYAHRPI"
<strong>Explanation:</strong>
P I N
A L S I G
Y A H R
P I
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "A", numRows = 1
<strong>Output:</strong> "A"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists of English letters (lower-case and upper-case), <code>','</code> and <code>'.'</code>.</li>
<li><code>1 <= numRows <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string convert(string s, int numRows) {\n string result;\n if(numRows == 1 || s.size() <= numRows)\n return s;\n unordered_map<int, vector<char>>test;\n int reverse = 1;\n int begin = 0;\n for(int i = 0;i<s.size();i++)\n {\n begin += reverse;\n if(begin == 1)\n reverse = 1;\n else if(begin == numRows)\n reverse = -1;\n test[begin].push_back(s.at(i));\n }\n //for(int i = 0;i<test[2].size();i++)\n //cout << test[2].at(i) <<endl;\n // int k = 0;\n // cout << test[1].at(0) << endl;\n // for(auto &a:test)\n // for(auto &b : a.second)\n // {\n // s.at(k++) = b;\n // }\n\n int k = 0;\n for(int i = 1;i<=numRows;i++)\n {\n for(int j = 0;j<test.at(i).size();j++)\n {\n //s.at((i-1)*numRows+j) = test.at(i).at(j);\n s.at(k++) = test.at(i).at(j);\n //result.push_back(test.at(i).at(j));\n } \n }\n\n return s;\n }\n};",
"memory": "20431"
} |
6 | <p>The string <code>"PAYPALISHIRING"</code> is written in 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>
<pre>
P A H N
A P L S I I G
Y I R
</pre>
<p>And then read line by line: <code>"PAHNAPLSIIGYIR"</code></p>
<p>Write the code that will take a string and make this conversion given a number of rows:</p>
<pre>
string convert(string s, int numRows);
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 3
<strong>Output:</strong> "PAHNAPLSIIGYIR"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 4
<strong>Output:</strong> "PINALSIGYAHRPI"
<strong>Explanation:</strong>
P I N
A L S I G
Y A H R
P I
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "A", numRows = 1
<strong>Output:</strong> "A"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists of English letters (lower-case and upper-case), <code>','</code> and <code>'.'</code>.</li>
<li><code>1 <= numRows <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string convert(string s, int n) {\n if(n == 1) return s;\n string t = \"\";\n map<int, int> l, r;\n for(int d = 0; d < n; d++){\n l[d] = 2*(n-1-d);\n r[d] = 2*d;\n }\n for(int row = 0; row < n; row++){\n for(int i = row; i < s.size(); i += 2*(n-1)){\n if(row == 0 || row == n-1) t += s[i];\n else{\n t += s[i];\n if(i + l[row] < s.size()) t += s[i+l[row]];\n }\n } \n }\n return t;\n }\n};",
"memory": "20843"
} |
6 | <p>The string <code>"PAYPALISHIRING"</code> is written in 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>
<pre>
P A H N
A P L S I I G
Y I R
</pre>
<p>And then read line by line: <code>"PAHNAPLSIIGYIR"</code></p>
<p>Write the code that will take a string and make this conversion given a number of rows:</p>
<pre>
string convert(string s, int numRows);
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 3
<strong>Output:</strong> "PAHNAPLSIIGYIR"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 4
<strong>Output:</strong> "PINALSIGYAHRPI"
<strong>Explanation:</strong>
P I N
A L S I G
Y A H R
P I
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "A", numRows = 1
<strong>Output:</strong> "A"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists of English letters (lower-case and upper-case), <code>','</code> and <code>'.'</code>.</li>
<li><code>1 <= numRows <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string convert(string s, int numRows) {\n string result;\n if(numRows == 1 || s.size() <= numRows)\n return s;\n unordered_map<int, vector<char>>test;\n int reverse = 1;\n int begin = 0;\n for(int i = 0;i<s.size();i++)\n {\n begin += reverse;\n if(begin == 1)\n reverse = 1;\n else if(begin == numRows)\n reverse = -1;\n test[begin].push_back(s.at(i));\n }\n //for(int i = 0;i<test[2].size();i++)\n //cout << test[2].at(i) <<endl;\n //int k = 0;\n //cout << test[1].at(0) << endl;\n // for(auto &a:test)\n // for(auto &b : a.second)\n // {\n // cout << \"j: \" << j <<endl;\n // cout << \"char: \" << b <<endl;\n // s.at(j++) = b;\n // }\n\n //int k = 0;\n for(int i = 1;i<=numRows;i++)\n {\n for(int j = 0;j<test.at(i).size();j++)\n {\n //s.at((i-1)*numRows+j) = test.at(i).at(j);\n result.push_back(test.at(i).at(j));\n } \n }\n\n return result;\n }\n};",
"memory": "20843"
} |
6 | <p>The string <code>"PAYPALISHIRING"</code> is written in 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>
<pre>
P A H N
A P L S I I G
Y I R
</pre>
<p>And then read line by line: <code>"PAHNAPLSIIGYIR"</code></p>
<p>Write the code that will take a string and make this conversion given a number of rows:</p>
<pre>
string convert(string s, int numRows);
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 3
<strong>Output:</strong> "PAHNAPLSIIGYIR"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 4
<strong>Output:</strong> "PINALSIGYAHRPI"
<strong>Explanation:</strong>
P I N
A L S I G
Y A H R
P I
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "A", numRows = 1
<strong>Output:</strong> "A"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists of English letters (lower-case and upper-case), <code>','</code> and <code>'.'</code>.</li>
<li><code>1 <= numRows <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string convert(string s, int numRows) {\n string result;\n if(numRows == 1 || s.size() <= numRows)\n return s;\n unordered_map<int, vector<char>>test;\n int reverse = 1;\n int begin = 0;\n for(int i = 0;i<s.size();i++)\n {\n begin += reverse;\n if(begin == 1)\n reverse = 1;\n else if(begin == numRows)\n reverse = -1;\n test[begin].push_back(s.at(i));\n }\n //for(int i = 0;i<test[2].size();i++)\n //cout << test[2].at(i) <<endl;\n //int k = 0;\n //cout << test[1].at(0) << endl;\n // for(auto &a:test)\n // for(auto &b : a.second)\n // {\n // cout << \"j: \" << j <<endl;\n // cout << \"char: \" << b <<endl;\n // s.at(j++) = b;\n // }\n\n //int k = 0;\n for(int i = 1;i<=numRows;i++)\n {\n for(int j = 0;j<test.at(i).size();j++)\n {\n //s.at((i-1)*numRows+j) = test.at(i).at(j);\n result.push_back(test.at(i).at(j));\n } \n }\n\n return result;\n }\n};",
"memory": "21256"
} |
6 | <p>The string <code>"PAYPALISHIRING"</code> is written in 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>
<pre>
P A H N
A P L S I I G
Y I R
</pre>
<p>And then read line by line: <code>"PAHNAPLSIIGYIR"</code></p>
<p>Write the code that will take a string and make this conversion given a number of rows:</p>
<pre>
string convert(string s, int numRows);
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 3
<strong>Output:</strong> "PAHNAPLSIIGYIR"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 4
<strong>Output:</strong> "PINALSIGYAHRPI"
<strong>Explanation:</strong>
P I N
A L S I G
Y A H R
P I
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "A", numRows = 1
<strong>Output:</strong> "A"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists of English letters (lower-case and upper-case), <code>','</code> and <code>'.'</code>.</li>
<li><code>1 <= numRows <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string convert(string s, int numRows) {\n unordered_map<int, vector<char>> pattern;\n\n if (numRows == 1)\n {\n return s;\n }\n \n int i = 1;\n bool forward = true;\n for (const auto& ch : s)\n {\n pattern[i].push_back(ch);\n\n if (i == numRows)\n {\n forward = false;\n }\n if (i == 1)\n {\n forward = true;\n }\n i = forward ? i + 1: i - 1;\n }\n\n string zigzag;\n for (int j = 1; j <= numRows; ++j)\n {\n const auto& list = pattern[j];\n for (const auto& ch : list)\n {\n zigzag.push_back(ch);\n }\n }\n\n return zigzag;\n }\n};",
"memory": "21256"
} |
6 | <p>The string <code>"PAYPALISHIRING"</code> is written in 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>
<pre>
P A H N
A P L S I I G
Y I R
</pre>
<p>And then read line by line: <code>"PAHNAPLSIIGYIR"</code></p>
<p>Write the code that will take a string and make this conversion given a number of rows:</p>
<pre>
string convert(string s, int numRows);
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 3
<strong>Output:</strong> "PAHNAPLSIIGYIR"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 4
<strong>Output:</strong> "PINALSIGYAHRPI"
<strong>Explanation:</strong>
P I N
A L S I G
Y A H R
P I
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "A", numRows = 1
<strong>Output:</strong> "A"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists of English letters (lower-case and upper-case), <code>','</code> and <code>'.'</code>.</li>
<li><code>1 <= numRows <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string convert(string s, int numRows) {\n vector<string> buckets(numRows);\n\n vector<string*> round;\n for (auto i = 0; i < numRows; ++i) {\n round.emplace_back(&buckets[i]);\n }\n\n for (auto i = numRows - 2; i >= 1; --i) {\n round.emplace_back(&buckets[i]);\n }\n\n for (auto i = 0, rid = 0; i < s.size(); ++i, rid = (rid + 1) % round.size()) {\n round[rid]->push_back(s[i]);\n }\n\n auto res = reduce(buckets.begin(), buckets.end());\n return res;\n }\n};",
"memory": "21668"
} |
6 | <p>The string <code>"PAYPALISHIRING"</code> is written in 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>
<pre>
P A H N
A P L S I I G
Y I R
</pre>
<p>And then read line by line: <code>"PAHNAPLSIIGYIR"</code></p>
<p>Write the code that will take a string and make this conversion given a number of rows:</p>
<pre>
string convert(string s, int numRows);
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 3
<strong>Output:</strong> "PAHNAPLSIIGYIR"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 4
<strong>Output:</strong> "PINALSIGYAHRPI"
<strong>Explanation:</strong>
P I N
A L S I G
Y A H R
P I
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "A", numRows = 1
<strong>Output:</strong> "A"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists of English letters (lower-case and upper-case), <code>','</code> and <code>'.'</code>.</li>
<li><code>1 <= numRows <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string convert(string s, int numRows) {\n if(numRows == 1) {\n return s;\n }\n\n vector< vector<char> > rows;\n for(int i = 0; i < numRows; i++) {\n rows.push_back({});\n }\n\n int currentRow = 0;\n int direction = 1;\n for(int i = 0; i < s.size(); i++) {\n rows[currentRow].push_back(s[i]);\n if(currentRow + direction < 0 || currentRow + direction > numRows - 1) {\n direction *= -1;\n }\n currentRow += direction;\n }\n\n char resultArray[s.size() + 1];\n int idx = 0;\n for(auto row : rows) {\n for(auto ch : row) {\n resultArray[idx] = ch;\n idx++;\n }\n }\n resultArray[idx] = '\\0';\n return string(resultArray);\n }\n};",
"memory": "21668"
} |
6 | <p>The string <code>"PAYPALISHIRING"</code> is written in 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>
<pre>
P A H N
A P L S I I G
Y I R
</pre>
<p>And then read line by line: <code>"PAHNAPLSIIGYIR"</code></p>
<p>Write the code that will take a string and make this conversion given a number of rows:</p>
<pre>
string convert(string s, int numRows);
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 3
<strong>Output:</strong> "PAHNAPLSIIGYIR"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 4
<strong>Output:</strong> "PINALSIGYAHRPI"
<strong>Explanation:</strong>
P I N
A L S I G
Y A H R
P I
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "A", numRows = 1
<strong>Output:</strong> "A"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists of English letters (lower-case and upper-case), <code>','</code> and <code>'.'</code>.</li>
<li><code>1 <= numRows <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string convert(string s, int numRows) {\n string result;\n if(numRows == 1 || s.size() <= numRows)\n return s;\n map<int, vector<char>>test;\n int reverse = 1;\n int begin = 0;\n for(int i = 0;i<s.size();i++)\n {\n begin += reverse;\n if(begin == 1)\n reverse = 1;\n else if(begin == numRows)\n reverse = -1;\n test[begin].push_back(s.at(i));\n }\n int k = 0;\n cout << test[1].at(0) << endl;\n for(auto &a:test)\n for(auto &b : a.second)\n {\n //s.at(k++) = b;\n cout << b <<endl;\n result.push_back(b);\n }\n\n // int k = 0;\n // for(int i = 1;i<=numRows;i++)\n // {\n // for(int j = 0;j<test.at(i).size();j++)\n // {\n // //s.at((i-1)*numRows+j) = test.at(i).at(j);\n // s.at(k++) = test.at(i).at(j);\n // //result.push_back(test.at(i).at(j));\n // } \n // }\n\n return result;\n }\n};",
"memory": "22081"
} |
6 | <p>The string <code>"PAYPALISHIRING"</code> is written in 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>
<pre>
P A H N
A P L S I I G
Y I R
</pre>
<p>And then read line by line: <code>"PAHNAPLSIIGYIR"</code></p>
<p>Write the code that will take a string and make this conversion given a number of rows:</p>
<pre>
string convert(string s, int numRows);
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 3
<strong>Output:</strong> "PAHNAPLSIIGYIR"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 4
<strong>Output:</strong> "PINALSIGYAHRPI"
<strong>Explanation:</strong>
P I N
A L S I G
Y A H R
P I
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "A", numRows = 1
<strong>Output:</strong> "A"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists of English letters (lower-case and upper-case), <code>','</code> and <code>'.'</code>.</li>
<li><code>1 <= numRows <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string convert(string s, int numRows) {\n string result;\n if(numRows == 1 || s.size() <= numRows)\n return s;\n map<int, vector<char>>test;\n int reverse = 1;\n int begin = 0;\n for(int i = 0;i<s.size();i++)\n {\n begin += reverse;\n if(begin == 1)\n reverse = 1;\n else if(begin == numRows)\n reverse = -1;\n test[begin].push_back(s.at(i));\n }\n int k = 0;\n cout << test[1].at(0) << endl;\n for(auto &a:test)\n for(auto &b : a.second)\n {\n //s.at(k++) = b;\n cout << b <<endl;\n result.push_back(b);\n }\n\n // int k = 0;\n // for(int i = 1;i<=numRows;i++)\n // {\n // for(int j = 0;j<test.at(i).size();j++)\n // {\n // //s.at((i-1)*numRows+j) = test.at(i).at(j);\n // s.at(k++) = test.at(i).at(j);\n // //result.push_back(test.at(i).at(j));\n // } \n // }\n\n return result;\n }\n};",
"memory": "22081"
} |
6 | <p>The string <code>"PAYPALISHIRING"</code> is written in 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>
<pre>
P A H N
A P L S I I G
Y I R
</pre>
<p>And then read line by line: <code>"PAHNAPLSIIGYIR"</code></p>
<p>Write the code that will take a string and make this conversion given a number of rows:</p>
<pre>
string convert(string s, int numRows);
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 3
<strong>Output:</strong> "PAHNAPLSIIGYIR"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 4
<strong>Output:</strong> "PINALSIGYAHRPI"
<strong>Explanation:</strong>
P I N
A L S I G
Y A H R
P I
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "A", numRows = 1
<strong>Output:</strong> "A"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists of English letters (lower-case and upper-case), <code>','</code> and <code>'.'</code>.</li>
<li><code>1 <= numRows <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string convert(string s, int numRows) \n {\n int size = s.size();\n \n if (numRows == 1 || numRows >= size) {\n return s; \n }\n\n int pairCol = numRows - 1;\n int charInPair = numRows + (pairCol - 1);\n int maxpair = size / charInPair;\n int checkmaxpair = size % charInPair == 0 ? 0 : 1;\n\n int completeCol = maxpair * pairCol;\n int remainingChar = size % charInPair;\n\n int remainingCol = 0;\n if (remainingChar > numRows) {\n remainingCol = remainingChar - numRows;\n }\n if (checkmaxpair == 1) {\n remainingCol++;\n }\n completeCol += remainingCol;\n\n vector<vector<char>> ans(numRows, vector<char>(completeCol, ' '));\n \n int count = 0;\n int j = 0;\n\n \n for (int i = 0; i < completeCol && count < size; i++) {\n if (i % pairCol == 0) {\n // Vertical column\n for (j = 0; j < numRows && count < size; j++) {\n ans[j][i] = s[count++];\n }\n j = numRows - 2; \n } else {\n \n ans[j][i] = s[count++];\n j--; \n }\n }\n\n \n string answer;\n for (int j = 0; j < numRows; j++) {\n for (int k = 0; k < completeCol; k++) {\n if (ans[j][k] != ' ') {\n answer += ans[j][k];\n }\n }\n }\n \n return answer;\n }\n};",
"memory": "22493"
} |
6 | <p>The string <code>"PAYPALISHIRING"</code> is written in 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>
<pre>
P A H N
A P L S I I G
Y I R
</pre>
<p>And then read line by line: <code>"PAHNAPLSIIGYIR"</code></p>
<p>Write the code that will take a string and make this conversion given a number of rows:</p>
<pre>
string convert(string s, int numRows);
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 3
<strong>Output:</strong> "PAHNAPLSIIGYIR"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 4
<strong>Output:</strong> "PINALSIGYAHRPI"
<strong>Explanation:</strong>
P I N
A L S I G
Y A H R
P I
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "A", numRows = 1
<strong>Output:</strong> "A"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists of English letters (lower-case and upper-case), <code>','</code> and <code>'.'</code>.</li>
<li><code>1 <= numRows <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string convert(string s, int numRows) \n {\n int size = s.size();\n \n if (numRows == 1 || numRows >= size) {\n return s; \n }\n\n int pairCol = numRows - 1;\n int charInPair = numRows + (pairCol - 1);\n int maxpair = size / charInPair;\n int checkmaxpair = size % charInPair == 0 ? 0 : 1;\n\n int completeCol = maxpair * pairCol;\n int remainingChar = size % charInPair;\n\n int remainingCol = 0;\n if (remainingChar > numRows) {\n remainingCol = remainingChar - numRows;\n }\n if (checkmaxpair == 1) {\n remainingCol++;\n }\n completeCol += remainingCol;\n\n vector<vector<char>> ans(numRows, vector<char>(completeCol, ' '));\n \n int count = 0;\n int j = 0;\n\n \n for (int i = 0; i < completeCol && count < size; i++) {\n if (i % pairCol == 0) {\n // Vertical column\n for (j = 0; j < numRows && count < size; j++) {\n ans[j][i] = s[count++];\n }\n j = numRows - 2; \n } else {\n \n ans[j][i] = s[count++];\n j--; \n }\n }\n\n \n string answer;\n for (int j = 0; j < numRows; j++) {\n for (int k = 0; k < completeCol; k++) {\n if (ans[j][k] != ' ') {\n answer += ans[j][k];\n }\n }\n }\n \n return answer;\n }\n};",
"memory": "22493"
} |
6 | <p>The string <code>"PAYPALISHIRING"</code> is written in 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>
<pre>
P A H N
A P L S I I G
Y I R
</pre>
<p>And then read line by line: <code>"PAHNAPLSIIGYIR"</code></p>
<p>Write the code that will take a string and make this conversion given a number of rows:</p>
<pre>
string convert(string s, int numRows);
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 3
<strong>Output:</strong> "PAHNAPLSIIGYIR"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 4
<strong>Output:</strong> "PINALSIGYAHRPI"
<strong>Explanation:</strong>
P I N
A L S I G
Y A H R
P I
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "A", numRows = 1
<strong>Output:</strong> "A"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists of English letters (lower-case and upper-case), <code>','</code> and <code>'.'</code>.</li>
<li><code>1 <= numRows <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string convert(string s, int numRows) {\n if(numRows == 1) return s;\n unordered_map<int,vector<char>> cToRow;\n int count = 1;\n bool back = false;\n for(char c:s){\n cToRow[count].push_back(c);\n if(!back){\n if(++count == numRows) back = true;\n }\n else{\n if(--count == 1) back = false;\n }\n }\n stringstream ret;\n for(int i = 1; i <= numRows;i++){\n string str(cToRow[i].begin(),cToRow[i].end());\n ret << str;\n }\n return ret.str();\n }\n};",
"memory": "22906"
} |
6 | <p>The string <code>"PAYPALISHIRING"</code> is written in 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>
<pre>
P A H N
A P L S I I G
Y I R
</pre>
<p>And then read line by line: <code>"PAHNAPLSIIGYIR"</code></p>
<p>Write the code that will take a string and make this conversion given a number of rows:</p>
<pre>
string convert(string s, int numRows);
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 3
<strong>Output:</strong> "PAHNAPLSIIGYIR"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 4
<strong>Output:</strong> "PINALSIGYAHRPI"
<strong>Explanation:</strong>
P I N
A L S I G
Y A H R
P I
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "A", numRows = 1
<strong>Output:</strong> "A"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists of English letters (lower-case and upper-case), <code>','</code> and <code>'.'</code>.</li>
<li><code>1 <= numRows <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\nmap<int,vector<int>> mapps;\n string convert(string s, int numRows) {\n string ssols=\"\";\n int po=0;\n mapps[1].push_back(s[po]);\n po++;\n while(po<s.size()){\n for(int p=2;po<s.size()&&p+1<=numRows;p++){\n mapps[p].push_back(s[po]);\n po++;\n }\n for(int p=numRows;p>=1&&po<s.size();p--){\n mapps[p].push_back(s[po]);\n po++;\n }\n }\n for(int p=1;p<=numRows;p++){\n for(int po=0;po<mapps[p].size();po++){\n ssols+=mapps[p][po];}\n }\n return ssols;}\n};\n",
"memory": "22906"
} |
6 | <p>The string <code>"PAYPALISHIRING"</code> is written in 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>
<pre>
P A H N
A P L S I I G
Y I R
</pre>
<p>And then read line by line: <code>"PAHNAPLSIIGYIR"</code></p>
<p>Write the code that will take a string and make this conversion given a number of rows:</p>
<pre>
string convert(string s, int numRows);
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 3
<strong>Output:</strong> "PAHNAPLSIIGYIR"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 4
<strong>Output:</strong> "PINALSIGYAHRPI"
<strong>Explanation:</strong>
P I N
A L S I G
Y A H R
P I
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "A", numRows = 1
<strong>Output:</strong> "A"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists of English letters (lower-case and upper-case), <code>','</code> and <code>'.'</code>.</li>
<li><code>1 <= numRows <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\n const vector<pair<int, int>> directions{{0, 1}, {1, -1}};\npublic:\n string convert(string s, int numRows) {\n if(numRows == 1) return s;\n vector<vector<char>> solution{};\n int direction = 0;\n int x = 0;\n int y = 0;\n for(char ch : s) {\n if(x >= solution.size()) {\n solution.push_back(vector<char>(numRows, '\\0'));\n }\n solution[x][y] = ch;\n int shift = y + directions[direction].second;\n if(shift < 0 || shift >= numRows) {\n direction = direction != 0 ? 0 : 1;\n }\n x += directions[direction].first;\n y += directions[direction].second;\n }\n string result{};\n for(int j = 0; j < numRows; j++) {\n for(int i = 0; i < solution.size(); i++) {\n if(solution[i][j] != '\\0') result += solution[i][j];\n }\n }\n return result;\n }\n};",
"memory": "23318"
} |
6 | <p>The string <code>"PAYPALISHIRING"</code> is written in 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>
<pre>
P A H N
A P L S I I G
Y I R
</pre>
<p>And then read line by line: <code>"PAHNAPLSIIGYIR"</code></p>
<p>Write the code that will take a string and make this conversion given a number of rows:</p>
<pre>
string convert(string s, int numRows);
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 3
<strong>Output:</strong> "PAHNAPLSIIGYIR"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 4
<strong>Output:</strong> "PINALSIGYAHRPI"
<strong>Explanation:</strong>
P I N
A L S I G
Y A H R
P I
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "A", numRows = 1
<strong>Output:</strong> "A"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists of English letters (lower-case and upper-case), <code>','</code> and <code>'.'</code>.</li>
<li><code>1 <= numRows <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\n const vector<pair<int, int>> directions{{0, 1}, {1, -1}};\npublic:\n string convert(string s, int numRows) {\n if(numRows == 1) return s;\n vector<vector<char>> solution{};\n int direction = 0;\n int x = 0;\n int y = 0;\n for(char ch : s) {\n if(x >= solution.size()) {\n solution.push_back(vector<char>(numRows, '\\0'));\n }\n solution[x][y] = ch;\n int shift = y + directions[direction].second;\n if(shift < 0 || shift >= numRows) {\n direction = direction != 0 ? 0 : 1;\n }\n x += directions[direction].first;\n y += directions[direction].second;\n }\n string result{};\n for(int j = 0; j < numRows; j++) {\n for(int i = 0; i < solution.size(); i++) {\n if(solution[i][j] != '\\0') result += solution[i][j];\n }\n }\n return result;\n }\n};",
"memory": "23318"
} |
6 | <p>The string <code>"PAYPALISHIRING"</code> is written in 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>
<pre>
P A H N
A P L S I I G
Y I R
</pre>
<p>And then read line by line: <code>"PAHNAPLSIIGYIR"</code></p>
<p>Write the code that will take a string and make this conversion given a number of rows:</p>
<pre>
string convert(string s, int numRows);
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 3
<strong>Output:</strong> "PAHNAPLSIIGYIR"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 4
<strong>Output:</strong> "PINALSIGYAHRPI"
<strong>Explanation:</strong>
P I N
A L S I G
Y A H R
P I
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "A", numRows = 1
<strong>Output:</strong> "A"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists of English letters (lower-case and upper-case), <code>','</code> and <code>'.'</code>.</li>
<li><code>1 <= numRows <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string convert(string s, int numRows) {\n int j = 0;\n vector<vector<char>> list(numRows);\n for( int i = 0; i < numRows; i++ ) {\n vector<char> iL ;\n list.push_back(iL);\n }\n int n = s.size();\n while(j < n) {\n for( int i = 0; j < n && i < numRows; i++ ) {\n list[i].push_back(s[j]);\n j++;\n }\n for( int i = numRows-2; j < n && i > 0; i-- ) {\n list[i].push_back(s[j]);\n j++;\n }\n }\n string newString = \"\";\n for( vector<char> iL: list ) {\n // for( char ch : iL ) cout << ch << \" \";\n // cout << \"\\n\"; \n newString.insert(newString.end(), iL.begin(), iL.end());\n }\n return newString;\n }\n};",
"memory": "23731"
} |
6 | <p>The string <code>"PAYPALISHIRING"</code> is written in 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>
<pre>
P A H N
A P L S I I G
Y I R
</pre>
<p>And then read line by line: <code>"PAHNAPLSIIGYIR"</code></p>
<p>Write the code that will take a string and make this conversion given a number of rows:</p>
<pre>
string convert(string s, int numRows);
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 3
<strong>Output:</strong> "PAHNAPLSIIGYIR"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 4
<strong>Output:</strong> "PINALSIGYAHRPI"
<strong>Explanation:</strong>
P I N
A L S I G
Y A H R
P I
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "A", numRows = 1
<strong>Output:</strong> "A"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists of English letters (lower-case and upper-case), <code>','</code> and <code>'.'</code>.</li>
<li><code>1 <= numRows <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string convert(string s, int numRows) {\n bool goRight = true;\n vector<vector<char>> m(1, vector<char>(numRows, '0'));\n\n size_t row = 0;\n size_t col = 0;\n\n for (char c : s) {\n m[row][col] = c;\n if (goRight && col < numRows - 1) {\n col++;\n } else if (col == numRows - 1) {\n goRight = false;\n row++;\n if (col > 0)\n col--;\n m.push_back(vector<char>(numRows, '0'));\n } else if (!goRight && col > 0) {\n row++;\n col--;\n m.push_back(vector<char>(numRows, '0'));\n } else if (col == 0) {\n goRight = true;\n col++;\n }\n }\n\n string res;\n for (size_t j = 0; j < numRows; ++j) {\n for (size_t i = 0; i < m.size(); ++i) {\n if (m[i][j] != '0') {\n res += m[i][j];\n }\n }\n }\n return res;\n }\n};",
"memory": "23731"
} |
6 | <p>The string <code>"PAYPALISHIRING"</code> is written in 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>
<pre>
P A H N
A P L S I I G
Y I R
</pre>
<p>And then read line by line: <code>"PAHNAPLSIIGYIR"</code></p>
<p>Write the code that will take a string and make this conversion given a number of rows:</p>
<pre>
string convert(string s, int numRows);
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 3
<strong>Output:</strong> "PAHNAPLSIIGYIR"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 4
<strong>Output:</strong> "PINALSIGYAHRPI"
<strong>Explanation:</strong>
P I N
A L S I G
Y A H R
P I
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "A", numRows = 1
<strong>Output:</strong> "A"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists of English letters (lower-case and upper-case), <code>','</code> and <code>'.'</code>.</li>
<li><code>1 <= numRows <= 1000</code></li>
</ul>
| 3 | {
"code": "#define DEBUG 0\nclass Solution {\npublic:\n vector<vector<char>> createZigZagVector(int numRows)\n {\n vector<vector<char>> zigZagVector;\n vector<char> emptyCharVector;\n\n for (unsigned int i = 0; i < numRows; i++)\n {\n zigZagVector.push_back(emptyCharVector);\n }\n\n return zigZagVector;\n }\n\n void printZigZagVector(vector<vector<char>> zigZag)\n {\n for (unsigned int i = 0; i < zigZag.size(); i++)\n {\n cout << \"row \" << i << \": \";\n for (unsigned int j = 0; j < zigZag.at(i).size(); j++)\n {\n cout << zigZag.at(i).at(j) << \" \";\n }\n\n cout << endl;\n }\n }\n\n string convertVectorToStr(vector<vector<char>> zigZag)\n {\n string convertedStr = \"\";\n for (unsigned int curRowIndex = 0; curRowIndex < zigZag.size(); curRowIndex++)\n {\n for (unsigned int curCharIndex = 0; curCharIndex < zigZag.at(curRowIndex).size(); curCharIndex++)\n {\n convertedStr += zigZag.at(curRowIndex).at(curCharIndex);\n }\n }\n\n return convertedStr;\n }\n\n string convert(string s, int numRows) \n {\n if (numRows == 1)\n return s;\n\n vector<vector<char>> zigZag = createZigZagVector(numRows);\n int curRow = 0;\n bool diagonalTime = false;\n\n for (unsigned int i = 0; i < s.size(); i++)\n {\n if (DEBUG) {cout << \"pushing \" << s.at(i) << \" to row \" << curRow << endl;}\n zigZag.at(curRow).push_back(s.at(i));\n\n if (!diagonalTime)\n curRow++;\n\n if (curRow >= numRows) {\n diagonalTime = true;\n curRow--;\n }\n\n if (diagonalTime)\n {\n if (curRow > 0) {\n curRow--;\n }\n else {\n diagonalTime = false;\n curRow = 1;\n }\n \n }\n }\n\n if (DEBUG) {printZigZagVector(zigZag);}\n\n return convertVectorToStr(zigZag);\n }\n};",
"memory": "24143"
} |
6 | <p>The string <code>"PAYPALISHIRING"</code> is written in 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>
<pre>
P A H N
A P L S I I G
Y I R
</pre>
<p>And then read line by line: <code>"PAHNAPLSIIGYIR"</code></p>
<p>Write the code that will take a string and make this conversion given a number of rows:</p>
<pre>
string convert(string s, int numRows);
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 3
<strong>Output:</strong> "PAHNAPLSIIGYIR"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 4
<strong>Output:</strong> "PINALSIGYAHRPI"
<strong>Explanation:</strong>
P I N
A L S I G
Y A H R
P I
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "A", numRows = 1
<strong>Output:</strong> "A"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists of English letters (lower-case and upper-case), <code>','</code> and <code>'.'</code>.</li>
<li><code>1 <= numRows <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string convert(string s, int numRows) {\n if(numRows == 1) return s;\n int n = s.size();\n \n string ans = \"\";\n \n int k = 0;\n vector<vector<char>> temp;\n for(int i = 0; i < numRows*2 - 2; i++) {\n temp.push_back({});\n for(int j = i; j < n; j += (numRows - 2)*2+2){\n temp[i].push_back(s[j]);\n }\n }\n \n for(int i = 0; i < temp.size(); i++) {\n for(int j = 0; j < temp[i].size(); j++) {\n cout << temp[i][j] << \" \";\n }\n cout << \"\\n\";\n }\n \n for(int i = 0; i < temp[0].size(); i++) {\n ans += temp[0][i];\n }\n \n int l = 1, r = temp.size() - 1;\n while(l < r) {\n int len = max(temp[l].size(), temp[r].size());\n for(int i = 0; i < len; i++) {\n ans += temp[l][i];\n if(i < temp[r].size()) {\n ans += temp[r][i];\n }\n }\n l++;\n r--;\n }\n if(temp.size() > 1) {\n for(int i = 0; i < temp[l].size(); i++) {\n ans += temp[l][i];\n }\n }\n \n \n return ans;\n }\n};",
"memory": "24143"
} |
6 | <p>The string <code>"PAYPALISHIRING"</code> is written in 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>
<pre>
P A H N
A P L S I I G
Y I R
</pre>
<p>And then read line by line: <code>"PAHNAPLSIIGYIR"</code></p>
<p>Write the code that will take a string and make this conversion given a number of rows:</p>
<pre>
string convert(string s, int numRows);
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 3
<strong>Output:</strong> "PAHNAPLSIIGYIR"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 4
<strong>Output:</strong> "PINALSIGYAHRPI"
<strong>Explanation:</strong>
P I N
A L S I G
Y A H R
P I
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "A", numRows = 1
<strong>Output:</strong> "A"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists of English letters (lower-case and upper-case), <code>','</code> and <code>'.'</code>.</li>
<li><code>1 <= numRows <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string convert(string s, int numRows) {\n if (numRows == 1) return s; // Edge case for single row\n \n map<int, list<char>> noteit;\n short k = 1; // current row index\n bool down = true; // direction flag\n short i = 0;\n short n = s.size();\n \n while (i < n) {\n noteit[k].push_back(s[i++]);\n \n // Change direction when reaching the first or last row\n if (k == numRows) {\n down = false;\n } else if (k == 1) {\n down = true;\n }\n\n // Move the index up or down based on the current direction\n if (down == true) {\n k++;\n } else {\n k--;\n }\n }\n \n // Now build the result string\n string result;\n for (int j = 1; j <= numRows; j++) {\n while (!noteit[j].empty()) {\n result += noteit[j].front();\n noteit[j].pop_front();\n }\n }\n \n return result;\n }\n};\n\n",
"memory": "24556"
} |
6 | <p>The string <code>"PAYPALISHIRING"</code> is written in 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>
<pre>
P A H N
A P L S I I G
Y I R
</pre>
<p>And then read line by line: <code>"PAHNAPLSIIGYIR"</code></p>
<p>Write the code that will take a string and make this conversion given a number of rows:</p>
<pre>
string convert(string s, int numRows);
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 3
<strong>Output:</strong> "PAHNAPLSIIGYIR"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 4
<strong>Output:</strong> "PINALSIGYAHRPI"
<strong>Explanation:</strong>
P I N
A L S I G
Y A H R
P I
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "A", numRows = 1
<strong>Output:</strong> "A"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists of English letters (lower-case and upper-case), <code>','</code> and <code>'.'</code>.</li>
<li><code>1 <= numRows <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string convert(string s, short numRows) {\n if (numRows == 1) return s; // Edge case for single row\n \n map<short, list<char>> noteit;\n int k = 1; // current row index\n int down = 1; // direction flag\n int i = 0;\n int n = s.size();\n \n while (i < n) {\n noteit[k].push_back(s[i++]);\n \n // Change direction when reaching the first or last row\n if (k == numRows) {\n down = -1;\n } else if (k == 1) {\n down = 1;\n }\n\n // Move the index up or down based on the current direction\n if (down == 1) {\n k++;\n } else {\n k--;\n }\n }\n \n // Now build the result string\n string result;\n for (int j = 1; j <= numRows; j++) {\n while (!noteit[j].empty()) {\n result += noteit[j].front();\n noteit[j].pop_front();\n }\n }\n \n return result;\n }\n};\n\n\n\n",
"memory": "24556"
} |
6 | <p>The string <code>"PAYPALISHIRING"</code> is written in 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>
<pre>
P A H N
A P L S I I G
Y I R
</pre>
<p>And then read line by line: <code>"PAHNAPLSIIGYIR"</code></p>
<p>Write the code that will take a string and make this conversion given a number of rows:</p>
<pre>
string convert(string s, int numRows);
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 3
<strong>Output:</strong> "PAHNAPLSIIGYIR"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 4
<strong>Output:</strong> "PINALSIGYAHRPI"
<strong>Explanation:</strong>
P I N
A L S I G
Y A H R
P I
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "A", numRows = 1
<strong>Output:</strong> "A"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists of English letters (lower-case and upper-case), <code>','</code> and <code>'.'</code>.</li>
<li><code>1 <= numRows <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string convert(string s, int numRows) {\n \n map<int,vector<char>> mp;\n int size = s.length();\n int inc = 0;\n int val=0;\n for(int i=0;i<size;i++)\n {\n if(inc==0)\n {\n mp[val].push_back(s[i]);\n val++;\n }\n else{\n val--;\n mp[val].push_back(s[i]);\n }\n\n // reached bottom\n if(val==numRows)\n {\n inc = 1;\n val--;\n }\n if(val==0){\n inc = 0;\n val++;\n }\n }\n string str = \"\";\n for(auto i:mp){\n for(auto j:i.second){\n str+=j;\n }\n }\n return str;\n }\n\n};",
"memory": "24968"
} |
6 | <p>The string <code>"PAYPALISHIRING"</code> is written in 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>
<pre>
P A H N
A P L S I I G
Y I R
</pre>
<p>And then read line by line: <code>"PAHNAPLSIIGYIR"</code></p>
<p>Write the code that will take a string and make this conversion given a number of rows:</p>
<pre>
string convert(string s, int numRows);
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 3
<strong>Output:</strong> "PAHNAPLSIIGYIR"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 4
<strong>Output:</strong> "PINALSIGYAHRPI"
<strong>Explanation:</strong>
P I N
A L S I G
Y A H R
P I
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "A", numRows = 1
<strong>Output:</strong> "A"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists of English letters (lower-case and upper-case), <code>','</code> and <code>'.'</code>.</li>
<li><code>1 <= numRows <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string convert(string s, int numRows) {\n \n map<int,vector<char>> mp;\n int size = s.length();\n int inc = 0;\n int val=0;\n for(int i=0;i<size;i++)\n {\n if(inc==0) // case 1 : going up to down\n {\n mp[val].push_back(s[i]);\n val++;\n }\n else{ // case 2 : going bottom to up \n val--;\n mp[val].push_back(s[i]);\n }\n\n // reached bottom\n if(val==numRows) // reached bottom so change the direction by by changing direction\n {\n inc = 1;\n val--;\n }\n if(val==0){ // reached top so change direction by changin ic\n inc = 0;\n val++;\n }\n }\n string str = \"\";\n for(auto i:mp){\n for(auto j:i.second){\n str+=j;\n }\n }\n return str;\n }\n\n};",
"memory": "24968"
} |
6 | <p>The string <code>"PAYPALISHIRING"</code> is written in 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>
<pre>
P A H N
A P L S I I G
Y I R
</pre>
<p>And then read line by line: <code>"PAHNAPLSIIGYIR"</code></p>
<p>Write the code that will take a string and make this conversion given a number of rows:</p>
<pre>
string convert(string s, int numRows);
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 3
<strong>Output:</strong> "PAHNAPLSIIGYIR"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 4
<strong>Output:</strong> "PINALSIGYAHRPI"
<strong>Explanation:</strong>
P I N
A L S I G
Y A H R
P I
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "A", numRows = 1
<strong>Output:</strong> "A"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists of English letters (lower-case and upper-case), <code>','</code> and <code>'.'</code>.</li>
<li><code>1 <= numRows <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string convert(string s, int numRows) {\n if(numRows==1){\n return s;\n }\n map<int, vector<int>> h;\n int sign=1;\n int j=0;\n int i=1;\n while(s[j]!= '\\0'){\n h[i].push_back(s[j]);\n i+=sign;\n if(i==numRows||i==1){\n sign*=-1;\n }\n j++;\n }\n string soln=\"\";\n for(int k=1; k<=numRows; k++){\n vector<int> vec= h[k];\n string str(vec.begin(), vec.end()); \n soln+= str;\n }\n return soln;\n\n\n }\n};",
"memory": "25381"
} |
6 | <p>The string <code>"PAYPALISHIRING"</code> is written in 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>
<pre>
P A H N
A P L S I I G
Y I R
</pre>
<p>And then read line by line: <code>"PAHNAPLSIIGYIR"</code></p>
<p>Write the code that will take a string and make this conversion given a number of rows:</p>
<pre>
string convert(string s, int numRows);
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 3
<strong>Output:</strong> "PAHNAPLSIIGYIR"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 4
<strong>Output:</strong> "PINALSIGYAHRPI"
<strong>Explanation:</strong>
P I N
A L S I G
Y A H R
P I
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "A", numRows = 1
<strong>Output:</strong> "A"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists of English letters (lower-case and upper-case), <code>','</code> and <code>'.'</code>.</li>
<li><code>1 <= numRows <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string convert(string s, int numRows) {\n int index = 0;\n string a[numRows + 1];\n int r = 1;\n while(index < s.size())\n {\n if (r == numRows){\n a[numRows] += s[index];\n index ++;\n r = max(1,--r);\n while(index < s.size() && r >= 2) {\n a[r] += s[index];\n r--;\n index ++;\n }\n }\n else {\n a[r] += s[index];\n index ++;\n r++;\n }\n \n }\n for(int i = 1 ; i <= numRows ; i ++)cout << a[i] << endl;\n for(int i = 2 ; i <= numRows ;i ++)\n a[1] = a[1] + a[i];\n return a[1];\n }\n};",
"memory": "25381"
} |
6 | <p>The string <code>"PAYPALISHIRING"</code> is written in 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>
<pre>
P A H N
A P L S I I G
Y I R
</pre>
<p>And then read line by line: <code>"PAHNAPLSIIGYIR"</code></p>
<p>Write the code that will take a string and make this conversion given a number of rows:</p>
<pre>
string convert(string s, int numRows);
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 3
<strong>Output:</strong> "PAHNAPLSIIGYIR"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 4
<strong>Output:</strong> "PINALSIGYAHRPI"
<strong>Explanation:</strong>
P I N
A L S I G
Y A H R
P I
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "A", numRows = 1
<strong>Output:</strong> "A"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists of English letters (lower-case and upper-case), <code>','</code> and <code>'.'</code>.</li>
<li><code>1 <= numRows <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string convert(string s, int numRows) {\n if (numRows == 1)\n return s;\n string res = \"\";\n int cycle = numRows + numRows - 2;\n\n for (int i = 0; i < s.size(); i += cycle) {\n res += s[i];\n }\n\n for (int row = 1; row < numRows - 1; ++row) {\n for (int i = 0; i < s.size(); i += cycle) {\n try {\n res += s.at(i + row);\n res += s.at(i + cycle - row);\n } catch (...) {\n break;\n }\n }\n }\n \n for (int i = numRows - 1; i < s.size(); i += cycle) {\n res += s[i];\n }\n\n return res;\n }\n};",
"memory": "25793"
} |
6 | <p>The string <code>"PAYPALISHIRING"</code> is written in 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>
<pre>
P A H N
A P L S I I G
Y I R
</pre>
<p>And then read line by line: <code>"PAHNAPLSIIGYIR"</code></p>
<p>Write the code that will take a string and make this conversion given a number of rows:</p>
<pre>
string convert(string s, int numRows);
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 3
<strong>Output:</strong> "PAHNAPLSIIGYIR"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 4
<strong>Output:</strong> "PINALSIGYAHRPI"
<strong>Explanation:</strong>
P I N
A L S I G
Y A H R
P I
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "A", numRows = 1
<strong>Output:</strong> "A"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists of English letters (lower-case and upper-case), <code>','</code> and <code>'.'</code>.</li>
<li><code>1 <= numRows <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string convert(string s, int numRows) {\n string arr[numRows];\n for(int i=0;i<numRows;i++){\n arr[i]=\"\";\n }\n int i=0;\n while(i<s.length()){\n for(int j=0;j<numRows && i<s.length();j++){\n arr[j]=arr[j]+s[i++];\n }\n for(int k=numRows-2;k>0 && i<s.length();k--){\n arr[k]=arr[k]+s[i++];\n }\n }\n string ans;\n for(int i=0;i<numRows;i++){\n ans=ans+arr[i];\n }\n return ans;\n }\n};",
"memory": "25793"
} |
6 | <p>The string <code>"PAYPALISHIRING"</code> is written in 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>
<pre>
P A H N
A P L S I I G
Y I R
</pre>
<p>And then read line by line: <code>"PAHNAPLSIIGYIR"</code></p>
<p>Write the code that will take a string and make this conversion given a number of rows:</p>
<pre>
string convert(string s, int numRows);
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 3
<strong>Output:</strong> "PAHNAPLSIIGYIR"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 4
<strong>Output:</strong> "PINALSIGYAHRPI"
<strong>Explanation:</strong>
P I N
A L S I G
Y A H R
P I
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "A", numRows = 1
<strong>Output:</strong> "A"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists of English letters (lower-case and upper-case), <code>','</code> and <code>'.'</code>.</li>
<li><code>1 <= numRows <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string convert(string s, int numRows) {\n if (numRows == 1)\n return s;\n vector<char> vchar;\n int cycle = numRows + numRows - 2;\n\n for (int i = 0; i < s.size(); i += cycle) {\n vchar.push_back(s[i]);\n }\n\n for (int row = 1; row < numRows - 1; ++row) {\n for (int i = 0; i < s.size(); i += cycle) {\n try {\n vchar.push_back(s.at(i + row));\n vchar.push_back(s.at(i + cycle - row));\n } catch (...) {\n break;\n }\n }\n }\n \n for (int i = numRows - 1; i < s.size(); i += cycle) {\n vchar.push_back(s[i]);\n }\n\n\n string res(vchar.begin(), vchar.end());\n\n return res;\n }\n};",
"memory": "26206"
} |
6 | <p>The string <code>"PAYPALISHIRING"</code> is written in 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>
<pre>
P A H N
A P L S I I G
Y I R
</pre>
<p>And then read line by line: <code>"PAHNAPLSIIGYIR"</code></p>
<p>Write the code that will take a string and make this conversion given a number of rows:</p>
<pre>
string convert(string s, int numRows);
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 3
<strong>Output:</strong> "PAHNAPLSIIGYIR"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 4
<strong>Output:</strong> "PINALSIGYAHRPI"
<strong>Explanation:</strong>
P I N
A L S I G
Y A H R
P I
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "A", numRows = 1
<strong>Output:</strong> "A"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists of English letters (lower-case and upper-case), <code>','</code> and <code>'.'</code>.</li>
<li><code>1 <= numRows <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string convert(string s, int numRows) {\n if (numRows == 1){\n return s;\n }\n vector<vector<string>> table;\n int cur = 0;\n int zig = -1;\n int zcount = 0;\n string atom;\n for(int i = 0; i < numRows; i++){\n table.push_back(vector<string>());\n }\n while(cur < s.length()){\n if(zcount <= 0 || zcount >= numRows-1){\n zig = -zig;\n }\n atom = s[cur];\n table[zcount].push_back(atom);\n zcount = zcount+zig;\n cur++;\n } \n string result;\n for(const auto& line: table){\n for(const auto& str: line){\n result += str;\n }\n }\n return result;\n }\n };",
"memory": "26206"
} |
6 | <p>The string <code>"PAYPALISHIRING"</code> is written in 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>
<pre>
P A H N
A P L S I I G
Y I R
</pre>
<p>And then read line by line: <code>"PAHNAPLSIIGYIR"</code></p>
<p>Write the code that will take a string and make this conversion given a number of rows:</p>
<pre>
string convert(string s, int numRows);
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 3
<strong>Output:</strong> "PAHNAPLSIIGYIR"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 4
<strong>Output:</strong> "PINALSIGYAHRPI"
<strong>Explanation:</strong>
P I N
A L S I G
Y A H R
P I
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "A", numRows = 1
<strong>Output:</strong> "A"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists of English letters (lower-case and upper-case), <code>','</code> and <code>'.'</code>.</li>
<li><code>1 <= numRows <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string convert(string s, int numRows) {\n if(numRows <= 1){\n return s;\n }\n\n unordered_set<int> visited;\n queue<int> toVisit;\n\n int gap = (numRows - 1) * 2;\n\n for(int i = 0; i < s.length() + (gap / 2); i += gap){\n toVisit.push(i);\n }\n\n string result = \"\";\n\n while(!toVisit.empty()){\n int curr = toVisit.front();\n toVisit.pop();\n\n // cout << curr << ' ';\n\n if(visited.find(curr) != visited.end() || curr < 0 || curr > s.length() + (gap / 2)){\n continue;\n }\n\n if(curr >= 0 && curr <= s.length() - 1){\n result += s[curr];\n }\n \n visited.insert(curr);\n\n toVisit.push(curr - 1);\n toVisit.push(curr + 1);\n }\n\n return result;\n }\n};",
"memory": "26618"
} |
6 | <p>The string <code>"PAYPALISHIRING"</code> is written in 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>
<pre>
P A H N
A P L S I I G
Y I R
</pre>
<p>And then read line by line: <code>"PAHNAPLSIIGYIR"</code></p>
<p>Write the code that will take a string and make this conversion given a number of rows:</p>
<pre>
string convert(string s, int numRows);
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 3
<strong>Output:</strong> "PAHNAPLSIIGYIR"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 4
<strong>Output:</strong> "PINALSIGYAHRPI"
<strong>Explanation:</strong>
P I N
A L S I G
Y A H R
P I
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "A", numRows = 1
<strong>Output:</strong> "A"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists of English letters (lower-case and upper-case), <code>','</code> and <code>'.'</code>.</li>
<li><code>1 <= numRows <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string convert(string s, int n) {\n if (n == 1) {\n return s;\n }\n\n // first, last row = n - 2 = 2\n // 2nd = n - 3 = 1, n - 2 - (n - 3) - 1 = 0\n // 3nd = \n\n // 1 = 3, 0 = n - 1, n - (n - 1) - 1\n // 2 = 2, 1 = prev - 1, n - (n - 2) - 1\n // 3 = 1, 2 = prev - 1, n - (n - 3) - 1\n // 4 = 0, 3\n\n ostringstream oss;\n int g1 = n - 1;\n int g2 = 0;\n\n for (int i = 0; i < n; i++) {\n bool useG1 = true;\n\n for (int j = i; j < s.size();) {\n oss << s[j];\n\n if (useG1 && g1 == 0) useG1 = false;\n else if (!useG1 && g2 == 0) useG1 = true;\n\n if (useG1) {\n j += g1 * 2;\n } else {\n j += g2 * 2;\n }\n\n useG1 = !useG1;\n }\n\ncout << oss.str() << endl;\n\n g1 -= 1;\n g2 += 1;\n }\n\n return oss.str();\n }\n};",
"memory": "26618"
} |
6 | <p>The string <code>"PAYPALISHIRING"</code> is written in 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>
<pre>
P A H N
A P L S I I G
Y I R
</pre>
<p>And then read line by line: <code>"PAHNAPLSIIGYIR"</code></p>
<p>Write the code that will take a string and make this conversion given a number of rows:</p>
<pre>
string convert(string s, int numRows);
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 3
<strong>Output:</strong> "PAHNAPLSIIGYIR"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 4
<strong>Output:</strong> "PINALSIGYAHRPI"
<strong>Explanation:</strong>
P I N
A L S I G
Y A H R
P I
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "A", numRows = 1
<strong>Output:</strong> "A"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists of English letters (lower-case and upper-case), <code>','</code> and <code>'.'</code>.</li>
<li><code>1 <= numRows <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string convert(string s, int numRows) {\n map<int,vector<char>> mp;\n int n=s.length();\n int i=0;\n while(i<n)\n {\n int c=0;\n while(i<n && c<numRows)\n {\n mp[c].push_back(s[i]);\n i++;\n c++;\n }\n c=numRows-2;\n while(i<n && c>0)\n {\n mp[c].push_back(s[i]);\n i++;\n c--;\n }\n }\n string ans=\"\";\n for(auto it : mp)\n {\n vector<char> v= it.second;\n for(auto ch: v)\n ans+=ch;\n }\n return ans;\n }\n};",
"memory": "27031"
} |
6 | <p>The string <code>"PAYPALISHIRING"</code> is written in 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>
<pre>
P A H N
A P L S I I G
Y I R
</pre>
<p>And then read line by line: <code>"PAHNAPLSIIGYIR"</code></p>
<p>Write the code that will take a string and make this conversion given a number of rows:</p>
<pre>
string convert(string s, int numRows);
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 3
<strong>Output:</strong> "PAHNAPLSIIGYIR"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 4
<strong>Output:</strong> "PINALSIGYAHRPI"
<strong>Explanation:</strong>
P I N
A L S I G
Y A H R
P I
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "A", numRows = 1
<strong>Output:</strong> "A"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists of English letters (lower-case and upper-case), <code>','</code> and <code>'.'</code>.</li>
<li><code>1 <= numRows <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string convert(string s, int numRows) {\n string ret=\"\";\n unordered_map<int, vector<int>> rowToIndex;\n bool down=true;\n int count=0;\n for(int i=0;i<s.length();i++){\n string s2=s.substr(i,1);\n if(down){\n rowToIndex[count].push_back(s[i]);\n count++;\n }else{\n rowToIndex[count].push_back(s[i]);\n count--;\n }\n if(count==numRows){\n count--;\n count--;\n down=false;\n }\n if(count==-1){\n count++;\n count++;\n down=true;\n }\n }\n for(int i=0;i<s.length();i++){\n for(int j=0;j<rowToIndex[i].size();j++){\n ret+=rowToIndex[i][j];\n }\n }\n return ret;\n }\n};",
"memory": "27031"
} |
6 | <p>The string <code>"PAYPALISHIRING"</code> is written in 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>
<pre>
P A H N
A P L S I I G
Y I R
</pre>
<p>And then read line by line: <code>"PAHNAPLSIIGYIR"</code></p>
<p>Write the code that will take a string and make this conversion given a number of rows:</p>
<pre>
string convert(string s, int numRows);
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 3
<strong>Output:</strong> "PAHNAPLSIIGYIR"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 4
<strong>Output:</strong> "PINALSIGYAHRPI"
<strong>Explanation:</strong>
P I N
A L S I G
Y A H R
P I
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "A", numRows = 1
<strong>Output:</strong> "A"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists of English letters (lower-case and upper-case), <code>','</code> and <code>'.'</code>.</li>
<li><code>1 <= numRows <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string convert(string s, int numRows) {\n vector<string> v(numRows);\n int i=0;\n int row=0;\n bool dir=true;\n int n=s.size();\n string ans=\"\";\n if(numRows==1) return s;\n while(true){\n if(dir){\n while(row<numRows && i<n){\n v[row++].push_back(s[i++]);\n // row++;\n // i++;\n }\n row=row-2;\n }\n else{\n while(row>=0 && i<n){\n v[row--].push_back(s[i++]);\n // row--;\n // i++;\n }\n row=1;\n }\n if(i>=n) break;\n dir=!dir;\n }\n for(int i=0;i<v.size();i++){\n ans=ans+v[i];\n }\n return ans;\n }\n};",
"memory": "27443"
} |
6 | <p>The string <code>"PAYPALISHIRING"</code> is written in 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>
<pre>
P A H N
A P L S I I G
Y I R
</pre>
<p>And then read line by line: <code>"PAHNAPLSIIGYIR"</code></p>
<p>Write the code that will take a string and make this conversion given a number of rows:</p>
<pre>
string convert(string s, int numRows);
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 3
<strong>Output:</strong> "PAHNAPLSIIGYIR"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 4
<strong>Output:</strong> "PINALSIGYAHRPI"
<strong>Explanation:</strong>
P I N
A L S I G
Y A H R
P I
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "A", numRows = 1
<strong>Output:</strong> "A"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists of English letters (lower-case and upper-case), <code>','</code> and <code>'.'</code>.</li>
<li><code>1 <= numRows <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string convert(string s, int numRows) {\n if(numRows == 1)\n {\n return s;\n }\n vector<string> v(numRows);\n int i = 0;\n int n = s.length();\n int j = 0;\n while(j < n)\n {\n while(j < n && i < numRows)\n {\n v[i].push_back(s[j]);\n i++;\n j++;\n }\n i-=2;\n while(j < n && i >= 0)\n {\n v[i].push_back(s[j]);\n i--;\n j++;\n }\n i+=2;\n }\n string ans = \"\";\n for(int k = 0;k < numRows;k++)\n {\n ans = ans + v[k];\n }\n return ans;\n }\n};",
"memory": "27443"
} |
6 | <p>The string <code>"PAYPALISHIRING"</code> is written in 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>
<pre>
P A H N
A P L S I I G
Y I R
</pre>
<p>And then read line by line: <code>"PAHNAPLSIIGYIR"</code></p>
<p>Write the code that will take a string and make this conversion given a number of rows:</p>
<pre>
string convert(string s, int numRows);
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 3
<strong>Output:</strong> "PAHNAPLSIIGYIR"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 4
<strong>Output:</strong> "PINALSIGYAHRPI"
<strong>Explanation:</strong>
P I N
A L S I G
Y A H R
P I
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "A", numRows = 1
<strong>Output:</strong> "A"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists of English letters (lower-case and upper-case), <code>','</code> and <code>'.'</code>.</li>
<li><code>1 <= numRows <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string convert(string s, int numRows) {\n if(numRows == 1){\n return s;\n }\n vector<string>arr(numRows);\n int row = 0;\n int i = 0;\n bool direction = true;\n while(true){\n if(direction){\n while(row < numRows && i < s.size() ){\n arr[row].push_back(s[i]);\n row++;\n i++;\n }\n row = numRows -2;\n }\n else{\n while(row >= 0 && i < s.size() ){\n arr[row].push_back(s[i]);\n row--;\n i++;\n }\n row = 1;\n }\n if(i >= s.size()){\n break;\n }\n direction = !direction;\n\n\n }\n string ans;\n for(int i = 0;i<arr.size();i++){\n ans = ans + arr[i];\n }\n \n return ans;\n }\n};",
"memory": "27856"
} |
6 | <p>The string <code>"PAYPALISHIRING"</code> is written in 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>
<pre>
P A H N
A P L S I I G
Y I R
</pre>
<p>And then read line by line: <code>"PAHNAPLSIIGYIR"</code></p>
<p>Write the code that will take a string and make this conversion given a number of rows:</p>
<pre>
string convert(string s, int numRows);
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 3
<strong>Output:</strong> "PAHNAPLSIIGYIR"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 4
<strong>Output:</strong> "PINALSIGYAHRPI"
<strong>Explanation:</strong>
P I N
A L S I G
Y A H R
P I
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "A", numRows = 1
<strong>Output:</strong> "A"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists of English letters (lower-case and upper-case), <code>','</code> and <code>'.'</code>.</li>
<li><code>1 <= numRows <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string convert(string s, int numRows) \n {\n string ans;\n vector<string> temp(numRows,\"\");\n int j=0,i=0,size=s.size();\n while(j<size)\n {\n for(i=0;i<numRows;i++)\n {\n if(j>=size) break;\n //cout<<i<<s[j]<<\" \";\n temp[i]+=s[j];\n j++;\n }\n for(i=numRows-2;i>0;i--)\n {\n \n if(j>=size) break;\n //cout<<i<<s[j]<<\" \";\n temp[i]+=s[j];\n j++;\n }\n }\n //cout<<endl;\n for(i=0;i<numRows;i++)\n {\n //cout<<temp[i]<<endl;\n ans=ans+temp[i];\n }\n return ans;\n }\n};",
"memory": "27856"
} |
6 | <p>The string <code>"PAYPALISHIRING"</code> is written in 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>
<pre>
P A H N
A P L S I I G
Y I R
</pre>
<p>And then read line by line: <code>"PAHNAPLSIIGYIR"</code></p>
<p>Write the code that will take a string and make this conversion given a number of rows:</p>
<pre>
string convert(string s, int numRows);
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 3
<strong>Output:</strong> "PAHNAPLSIIGYIR"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 4
<strong>Output:</strong> "PINALSIGYAHRPI"
<strong>Explanation:</strong>
P I N
A L S I G
Y A H R
P I
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "A", numRows = 1
<strong>Output:</strong> "A"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists of English letters (lower-case and upper-case), <code>','</code> and <code>'.'</code>.</li>
<li><code>1 <= numRows <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string convert(string s, int numRows) {\n unordered_map<int,list<char>> mp;\n int i = 0; bool flag = true;\n for(auto ch: s){\n if(flag) mp[i++].push_back(ch); \n else mp[i--].push_back(ch);\n\n if(i == numRows-1 || i == 0) flag = !flag;\n }\n i = 0;\n string res = \"\";\n while(i != mp.size()){\n auto l = mp[i++];\n for(auto ch: l){\n res.push_back(ch);\n }\n }\n return res;\n }\n};",
"memory": "29918"
} |
6 | <p>The string <code>"PAYPALISHIRING"</code> is written in 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>
<pre>
P A H N
A P L S I I G
Y I R
</pre>
<p>And then read line by line: <code>"PAHNAPLSIIGYIR"</code></p>
<p>Write the code that will take a string and make this conversion given a number of rows:</p>
<pre>
string convert(string s, int numRows);
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 3
<strong>Output:</strong> "PAHNAPLSIIGYIR"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 4
<strong>Output:</strong> "PINALSIGYAHRPI"
<strong>Explanation:</strong>
P I N
A L S I G
Y A H R
P I
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "A", numRows = 1
<strong>Output:</strong> "A"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists of English letters (lower-case and upper-case), <code>','</code> and <code>'.'</code>.</li>
<li><code>1 <= numRows <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n void diagonal(string s, vector<vector<char>>& store, int &i, int &j, int& ind, int n){\n while(i >= 0 && ind < s.size()) store[i--][j++] = s[ind++];\n --j;\n i = 1;\n }\n void vertical(string s, vector<vector<char>>& store, int &i, int &j, int& ind, int rows, int n){\n while(i < rows - 1 && ind < s.size()) store[i++][j] = s[ind++];\n }\n string convert(string s, int rows) {\n int n = s.size(), ind = 0, i = 0, j = 0;\n if(rows == 1 || n == 1) return s;\n vector<vector<char>> store(rows + 1, vector<char>(n / 2 + 1, ' '));\n while(ind < n){\n vertical(s, store, i, j, ind, rows, n);\n diagonal(s, store, i, j, ind, n);\n }\n string ans = \"\";\n for(int i = 0; i < store.size(); i++){\n for(int j = 0; j < store[0].size(); j++){\n if(store[i][j] != ' ') ans.push_back(store[i][j]);\n }\n }\n return ans;\n }\n};",
"memory": "29918"
} |
6 | <p>The string <code>"PAYPALISHIRING"</code> is written in 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>
<pre>
P A H N
A P L S I I G
Y I R
</pre>
<p>And then read line by line: <code>"PAHNAPLSIIGYIR"</code></p>
<p>Write the code that will take a string and make this conversion given a number of rows:</p>
<pre>
string convert(string s, int numRows);
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 3
<strong>Output:</strong> "PAHNAPLSIIGYIR"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 4
<strong>Output:</strong> "PINALSIGYAHRPI"
<strong>Explanation:</strong>
P I N
A L S I G
Y A H R
P I
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "A", numRows = 1
<strong>Output:</strong> "A"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists of English letters (lower-case and upper-case), <code>','</code> and <code>'.'</code>.</li>
<li><code>1 <= numRows <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string convert(string s, int numRows) {\n if(numRows <= 1){\n return s;\n }\n\n unordered_set<int> visited;\n queue<int> toVisit;\n\n int gap = (numRows - 1) * 2;\n\n for(int i = 0; i < s.length() + gap; i += gap){\n toVisit.push(i);\n }\n\n string result = \"\";\n\n while(!toVisit.empty()){\n int curr = toVisit.front();\n toVisit.pop();\n\n // cout << curr << ' ';\n\n if(visited.find(curr) != visited.end() || curr < 0 || curr > s.length() + gap){\n continue;\n }\n\n if(curr >= 0 && curr <= s.length() - 1){\n result += s[curr];\n }\n \n visited.insert(curr);\n\n toVisit.push(curr - 1);\n toVisit.push(curr + 1);\n }\n\n return result;\n }\n};",
"memory": "30331"
} |
6 | <p>The string <code>"PAYPALISHIRING"</code> is written in 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>
<pre>
P A H N
A P L S I I G
Y I R
</pre>
<p>And then read line by line: <code>"PAHNAPLSIIGYIR"</code></p>
<p>Write the code that will take a string and make this conversion given a number of rows:</p>
<pre>
string convert(string s, int numRows);
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 3
<strong>Output:</strong> "PAHNAPLSIIGYIR"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 4
<strong>Output:</strong> "PINALSIGYAHRPI"
<strong>Explanation:</strong>
P I N
A L S I G
Y A H R
P I
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "A", numRows = 1
<strong>Output:</strong> "A"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists of English letters (lower-case and upper-case), <code>','</code> and <code>'.'</code>.</li>
<li><code>1 <= numRows <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string convert(string s, int numRows) {\n if(numRows <= 1) return s;\n string ans = \"\";\n \n vector<vector<char>> x(numRows);\n int i=0,k = 0;\n while(i<s.size()){\n while(k<numRows && i<s.size()){\n x[k].push_back(s[i]);\n i++;\n k++;\n }\n k -= (2);\n while(k>0 && i<s.size()){\n x[k].push_back(s[i]);\n k--;\n i++;\n }\n }\n for(int i=0;i<numRows;i++){\n ans = ans+string(x[i].begin(),x[i].end());\n }\n return ans;\n }\n};",
"memory": "30743"
} |
6 | <p>The string <code>"PAYPALISHIRING"</code> is written in 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>
<pre>
P A H N
A P L S I I G
Y I R
</pre>
<p>And then read line by line: <code>"PAHNAPLSIIGYIR"</code></p>
<p>Write the code that will take a string and make this conversion given a number of rows:</p>
<pre>
string convert(string s, int numRows);
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 3
<strong>Output:</strong> "PAHNAPLSIIGYIR"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 4
<strong>Output:</strong> "PINALSIGYAHRPI"
<strong>Explanation:</strong>
P I N
A L S I G
Y A H R
P I
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "A", numRows = 1
<strong>Output:</strong> "A"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists of English letters (lower-case and upper-case), <code>','</code> and <code>'.'</code>.</li>
<li><code>1 <= numRows <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string convert(string s, int numRows) {\n if(numRows == 1){\n return s;\n }\n\n vector<vector<char> >v(numRows);\n for (int i = 0; i < s.length();)\n {\n for (int j = 0; j < numRows-1 && i < s.length(); j++)\n {\n v[j].push_back(s[i]);\n ++i;\n }\n for (int k = numRows-1; k > 0 && i < s.length(); --k){\n \n v[k].push_back(s[i]);\n ++i;\n }\n \n \n }\n string ans = \"\";\n\n for (int i = 0; i < numRows; i++)\n {\n ans = ans + string(v[i].begin(), v[i].end());\n }\n return ans;\n }\n};",
"memory": "31156"
} |
6 | <p>The string <code>"PAYPALISHIRING"</code> is written in 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>
<pre>
P A H N
A P L S I I G
Y I R
</pre>
<p>And then read line by line: <code>"PAHNAPLSIIGYIR"</code></p>
<p>Write the code that will take a string and make this conversion given a number of rows:</p>
<pre>
string convert(string s, int numRows);
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 3
<strong>Output:</strong> "PAHNAPLSIIGYIR"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 4
<strong>Output:</strong> "PINALSIGYAHRPI"
<strong>Explanation:</strong>
P I N
A L S I G
Y A H R
P I
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "A", numRows = 1
<strong>Output:</strong> "A"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists of English letters (lower-case and upper-case), <code>','</code> and <code>'.'</code>.</li>
<li><code>1 <= numRows <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string convert(string s, int numRows) {\n string *ret = new string(s.length(),'a');\n if(numRows == 1 || numRows == s.length()){\n return s;\n }\n int colDis = numRows-2;\n int numCols = s.length()/(2*numRows-2)*(colDis+1)+s.length()%(2*numRows-2);\n int row = 0;\n int col = 0;\n int direction = 1;\n int count = 0;\n string mat(numCols*numRows,'$');\n for(int i=0;i<s.length();i++){\n mat[row*numCols+col] = s[i];\n row+=direction;\n if(direction < 1){\n col++;\n }\n if(row == numRows){\n direction*=-1;\n col++;\n row-=2;\n }\n if(row == 0){\n direction*=-1;\n }\n }\n for(int i=0;i<numCols*numRows;i++){\n if(mat[i]!= '$'){\n if(count < (*ret).length())\n (*ret)[count] = mat[i];\n count++; \n }\n }\n return (*ret);\n }\n};",
"memory": "31156"
} |
6 | <p>The string <code>"PAYPALISHIRING"</code> is written in 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>
<pre>
P A H N
A P L S I I G
Y I R
</pre>
<p>And then read line by line: <code>"PAHNAPLSIIGYIR"</code></p>
<p>Write the code that will take a string and make this conversion given a number of rows:</p>
<pre>
string convert(string s, int numRows);
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 3
<strong>Output:</strong> "PAHNAPLSIIGYIR"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 4
<strong>Output:</strong> "PINALSIGYAHRPI"
<strong>Explanation:</strong>
P I N
A L S I G
Y A H R
P I
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "A", numRows = 1
<strong>Output:</strong> "A"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists of English letters (lower-case and upper-case), <code>','</code> and <code>'.'</code>.</li>
<li><code>1 <= numRows <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string convert(string s, int numRows) {\n if (numRows == 1) {\n return s;\n }\n\n std::vector<std::vector<char>> rows;\n rows.resize(numRows);\n\n int curr_row = 0;\n int curr_col = 0;\n std::string curr_dir = \"down\";\n \n\n for (char c : s) {\n rows.at(curr_row).push_back(c);\n if (curr_dir == (\"down\") && curr_row < numRows - 1) {\n curr_row ++;\n } else if (curr_dir == (\"down\") && curr_row == numRows - 1) {\n curr_row -= 1;\n curr_col ++;\n curr_dir = \"up\";\n } else if (curr_dir == (\"up\") && curr_row > 0) {\n curr_row --;\n curr_col ++;\n } else if (curr_dir == (\"up\") && curr_row == 0) {\n curr_row ++;\n curr_dir = \"down\";\n }\n }\n\n for (std::vector<char> r : rows) {\n r.resize(curr_col);\n }\n\n std::string final_str;\n \n for (std::vector<char> r : rows) {\n for (char s : r) {\n final_str += s;\n }\n }\n return final_str;\n }\n};",
"memory": "31568"
} |
6 | <p>The string <code>"PAYPALISHIRING"</code> is written in 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>
<pre>
P A H N
A P L S I I G
Y I R
</pre>
<p>And then read line by line: <code>"PAHNAPLSIIGYIR"</code></p>
<p>Write the code that will take a string and make this conversion given a number of rows:</p>
<pre>
string convert(string s, int numRows);
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 3
<strong>Output:</strong> "PAHNAPLSIIGYIR"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 4
<strong>Output:</strong> "PINALSIGYAHRPI"
<strong>Explanation:</strong>
P I N
A L S I G
Y A H R
P I
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "A", numRows = 1
<strong>Output:</strong> "A"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists of English letters (lower-case and upper-case), <code>','</code> and <code>'.'</code>.</li>
<li><code>1 <= numRows <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string convert(string s, int numRows) {\n vector<vector<char>> a(numRows);\n string solution;\n bool GoingUp = 0;\n int colHeight = 0;\n int rowHeight = -1;\n\n if(numRows == 1){\n return s;\n }\n\n for (auto i = 0; i < numRows; i++) {\n a[i] = std::vector<char>(); // Assign a new empty vector to a[i]\n }\n\n for(int i = 0; i < s.length(); i++){\n cout<<\"i\" << i<<endl;\n if(rowHeight == 0){\n GoingUp = 0;\n }\n else if(rowHeight == numRows - 1){\n GoingUp = 1;\n }\n cout<<GoingUp<<endl;\n if(GoingUp == 0){\n rowHeight = rowHeight + 1;\n a[rowHeight].push_back(s[i]);\n }\n else{\n colHeight = colHeight + 1;\n rowHeight = rowHeight - 1;\n a[rowHeight].push_back(s[i]);\n }\n }\n\n for(int i = 0; i < a.size(); i++){\n string temp(a[i].begin(), a[i].end());\n solution = solution + temp;\n }\n\n return solution;\n }\n};",
"memory": "31568"
} |
6 | <p>The string <code>"PAYPALISHIRING"</code> is written in 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>
<pre>
P A H N
A P L S I I G
Y I R
</pre>
<p>And then read line by line: <code>"PAHNAPLSIIGYIR"</code></p>
<p>Write the code that will take a string and make this conversion given a number of rows:</p>
<pre>
string convert(string s, int numRows);
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 3
<strong>Output:</strong> "PAHNAPLSIIGYIR"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 4
<strong>Output:</strong> "PINALSIGYAHRPI"
<strong>Explanation:</strong>
P I N
A L S I G
Y A H R
P I
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "A", numRows = 1
<strong>Output:</strong> "A"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists of English letters (lower-case and upper-case), <code>','</code> and <code>'.'</code>.</li>
<li><code>1 <= numRows <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string convert(string s, int numRows) {\n\n if( numRows == 1 )\n {\n return s;\n }\n\n vector<string> rows;\n for( int j = 0; j < numRows; j++ )\n {\n rows.push_back( \"\" );\n }\n\n int activeRow = 0;\n int activeRowDelta = 1;\n for( int i = 0; i < s.size(); i++ )\n {\n rows[activeRow].push_back( s[i] );\n activeRow += activeRowDelta;\n if( activeRow < 0 )\n {\n activeRow = 0;\n activeRowDelta *= -1;\n activeRow += activeRowDelta;\n }\n else if( activeRow >= numRows )\n {\n activeRow = numRows - 1;\n activeRowDelta *= -1;\n activeRow += activeRowDelta;\n }\n }\n string outStr = \"\";\n for( int j = 0; j < numRows; j++ )\n {\n outStr = outStr + rows[j];\n }\n return outStr;\n }\n};",
"memory": "31981"
} |
6 | <p>The string <code>"PAYPALISHIRING"</code> is written in 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>
<pre>
P A H N
A P L S I I G
Y I R
</pre>
<p>And then read line by line: <code>"PAHNAPLSIIGYIR"</code></p>
<p>Write the code that will take a string and make this conversion given a number of rows:</p>
<pre>
string convert(string s, int numRows);
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 3
<strong>Output:</strong> "PAHNAPLSIIGYIR"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 4
<strong>Output:</strong> "PINALSIGYAHRPI"
<strong>Explanation:</strong>
P I N
A L S I G
Y A H R
P I
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "A", numRows = 1
<strong>Output:</strong> "A"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists of English letters (lower-case and upper-case), <code>','</code> and <code>'.'</code>.</li>
<li><code>1 <= numRows <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string convert(string s, int intRows) {\n int n=s.length();\n if(intRows==1)\n return s;\n int cols=0;\n while(n>0){\n n=n-intRows;\n cols++;\n if(n>0){\n n=n-(intRows-2);\n cols+=(intRows-2);\n }\n }\n n=s.length();\n int col=0;\n char** arr = new char*[intRows]; \nfor (int i = 0; i < intRows; i++) {\n arr[i] = new char[cols]; \n}\nfor(int i=0;i<intRows;i++)\n for(int j=0;j<cols;j++)\n arr[i][j]=' ';\nint k=0;\n while(k<n){\n for(int i=0;i<intRows&& k<n;i++){\n arr[i][col]=s[k++];\n }\n col++;\n for(int i=intRows-2;i>0&&k<n;i--){\n arr[i][col++]=s[k++];\n }\n }\n string ans=\"\";\n for(int i=0;i<intRows;i++)\n for(int j=0;j<cols;j++)\n if(arr[i][j]!=' ')\n ans+=arr[i][j];\n return ans;\n }\n};",
"memory": "31981"
} |
6 | <p>The string <code>"PAYPALISHIRING"</code> is written in 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>
<pre>
P A H N
A P L S I I G
Y I R
</pre>
<p>And then read line by line: <code>"PAHNAPLSIIGYIR"</code></p>
<p>Write the code that will take a string and make this conversion given a number of rows:</p>
<pre>
string convert(string s, int numRows);
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 3
<strong>Output:</strong> "PAHNAPLSIIGYIR"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 4
<strong>Output:</strong> "PINALSIGYAHRPI"
<strong>Explanation:</strong>
P I N
A L S I G
Y A H R
P I
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "A", numRows = 1
<strong>Output:</strong> "A"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists of English letters (lower-case and upper-case), <code>','</code> and <code>'.'</code>.</li>
<li><code>1 <= numRows <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string convert(string s, int numRows) {\n vector<vector<char>> result;\n int count = 0;\n int i = 0;\n for(; i < s.size(); ) {\n vector<char> cols(numRows,' ');\n \n if(count == 0 || count == numRows-1) {\n for(int j = numRows-1; j >= 0 && i < s.size(); j--) {\n cols[j] = s[i++];\n }\n count = 1;\n } else {\n if(i < s.size()){ \n cols[count] = s[i++];\n }\n count += 1;\n }\n result.push_back(cols);\n \n count = count%numRows;\n }\n if(result.size() == 0) {\n return \"\";\n } else {\n string resultStr = \"\";\n for(int j = result[0].size()-1; j >= 0; j--) {\n for(int i = 0; i < result.size(); i++) {\n if(result[i][j] != ' ') {\n resultStr.push_back(result[i][j]);\n }\n }\n }\n return resultStr;\n }\n }\n};",
"memory": "32393"
} |
6 | <p>The string <code>"PAYPALISHIRING"</code> is written in 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>
<pre>
P A H N
A P L S I I G
Y I R
</pre>
<p>And then read line by line: <code>"PAHNAPLSIIGYIR"</code></p>
<p>Write the code that will take a string and make this conversion given a number of rows:</p>
<pre>
string convert(string s, int numRows);
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 3
<strong>Output:</strong> "PAHNAPLSIIGYIR"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 4
<strong>Output:</strong> "PINALSIGYAHRPI"
<strong>Explanation:</strong>
P I N
A L S I G
Y A H R
P I
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "A", numRows = 1
<strong>Output:</strong> "A"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists of English letters (lower-case and upper-case), <code>','</code> and <code>'.'</code>.</li>
<li><code>1 <= numRows <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string convert(string s, int numRows) {\n if (numRows==1) return s;\n vector<string> res;\n for (int i=0;i<numRows;i++){\n res.push_back(\"\");\n }\n int down = 1; int row =0;\n res[row] = res[row]+s[0];row++;\n for (int i=1; i<s.length(); i++){\n \n res[row]=res[row]+s[i];\n if (row+1==numRows || row == 0){\n down = !down;\n }\n if (down) row++; \n else row--;\n \n }\n for (int i=1; i<numRows; i++){\n res[0]=res[0]+res[i];\n }\n return res[0];\n }\n};",
"memory": "32393"
} |
6 | <p>The string <code>"PAYPALISHIRING"</code> is written in 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>
<pre>
P A H N
A P L S I I G
Y I R
</pre>
<p>And then read line by line: <code>"PAHNAPLSIIGYIR"</code></p>
<p>Write the code that will take a string and make this conversion given a number of rows:</p>
<pre>
string convert(string s, int numRows);
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 3
<strong>Output:</strong> "PAHNAPLSIIGYIR"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 4
<strong>Output:</strong> "PINALSIGYAHRPI"
<strong>Explanation:</strong>
P I N
A L S I G
Y A H R
P I
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "A", numRows = 1
<strong>Output:</strong> "A"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists of English letters (lower-case and upper-case), <code>','</code> and <code>'.'</code>.</li>
<li><code>1 <= numRows <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string convert(string s, int numRows) {\n if(numRows == 1) return s;\n vector<vector<string>> row;\n for(int i = 0; i < numRows; i++) {\n vector<string> vec;\n row.push_back(vec);\n }\n int counter = 0;\n int isadd = 1;\n for(int i = 0; i < s.length(); i++) {\n if(isadd == 1) {\n if(counter == numRows - 1) {\n row[counter].push_back(s.substr(i, 1));\n counter --;\n\n isadd = 0;\n }\n else {\n row[counter].push_back(s.substr(i, 1));\n counter++;\n }\n }\n else {\n \n if(counter == 0) {\n row[counter].push_back(s.substr(i, 1));\n counter++;\n isadd = 1;\n }\n else {\n row[counter].push_back(s.substr(i, 1));\n counter --;\n }\n }\n }\n string ret = \"\";\n for(auto i : row) {\n for(auto j : i) {\n ret += j;\n }\n }\n \n return ret;\n }\n};",
"memory": "32806"
} |
6 | <p>The string <code>"PAYPALISHIRING"</code> is written in 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>
<pre>
P A H N
A P L S I I G
Y I R
</pre>
<p>And then read line by line: <code>"PAHNAPLSIIGYIR"</code></p>
<p>Write the code that will take a string and make this conversion given a number of rows:</p>
<pre>
string convert(string s, int numRows);
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 3
<strong>Output:</strong> "PAHNAPLSIIGYIR"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 4
<strong>Output:</strong> "PINALSIGYAHRPI"
<strong>Explanation:</strong>
P I N
A L S I G
Y A H R
P I
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "A", numRows = 1
<strong>Output:</strong> "A"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists of English letters (lower-case and upper-case), <code>','</code> and <code>'.'</code>.</li>
<li><code>1 <= numRows <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string convert(string s, int numRows) {\n if(numRows == 1) return s;\n vector<vector<string>> row;\n for(int i = 0; i < numRows; i++) {\n vector<string> vec;\n row.push_back(vec);\n }\n int counter = 0;\n int isadd = 1;\n for(int i = 0; i < s.length(); i++) {\n if(isadd == 1) {\n if(counter == numRows - 1) {\n row[counter].push_back(s.substr(i, 1));\n counter --;\n\n isadd = 0;\n }\n else {\n row[counter].push_back(s.substr(i, 1));\n counter++;\n }\n }\n else {\n \n if(counter == 0) {\n row[counter].push_back(s.substr(i, 1));\n counter++;\n isadd = 1;\n }\n else {\n row[counter].push_back(s.substr(i, 1));\n counter --;\n }\n }\n }\n string ret = \"\";\n for(auto i : row) {\n for(auto j : i) {\n ret += j;\n }\n }\n \n return ret;\n }\n};",
"memory": "32806"
} |
6 | <p>The string <code>"PAYPALISHIRING"</code> is written in 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>
<pre>
P A H N
A P L S I I G
Y I R
</pre>
<p>And then read line by line: <code>"PAHNAPLSIIGYIR"</code></p>
<p>Write the code that will take a string and make this conversion given a number of rows:</p>
<pre>
string convert(string s, int numRows);
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 3
<strong>Output:</strong> "PAHNAPLSIIGYIR"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 4
<strong>Output:</strong> "PINALSIGYAHRPI"
<strong>Explanation:</strong>
P I N
A L S I G
Y A H R
P I
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "A", numRows = 1
<strong>Output:</strong> "A"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists of English letters (lower-case and upper-case), <code>','</code> and <code>'.'</code>.</li>
<li><code>1 <= numRows <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string convert(string s, int numRows) {\n int n = s.size();\n vector<vector<char>> mat;\n mat.push_back(vector<char>{});\n for (int i = 0; i < n; i++) {\n if (mat.back().size()==numRows) { \n int new_col_count = numRows - 2;\n for ( int j = 0; j < new_col_count; ++j ) {\n vector<char> new_column(numRows, ' ');\n new_column[numRows-2-j] = s[i];\n mat.push_back(new_column);\n i++;\n if ( i == n) {\n break;\n }\n }\n \n if ( i < n) {\n mat.push_back(vector<char>{});\n }\n }\n mat.back().push_back(s[i]);\n }\n\n string ret;\n \n for ( int i = 0; i < numRows; ++i ) {\n for ( int j = 0; j < mat.size(); j++ ){\n if ( i < mat[j].size() && mat[j][i]!=' ')\n ret.push_back(mat[j][i]);\n }\n }\n\n return ret;\n\n }\n};",
"memory": "33218"
} |
6 | <p>The string <code>"PAYPALISHIRING"</code> is written in 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>
<pre>
P A H N
A P L S I I G
Y I R
</pre>
<p>And then read line by line: <code>"PAHNAPLSIIGYIR"</code></p>
<p>Write the code that will take a string and make this conversion given a number of rows:</p>
<pre>
string convert(string s, int numRows);
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 3
<strong>Output:</strong> "PAHNAPLSIIGYIR"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 4
<strong>Output:</strong> "PINALSIGYAHRPI"
<strong>Explanation:</strong>
P I N
A L S I G
Y A H R
P I
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "A", numRows = 1
<strong>Output:</strong> "A"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists of English letters (lower-case and upper-case), <code>','</code> and <code>'.'</code>.</li>
<li><code>1 <= numRows <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string convert(string s, int numRows) {\n\n if (numRows == 1) {\n return s;\n }\n\n vector<string> strcols;\n\n int cnt = 0, row = 0, col = 0;\n bool flag = true;\n string sub;\n for (size_t idx = 0; idx < s.size(); idx++) {\n\n if (flag) {\n sub += s[idx];\n if (row == numRows - 1 || idx == s.size() - 1) {\n col ++;\n cout << sub << endl;\n strcols.push_back(sub);\n\n flag = false;\n row--;\n if (row == 0) {\n flag = true;\n sub.clear();\n }\n }\n else {\n row++;\n }\n }\n else {\n sub = string(numRows, ' ');\n sub[row] = s[idx];\n cout << sub << endl;\n strcols.push_back(sub);\n\n row--;\n col++;\n\n if (row <= 0) {\n row = 0;\n flag = true;\n sub.clear();\n }\n }\n }\n\n string output;\n for (int row = 0; row < numRows; row++) {\n for (int col = 0; col < strcols.size(); col++) {\n if (row < strcols[col].size()) {\n if (strcols[col][row] != ' ') {\n output += strcols[col][row];\n }\n }\n }\n }\n\n return output;\n }\n};",
"memory": "33218"
} |
6 | <p>The string <code>"PAYPALISHIRING"</code> is written in 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>
<pre>
P A H N
A P L S I I G
Y I R
</pre>
<p>And then read line by line: <code>"PAHNAPLSIIGYIR"</code></p>
<p>Write the code that will take a string and make this conversion given a number of rows:</p>
<pre>
string convert(string s, int numRows);
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 3
<strong>Output:</strong> "PAHNAPLSIIGYIR"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 4
<strong>Output:</strong> "PINALSIGYAHRPI"
<strong>Explanation:</strong>
P I N
A L S I G
Y A H R
P I
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "A", numRows = 1
<strong>Output:</strong> "A"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists of English letters (lower-case and upper-case), <code>','</code> and <code>'.'</code>.</li>
<li><code>1 <= numRows <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string convert(string s, int r) {\n map<int,string> m;\n bool flag=true;\n int t=0;\n for(int i=0;i<s.size();i++){\n m[t]+=s[i];\n if(flag) t++;\n else t--;\n if(t==r-1) flag=false;\n if(t==0) flag=true;\n }\n string ans;\n for(auto x:m)ans=ans+x.second;\n return ans;\n }\n};",
"memory": "33631"
} |
6 | <p>The string <code>"PAYPALISHIRING"</code> is written in 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>
<pre>
P A H N
A P L S I I G
Y I R
</pre>
<p>And then read line by line: <code>"PAHNAPLSIIGYIR"</code></p>
<p>Write the code that will take a string and make this conversion given a number of rows:</p>
<pre>
string convert(string s, int numRows);
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 3
<strong>Output:</strong> "PAHNAPLSIIGYIR"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 4
<strong>Output:</strong> "PINALSIGYAHRPI"
<strong>Explanation:</strong>
P I N
A L S I G
Y A H R
P I
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "A", numRows = 1
<strong>Output:</strong> "A"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists of English letters (lower-case and upper-case), <code>','</code> and <code>'.'</code>.</li>
<li><code>1 <= numRows <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string convert(string s, int numRows) {\n map<int,string> mpp;\n int row = 1;\n bool flag = true;\n for(int i=0; i<s.size(); i++){\n if(flag == true){\n mpp[row].push_back(s[i]);\n row++;\n }\n else if(flag == false){\n mpp[row].push_back(s[i]);\n row--;\n }\n if(i!=0 && row==1){\n flag = true;\n }\n if(row == numRows){\n flag = false;\n }\n }\n string ans = \"\";\n for(auto x:mpp){\n ans = ans + x.second;\n }\n return ans;\n }\n};",
"memory": "33631"
} |
6 | <p>The string <code>"PAYPALISHIRING"</code> is written in 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>
<pre>
P A H N
A P L S I I G
Y I R
</pre>
<p>And then read line by line: <code>"PAHNAPLSIIGYIR"</code></p>
<p>Write the code that will take a string and make this conversion given a number of rows:</p>
<pre>
string convert(string s, int numRows);
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 3
<strong>Output:</strong> "PAHNAPLSIIGYIR"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 4
<strong>Output:</strong> "PINALSIGYAHRPI"
<strong>Explanation:</strong>
P I N
A L S I G
Y A H R
P I
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "A", numRows = 1
<strong>Output:</strong> "A"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists of English letters (lower-case and upper-case), <code>','</code> and <code>'.'</code>.</li>
<li><code>1 <= numRows <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string convert(string s, int numRows) {\n const size_t N(s.size());\n if (numRows == 1)\n return s;\n int cols(0), idx(0);\n bool isCross(false);\n while (idx < N) {\n if (isCross) {\n idx += (numRows - 2);\n cols += (numRows - 2);\n isCross = !isCross;\n } else {\n idx += numRows;\n cols += 1;\n isCross = !isCross;\n }\n }\n\n vector<vector<char>> result(numRows, vector<char>(cols, 0));\n idx = 0;\n int row = 0, col = 0;\n isCross = false;\n while (idx < N) {\n result[row][col] = s[idx++];\n if (!isCross) {\n if (row == numRows - 1) {\n isCross = true;\n row = row - 1;\n col = col + 1;\n } else {\n row = row + 1;\n }\n } else {\n if (row == 0) {\n isCross = false;\n row = row + 1;\n } else {\n row = row - 1;\n col = col + 1;\n }\n }\n }\n string zigzag;\n for (int i = 0; i < numRows; i++) {\n for (int j = 0; j < cols; j++) {\n if (result[i][j] != 0) {\n zigzag += (result[i][j]);\n }\n }\n }\n return zigzag;\n }\n};",
"memory": "34043"
} |
6 | <p>The string <code>"PAYPALISHIRING"</code> is written in 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>
<pre>
P A H N
A P L S I I G
Y I R
</pre>
<p>And then read line by line: <code>"PAHNAPLSIIGYIR"</code></p>
<p>Write the code that will take a string and make this conversion given a number of rows:</p>
<pre>
string convert(string s, int numRows);
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 3
<strong>Output:</strong> "PAHNAPLSIIGYIR"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 4
<strong>Output:</strong> "PINALSIGYAHRPI"
<strong>Explanation:</strong>
P I N
A L S I G
Y A H R
P I
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "A", numRows = 1
<strong>Output:</strong> "A"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists of English letters (lower-case and upper-case), <code>','</code> and <code>'.'</code>.</li>
<li><code>1 <= numRows <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string convert(string s, int numRows) {\n if(numRows==1) return(s);\n int n=s.size();\n string ans=\"\";\n map<int,string> mp;\n for (int i = 0; i < numRows; ++i) {\n mp[i] =\"\";\n }\n bool flag=true;\n int t=0;\n for(int i=0;i<n;i++)\n {\n if(flag)\n {\n //cout<<type(mp[t])<<endl;\n mp[t]=mp[t]+string(1, s[i]);\n t++;\n if(t>numRows-1) {\n flag=false;\n t=t-2;\n if(t<0) t=0;\n }\n }\n else{\n mp[t]=mp[t]+string(1, s[i]);\n t--;\n if(t==0)\n {\n flag=true;\n }\n else if(t<0)\n {\n t=1;\n flag=true;\n }\n\n }\n //cout<<t<<\"$\"<<endl;\n }\n for (auto i : mp)\n {\n ans=ans+i.second;\n cout << i.first << \" \\t\\t\\t \" << i.second << endl;\n }\n //cout << i.first << \" \\t\\t\\t \" << i.second << endl;\n return(ans);\n\n \n }\n};",
"memory": "34043"
} |
6 | <p>The string <code>"PAYPALISHIRING"</code> is written in 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>
<pre>
P A H N
A P L S I I G
Y I R
</pre>
<p>And then read line by line: <code>"PAHNAPLSIIGYIR"</code></p>
<p>Write the code that will take a string and make this conversion given a number of rows:</p>
<pre>
string convert(string s, int numRows);
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 3
<strong>Output:</strong> "PAHNAPLSIIGYIR"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 4
<strong>Output:</strong> "PINALSIGYAHRPI"
<strong>Explanation:</strong>
P I N
A L S I G
Y A H R
P I
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "A", numRows = 1
<strong>Output:</strong> "A"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists of English letters (lower-case and upper-case), <code>','</code> and <code>'.'</code>.</li>
<li><code>1 <= numRows <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\nint noofclm(string s, int numRows) {\n if (numRows == 1) return s.size();\n int n = s.size();\n int clm = 0;\n bool flag = true;\n while (n > 0) {\n if (flag) {\n clm++;\n n -= numRows;\n flag = false;\n } else {\n clm += (numRows - 2);\n n -= (numRows - 2);\n flag = true;\n }\n }\n return clm;\n }\n \n string convert(string s, int numRows) {\n if (numRows == 1) {\n return s;\n }\n int clm = noofclm(s, numRows);\n vector<vector<char>> zigzag(numRows, vector<char>(clm, ' ')); \n int i = 0;\n int col = 0;\n while (i < s.size()) {\n for (int row = 0; row < numRows && i < s.size(); row++) {\n zigzag[row][col] = s[i++];\n }\n for (int row = numRows - 2; row >= 1 && i < s.size(); row--) {\n zigzag[row][++col] = s[i++];\n }\n col++;\n }\n \n string str = \"\";\n for (int i = 0; i < numRows; i++) {\n for (int j = 0; j < clm; j++) {\n if (zigzag[i][j] != ' ') {\n str += zigzag[i][j];\n }\n }\n }\n return str;\n }\n};",
"memory": "34456"
} |
6 | <p>The string <code>"PAYPALISHIRING"</code> is written in 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>
<pre>
P A H N
A P L S I I G
Y I R
</pre>
<p>And then read line by line: <code>"PAHNAPLSIIGYIR"</code></p>
<p>Write the code that will take a string and make this conversion given a number of rows:</p>
<pre>
string convert(string s, int numRows);
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 3
<strong>Output:</strong> "PAHNAPLSIIGYIR"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 4
<strong>Output:</strong> "PINALSIGYAHRPI"
<strong>Explanation:</strong>
P I N
A L S I G
Y A H R
P I
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "A", numRows = 1
<strong>Output:</strong> "A"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists of English letters (lower-case and upper-case), <code>','</code> and <code>'.'</code>.</li>
<li><code>1 <= numRows <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string convert(string s, int numRows) {\n if(numRows == 1) return s; // Handle edge case\n\n int n = s.size();\n int cycleLen = 2 * numRows - 2; // Calculate the length of a full cycle (zigzag)\n\n // Estimate the number of columns needed\n int numCols = (n / cycleLen) * (numRows - 1) + min(numRows, n % cycleLen);\n vector<vector<char>> mat(numRows, vector<char>(numCols, '0'));\n\n int row = 0, col = 0;\n\n for (int i = 0; i < n; i++) {\n mat[row][col] = s[i];\n\n if (row == 0) {\n row++; // Start moving down\n } else if (row == numRows - 1) {\n row--; // Start moving up\n col++; // Move to the next column while going up\n } else {\n if (col % (numRows - 1) == 0) {\n row++; // Continue moving down\n } else {\n row--;\n col++; // Move up diagonally\n }\n }\n }\n\n // Construct the final result by reading the matrix row by row\n string ans = \"\";\n for (int i = 0; i < numRows; i++) {\n for (int j = 0; j < numCols; j++) {\n if (mat[i][j] != '0') {\n ans += mat[i][j];\n }\n }\n }\n\n return ans;\n }\n};\n",
"memory": "34456"
} |
6 | <p>The string <code>"PAYPALISHIRING"</code> is written in 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>
<pre>
P A H N
A P L S I I G
Y I R
</pre>
<p>And then read line by line: <code>"PAHNAPLSIIGYIR"</code></p>
<p>Write the code that will take a string and make this conversion given a number of rows:</p>
<pre>
string convert(string s, int numRows);
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 3
<strong>Output:</strong> "PAHNAPLSIIGYIR"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 4
<strong>Output:</strong> "PINALSIGYAHRPI"
<strong>Explanation:</strong>
P I N
A L S I G
Y A H R
P I
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "A", numRows = 1
<strong>Output:</strong> "A"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists of English letters (lower-case and upper-case), <code>','</code> and <code>'.'</code>.</li>
<li><code>1 <= numRows <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string convert(string s, int numRows) {\n if(numRows == 1 || s.size() <= numRows) return s;\n\n // Calculate Columns\n int zigzagNums = (s.size() / (2*numRows-2))+1;\n int numCols = zigzagNums*(numRows-1);\n vector<vector<char>> zigzag = vector<vector<char>>(numRows, vector<char>(numCols, 0));\n\n // cout << zigzagNums << endl;\n // cout << numCols << endl;\n\n bool goingDown = true;\n int row = 0; int col = 0;\n for(int i = 0; i < s.size(); i++) {\n // cout << row << \", \" << col << endl;\n zigzag[row][col] = s[i];\n if(goingDown) {\n // Stop going down\n if(row == numRows-1) {\n goingDown = false;\n row--; col++;\n }\n // Going down\n else {\n row++;\n }\n } else {\n // Stop going up\n if(row == 0) {\n goingDown = true;\n row++;\n } \n // Going up\n else {\n row--; col++;\n }\n }\n }\n\n int s_Index = 0;\n for(int i = 0; i < numRows; i++) {\n for(int j = 0; j < numCols; j++) {\n if(zigzag[i][j] != 0){\n s[s_Index] = zigzag[i][j];\n s_Index++;\n } \n }\n }\n return s;\n }\n};",
"memory": "34868"
} |
6 | <p>The string <code>"PAYPALISHIRING"</code> is written in 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>
<pre>
P A H N
A P L S I I G
Y I R
</pre>
<p>And then read line by line: <code>"PAHNAPLSIIGYIR"</code></p>
<p>Write the code that will take a string and make this conversion given a number of rows:</p>
<pre>
string convert(string s, int numRows);
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 3
<strong>Output:</strong> "PAHNAPLSIIGYIR"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 4
<strong>Output:</strong> "PINALSIGYAHRPI"
<strong>Explanation:</strong>
P I N
A L S I G
Y A H R
P I
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "A", numRows = 1
<strong>Output:</strong> "A"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists of English letters (lower-case and upper-case), <code>','</code> and <code>'.'</code>.</li>
<li><code>1 <= numRows <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string convert(string s, int numRows) {\n if(numRows == 1 || s.size() <= numRows) return s;\n\n // Calculate Columns\n int zigzagNums = (s.size() / (2*numRows-2))+1;\n int numCols = zigzagNums*(numRows-1);\n vector<vector<char>> zigzag = vector<vector<char>>(numRows, vector<char>(numCols, 0));\n\n // cout << zigzagNums << endl;\n // cout << numCols << endl;\n\n bool goingDown = true;\n int row = 0; int col = 0;\n for(int i = 0; i < s.size(); i++) {\n // cout << row << \", \" << col << endl;\n zigzag[row][col] = s[i];\n if(goingDown) {\n // Stop going down\n if(row == numRows-1) {\n goingDown = false;\n row--; col++;\n }\n // Going down\n else {\n row++;\n }\n } else {\n // Stop going up\n if(row == 0) {\n goingDown = true;\n row++;\n } \n // Going up\n else {\n row--; col++;\n }\n }\n }\n\n int s_Index = 0;\n for(int i = 0; i < numRows; i++) {\n for(int j = 0; j < numCols; j++) {\n if(zigzag[i][j] != 0){\n s[s_Index] = zigzag[i][j];\n s_Index++;\n } \n }\n }\n return s;\n }\n};",
"memory": "35281"
} |
6 | <p>The string <code>"PAYPALISHIRING"</code> is written in 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>
<pre>
P A H N
A P L S I I G
Y I R
</pre>
<p>And then read line by line: <code>"PAHNAPLSIIGYIR"</code></p>
<p>Write the code that will take a string and make this conversion given a number of rows:</p>
<pre>
string convert(string s, int numRows);
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 3
<strong>Output:</strong> "PAHNAPLSIIGYIR"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 4
<strong>Output:</strong> "PINALSIGYAHRPI"
<strong>Explanation:</strong>
P I N
A L S I G
Y A H R
P I
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "A", numRows = 1
<strong>Output:</strong> "A"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists of English letters (lower-case and upper-case), <code>','</code> and <code>'.'</code>.</li>
<li><code>1 <= numRows <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string convert(string s, int numRows) {\n std::string result_str = \"\";\n std::unordered_map<std::string,char> ch_row_column; \n int cur_row = 0, cur_column = 0; // row and column of the current character\n\n if (numRows==1||s.size()==1)\n return s;\n else if (numRows>=s.size())\n return s;\n\n for (int i = 0; i<s.size(); i++){\n if (cur_column%(numRows-1)==0){ // odd column\n ch_row_column.insert({std::to_string(cur_row++)+','+std::to_string(cur_column),s[i]});\n if (cur_row%numRows==0 && cur_row!=0){\n cur_column++;\n if (cur_column%(numRows-1)==0)\n cur_row = 0;\n else\n cur_row--;\n }\n }\n else{ // even column\n ch_row_column.insert({std::to_string(--cur_row)+','+std::to_string(cur_column),s[i]});\n cur_column++;\n if (cur_column%(numRows-1)==0 && cur_column!=0)\n cur_row--;\n } \n }\n\n int max_column = cur_column; // record the maximum column number\n\n cur_column = 0;\n cur_row = 0;\n\n for (int i = 0; i<s.size(); i++){\n if (ch_row_column[{std::to_string(cur_row)+','+std::to_string(cur_column)}]=='\\0'){\n cur_column = 0;\n cur_row++;\n i--;\n continue;\n }\n result_str += ch_row_column[{std::to_string(cur_row)+','+std::to_string(cur_column)}];\n\n if (cur_column<max_column && cur_column%(numRows-1)==0 && cur_row!=(numRows-1)) \n cur_column = cur_column + (numRows - 1 - cur_row);\n else if (cur_column<max_column)\n cur_column = cur_column + cur_row;\n else {\n cur_column = 0;\n cur_row++;\n }\n }\n\n return result_str;\n }\n};",
"memory": "35693"
} |
6 | <p>The string <code>"PAYPALISHIRING"</code> is written in 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>
<pre>
P A H N
A P L S I I G
Y I R
</pre>
<p>And then read line by line: <code>"PAHNAPLSIIGYIR"</code></p>
<p>Write the code that will take a string and make this conversion given a number of rows:</p>
<pre>
string convert(string s, int numRows);
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 3
<strong>Output:</strong> "PAHNAPLSIIGYIR"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 4
<strong>Output:</strong> "PINALSIGYAHRPI"
<strong>Explanation:</strong>
P I N
A L S I G
Y A H R
P I
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "A", numRows = 1
<strong>Output:</strong> "A"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists of English letters (lower-case and upper-case), <code>','</code> and <code>'.'</code>.</li>
<li><code>1 <= numRows <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string convert(string s, int numRows) {\n std::string result_str = \"\";\n std::unordered_map<std::string,char> ch_row_column; \n int cur_row = 0, cur_column = 0; // row and column of the current character\n\n if (numRows==1||s.size()==1)\n return s;\n else if (numRows>=s.size())\n return s;\n\n for (int i = 0; i<s.size(); i++){\n if (cur_column%(numRows-1)==0){ // odd column\n ch_row_column.insert({std::to_string(cur_row++)+','+std::to_string(cur_column),s[i]});\n if (cur_row%numRows==0 && cur_row!=0){\n cur_column++;\n if (cur_column%(numRows-1)==0)\n cur_row = 0;\n else\n cur_row--;\n }\n }\n else{ // even column\n ch_row_column.insert({std::to_string(--cur_row)+','+std::to_string(cur_column),s[i]});\n cur_column++;\n if (cur_column%(numRows-1)==0 && cur_column!=0)\n cur_row--;\n } \n }\n\n int max_column = cur_column; // record the maximum column number\n\n cur_column = 0;\n cur_row = 0;\n\n for (int i = 0; i<s.size(); i++){\n if (ch_row_column[{std::to_string(cur_row)+','+std::to_string(cur_column)}]=='\\0'){\n cur_column = 0;\n cur_row++;\n i--;\n continue;\n }\n result_str += ch_row_column[{std::to_string(cur_row)+','+std::to_string(cur_column)}];\n\n if (cur_column<max_column && cur_column%(numRows-1)==0 && cur_row!=(numRows-1)) \n cur_column = cur_column + (numRows - 1 - cur_row);\n else if (cur_column<max_column)\n cur_column = cur_column + cur_row;\n else {\n cur_column = 0;\n cur_row++;\n }\n }\n\n return result_str;\n }\n};",
"memory": "35693"
} |
6 | <p>The string <code>"PAYPALISHIRING"</code> is written in 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>
<pre>
P A H N
A P L S I I G
Y I R
</pre>
<p>And then read line by line: <code>"PAHNAPLSIIGYIR"</code></p>
<p>Write the code that will take a string and make this conversion given a number of rows:</p>
<pre>
string convert(string s, int numRows);
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 3
<strong>Output:</strong> "PAHNAPLSIIGYIR"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 4
<strong>Output:</strong> "PINALSIGYAHRPI"
<strong>Explanation:</strong>
P I N
A L S I G
Y A H R
P I
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "A", numRows = 1
<strong>Output:</strong> "A"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists of English letters (lower-case and upper-case), <code>','</code> and <code>'.'</code>.</li>
<li><code>1 <= numRows <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string convert(string s, int numRows) {\n if (numRows == 1) {\n return s;\n }\n\n int sections = ceil(s.length() / (2 * numRows - 2.0));\n int numCols = sections * (numRows - 1);\n\n vector<vector<char>> matrix(numRows, vector<char>(numCols, string::npos));\n int x = 0;\n int y = 0;\n int i = 0;\n while (i < s.length()) {\n while (y < numRows && i < s.length()) {\n matrix[y][x] = s[i];\n i++;\n y++;\n }\n\n y -= 2;\n x++;\n\n while (y > 0 && i < s.length()) {\n matrix[y][x] = s[i];\n y--;\n x++;\n i++;\n }\n }\n\n string zigzagged;\n for (auto& row : matrix) {\n for (auto& c : row) {\n if (c != string::npos) {\n zigzagged += c;\n }\n }\n }\n\n return zigzagged;\n }\n};",
"memory": "36106"
} |
6 | <p>The string <code>"PAYPALISHIRING"</code> is written in 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>
<pre>
P A H N
A P L S I I G
Y I R
</pre>
<p>And then read line by line: <code>"PAHNAPLSIIGYIR"</code></p>
<p>Write the code that will take a string and make this conversion given a number of rows:</p>
<pre>
string convert(string s, int numRows);
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 3
<strong>Output:</strong> "PAHNAPLSIIGYIR"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 4
<strong>Output:</strong> "PINALSIGYAHRPI"
<strong>Explanation:</strong>
P I N
A L S I G
Y A H R
P I
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "A", numRows = 1
<strong>Output:</strong> "A"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists of English letters (lower-case and upper-case), <code>','</code> and <code>'.'</code>.</li>
<li><code>1 <= numRows <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string convert(string s, int numRows) {\n if (numRows == 1) {\n return s;\n }\n\n int n = int(s.size());\n int sections = ceil(n / (2 * numRows - 2.0));\n int numCols = sections * (numRows - 1);\n\n vector<vector<char>> matrix(numRows, vector<char>(numCols, ' '));\n\n int currRow = 0, currCol = 0;\n int currStringIndex = 0;\n\n // Iterate in zig-zag pattern on matrix and fill it with string\n // characters.\n while (currStringIndex < n) {\n // Move down.\n while (currRow < numRows && currStringIndex < n) {\n matrix[currRow][currCol] = s[currStringIndex];\n currRow++;\n currStringIndex++;\n }\n\n currRow -= 2;\n currCol++;\n\n // Move up (with moving right also).\n while (currRow > 0 && currCol < numCols && currStringIndex < n) {\n matrix[currRow][currCol] = s[currStringIndex];\n currRow--;\n currCol++;\n currStringIndex++;\n }\n }\n\n string answer;\n for (auto& row : matrix) {\n for (auto& character : row) {\n if (character != ' ') {\n answer += character;\n }\n }\n }\n\n return answer;\n }\n};",
"memory": "36106"
} |
6 | <p>The string <code>"PAYPALISHIRING"</code> is written in 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>
<pre>
P A H N
A P L S I I G
Y I R
</pre>
<p>And then read line by line: <code>"PAHNAPLSIIGYIR"</code></p>
<p>Write the code that will take a string and make this conversion given a number of rows:</p>
<pre>
string convert(string s, int numRows);
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 3
<strong>Output:</strong> "PAHNAPLSIIGYIR"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 4
<strong>Output:</strong> "PINALSIGYAHRPI"
<strong>Explanation:</strong>
P I N
A L S I G
Y A H R
P I
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "A", numRows = 1
<strong>Output:</strong> "A"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists of English letters (lower-case and upper-case), <code>','</code> and <code>'.'</code>.</li>
<li><code>1 <= numRows <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\nstring convert(string s, int numRows) {\n if (numRows == 1) return s;\n\n // 1. Compute the size of 2D matrix\n int n = s.length();\n // each section has at most 2*rows-2 chars\n int sections = ceil(n / (2.0*numRows - 2));\n cout << \"sections = \" << sections << endl;\n // each section has at most rows-1 cols\n int numCols = sections * (numRows - 1);\n\n // 2. Construct 2D \n vector<vector<char>> matrix(numRows, vector<char>(numCols, ' '));\n int row = 0, col = 0;\n int idx = 0;\n\n // 3. Iterate zig-zag pattern on matrix\n while (idx < n) {\n // Move down\n while (row < numRows && idx < n) {\n matrix[row++][col] = s[idx++];\n }\n // At this point, row is at rows (1 more over index)\n row -= 2; // row will need to be 2 higher than\n col += 1; // col will need to shift by 1\n\n // Move up\n while (row > 0 /*&& col < numCols*/ && idx < n) {\n matrix[row--][col++] = s[idx++];\n }\n }\n\n // Construct the answer:\n string answer = \"\";\n for (auto& row : matrix) {\n for (auto& c : row) {\n if (c != ' ')\n answer += c;\n }\n }\n\n return answer;\n}\n};",
"memory": "36518"
} |
6 | <p>The string <code>"PAYPALISHIRING"</code> is written in 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>
<pre>
P A H N
A P L S I I G
Y I R
</pre>
<p>And then read line by line: <code>"PAHNAPLSIIGYIR"</code></p>
<p>Write the code that will take a string and make this conversion given a number of rows:</p>
<pre>
string convert(string s, int numRows);
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 3
<strong>Output:</strong> "PAHNAPLSIIGYIR"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 4
<strong>Output:</strong> "PINALSIGYAHRPI"
<strong>Explanation:</strong>
P I N
A L S I G
Y A H R
P I
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "A", numRows = 1
<strong>Output:</strong> "A"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists of English letters (lower-case and upper-case), <code>','</code> and <code>'.'</code>.</li>
<li><code>1 <= numRows <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string convert(string s, int num) {\n if (num == 1) return s;\n int n = s.length();\n int it = 0;\n int col = (n / (2 * num - 2) + 1) * (num - 1);\n int move = num - 2;\n int c = 0;\n bool ans = true;\n\n vector<vector<char>> v(num, vector<char>(col, '#'));\n\n while (c != col) {\n // Fill downwards\n for (int j = 0; j < num; j++) {\n if (it >= n) { // Corrected condition\n ans = false;\n break;\n }\n if (ans == false) break;\n v[j][c] = s[it];\n it++;\n }\n c++;\n\n // Fill diagonally upwards\n while (move != 0) {\n if (it >= n) { // Corrected condition\n ans = false;\n break;\n }\n if (ans == false) break;\n v[move][c] = s[it];\n it++;\n c++;\n move--;\n }\n\n move = num - 2; // Reset move after diagonal movement\n }\n\n string final = \"\";\n for (int i = 0; i < num; i++) {\n for (int j = 0; j < col; j++) {\n if (v[i][j] != '#') {\n final += v[i][j];\n }\n }\n }\n\n return final;\n }\n};\n",
"memory": "36518"
} |
6 | <p>The string <code>"PAYPALISHIRING"</code> is written in 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>
<pre>
P A H N
A P L S I I G
Y I R
</pre>
<p>And then read line by line: <code>"PAHNAPLSIIGYIR"</code></p>
<p>Write the code that will take a string and make this conversion given a number of rows:</p>
<pre>
string convert(string s, int numRows);
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 3
<strong>Output:</strong> "PAHNAPLSIIGYIR"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 4
<strong>Output:</strong> "PINALSIGYAHRPI"
<strong>Explanation:</strong>
P I N
A L S I G
Y A H R
P I
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "A", numRows = 1
<strong>Output:</strong> "A"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists of English letters (lower-case and upper-case), <code>','</code> and <code>'.'</code>.</li>
<li><code>1 <= numRows <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution\n{\npublic:\n string convert(string s, int numRows)\n {\n vector<ostringstream> lines(numRows);\n int row = 0;\n bool directionIsDown = true;\n for (char &c : s)\n {\n \n#ifdef DEBUG\n if (row != 0 && !directionIsDown)\n {\n for (int i = 0; i < numRows; i++)\n {\n if (i != row)\n {\n lines[i] << ' ';\n }\n lines[i] << ' ';\n }\n }\n#endif\n lines[row] << c;\n\n if (directionIsDown)\n {\n if (row == numRows - 1)\n {\n directionIsDown = false;\n --row;\n }\n else\n {\n ++row;\n }\n }\n else\n {\n if (row == 0)\n {\n directionIsDown = true;\n ++row;\n }\n else\n {\n --row;\n }\n }\n row = row % numRows;\n }\n ostringstream output;\n for (ostringstream &line : lines)\n {\n string str = line.str();\n cout << str << std::endl;\n output << str;\n }\n return output.str();\n }\n};",
"memory": "36931"
} |
6 | <p>The string <code>"PAYPALISHIRING"</code> is written in 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>
<pre>
P A H N
A P L S I I G
Y I R
</pre>
<p>And then read line by line: <code>"PAHNAPLSIIGYIR"</code></p>
<p>Write the code that will take a string and make this conversion given a number of rows:</p>
<pre>
string convert(string s, int numRows);
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 3
<strong>Output:</strong> "PAHNAPLSIIGYIR"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 4
<strong>Output:</strong> "PINALSIGYAHRPI"
<strong>Explanation:</strong>
P I N
A L S I G
Y A H R
P I
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "A", numRows = 1
<strong>Output:</strong> "A"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists of English letters (lower-case and upper-case), <code>','</code> and <code>'.'</code>.</li>
<li><code>1 <= numRows <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string convert(string s, int numRows) {\n if (numRows <= 1) {\n return s;\n }\n \n vector<stringstream> rows(numRows);\n int i = 0;\n int dir = 1;\n for (char c : s) {\n rows[i] << c;\n if (i == numRows - 1) {\n dir = -1;\n }\n else if (i == 0) {\n dir = 1;\n }\n i += dir;\n }\n\n for (size_t i = 1; i < numRows; ++i) {\n rows[0] << rows[i].str();\n cout << rows[i].str() << '\\n';\n }\n return rows[0].str();\n }\n};",
"memory": "36931"
} |
6 | <p>The string <code>"PAYPALISHIRING"</code> is written in 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>
<pre>
P A H N
A P L S I I G
Y I R
</pre>
<p>And then read line by line: <code>"PAHNAPLSIIGYIR"</code></p>
<p>Write the code that will take a string and make this conversion given a number of rows:</p>
<pre>
string convert(string s, int numRows);
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 3
<strong>Output:</strong> "PAHNAPLSIIGYIR"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 4
<strong>Output:</strong> "PINALSIGYAHRPI"
<strong>Explanation:</strong>
P I N
A L S I G
Y A H R
P I
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "A", numRows = 1
<strong>Output:</strong> "A"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists of English letters (lower-case and upper-case), <code>','</code> and <code>'.'</code>.</li>
<li><code>1 <= numRows <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n void make_move(int &row, int &col, bool &isDiag, int numRows) {\n if (row == numRows - 1) {\n isDiag = 1;\n }\n if (row == 0){\n isDiag = 0;\n }\n if (isDiag == 0) {\n ++row;\n }\n else {\n --row;\n col++;\n }\n }\n\n\n string convert(string s, int numRows) {\n int numFullCols = s.size() / numRows + 1;\n int numCols = numFullCols + (numFullCols - 1) * (numRows - 2);\n vector<vector<char>> zigZagMatrix(numRows, vector<char>(numCols, 0));\n\n if (numRows == 1)\n return s;\n \n int len = s.size();\n int i = 0, j = 0;\n bool direction = 0;\n for (int k = 0; k < len; ++k) {\n zigZagMatrix[i][j] = s[k];\n make_move(i, j, direction, numRows);\n \n }\n\n string out = \"\";\n for (auto &vectCh : zigZagMatrix) {\n for (auto ch : vectCh )\n if (ch != 0) {\n out.push_back(ch);\n }\n\n }\n\n return out; \n }\n};",
"memory": "37343"
} |
6 | <p>The string <code>"PAYPALISHIRING"</code> is written in 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>
<pre>
P A H N
A P L S I I G
Y I R
</pre>
<p>And then read line by line: <code>"PAHNAPLSIIGYIR"</code></p>
<p>Write the code that will take a string and make this conversion given a number of rows:</p>
<pre>
string convert(string s, int numRows);
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 3
<strong>Output:</strong> "PAHNAPLSIIGYIR"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 4
<strong>Output:</strong> "PINALSIGYAHRPI"
<strong>Explanation:</strong>
P I N
A L S I G
Y A H R
P I
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "A", numRows = 1
<strong>Output:</strong> "A"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists of English letters (lower-case and upper-case), <code>','</code> and <code>'.'</code>.</li>
<li><code>1 <= numRows <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n void make_move(int &row, int &col, bool &isDiag, const int &numRows) {\n if (row == numRows - 1) {\n isDiag = 1;\n }\n if (row == 0){\n isDiag = 0;\n }\n if (isDiag == 0) {\n ++row;\n }\n else {\n --row;\n col++;\n }\n }\n\n\n string convert(string s, int numRows) {\n int numFullCols = s.size() / numRows + 1;\n int numCols = numFullCols + (numFullCols - 1) * (numRows - 2);\n vector<vector<char>> zigZagMatrix(numRows, vector<char>(numCols, 0));\n\n if (numRows == 1)\n return s;\n \n int len = s.size();\n int i = 0, j = 0;\n bool direction = 0;\n for (int k = 0; k < len; ++k) {\n zigZagMatrix[i][j] = s[k];\n make_move(i, j, direction, numRows);\n }\n\n string out = \"\";\n for (auto &vectCh : zigZagMatrix) {\n for (auto ch : vectCh )\n if (ch != 0) {\n out.push_back(ch);\n }\n\n }\n\n return out; \n }\n};",
"memory": "37343"
} |
6 | <p>The string <code>"PAYPALISHIRING"</code> is written in 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>
<pre>
P A H N
A P L S I I G
Y I R
</pre>
<p>And then read line by line: <code>"PAHNAPLSIIGYIR"</code></p>
<p>Write the code that will take a string and make this conversion given a number of rows:</p>
<pre>
string convert(string s, int numRows);
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 3
<strong>Output:</strong> "PAHNAPLSIIGYIR"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 4
<strong>Output:</strong> "PINALSIGYAHRPI"
<strong>Explanation:</strong>
P I N
A L S I G
Y A H R
P I
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "A", numRows = 1
<strong>Output:</strong> "A"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists of English letters (lower-case and upper-case), <code>','</code> and <code>'.'</code>.</li>
<li><code>1 <= numRows <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string convert(string s, int numRows) {\n stringstream zz;\n vector<stringstream> prev_row(numRows);\n int curr_row = 0;\n bool forward = true;\n\n if (numRows == 1) {\n return s;\n }\n\n for (int spos=0; spos<s.size(); spos++) {\n prev_row[curr_row] << s[spos];\n \n if (forward) {\n if (curr_row < numRows - 1) {\n curr_row++;\n }\n else {\n forward = false;\n curr_row--;\n }\n }\n else {\n if (curr_row > 0) {\n curr_row--;\n }\n else {\n forward = true;\n curr_row++;\n }\n }\n }\n\n for (auto& vec: prev_row) {\n zz << vec.str();\n }\n return zz.str();\n }\n};\n",
"memory": "37756"
} |
6 | <p>The string <code>"PAYPALISHIRING"</code> is written in 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>
<pre>
P A H N
A P L S I I G
Y I R
</pre>
<p>And then read line by line: <code>"PAHNAPLSIIGYIR"</code></p>
<p>Write the code that will take a string and make this conversion given a number of rows:</p>
<pre>
string convert(string s, int numRows);
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 3
<strong>Output:</strong> "PAHNAPLSIIGYIR"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 4
<strong>Output:</strong> "PINALSIGYAHRPI"
<strong>Explanation:</strong>
P I N
A L S I G
Y A H R
P I
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "A", numRows = 1
<strong>Output:</strong> "A"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists of English letters (lower-case and upper-case), <code>','</code> and <code>'.'</code>.</li>
<li><code>1 <= numRows <= 1000</code></li>
</ul>
| 3 | {
"code": "#include <string>\n#include <vector>\n\nclass Solution {\npublic:\n void create(string s,vector<vector<char>>& v, int row, int col, int index) {\n if (index >= s.size()) {\n return;\n }\n if (row == 0 && index < s.size()) {\n while (row < v.size()-1 && index < s.size()) { \n v[row][col] = s[index];\n row++;\n index++; \n }\n }\n if (row == v.size() - 1 && index < s.size()) { \n while (row > 0 && index < s.size()) { \n v[row][col] = s[index];\n row--;\n col++;\n index++;\n }\n }\n create(s, v, row, col, index); \n }\n\n std::string convert(std::string s, int numRows) {\n if (numRows == 1) return s; // Special case for single row\n string output;\n int numCols = (s.size() / (2 * numRows - 2) + 1) * (numRows - 1);\n vector<vector<char>> v(numRows,vector<char>(numCols, '\\0'));\n create(s, v, 0, 0, 0);\n for (int i = 0; i < numRows; i++) {\n for (int j = 0; j < numCols; j++) {\n if (v[i][j] != '\\0') {\n output.push_back(v[i][j]);\n }\n }\n }\n return output;\n }\n};\n",
"memory": "37756"
} |
6 | <p>The string <code>"PAYPALISHIRING"</code> is written in 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>
<pre>
P A H N
A P L S I I G
Y I R
</pre>
<p>And then read line by line: <code>"PAHNAPLSIIGYIR"</code></p>
<p>Write the code that will take a string and make this conversion given a number of rows:</p>
<pre>
string convert(string s, int numRows);
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 3
<strong>Output:</strong> "PAHNAPLSIIGYIR"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 4
<strong>Output:</strong> "PINALSIGYAHRPI"
<strong>Explanation:</strong>
P I N
A L S I G
Y A H R
P I
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "A", numRows = 1
<strong>Output:</strong> "A"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists of English letters (lower-case and upper-case), <code>','</code> and <code>'.'</code>.</li>
<li><code>1 <= numRows <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n const vector<vector<int>>moves = { {0,1},{1,-1} };\nstring convert(string s, int numRows) {\n if (numRows == 1) return s;\n if (numRows == 2) {\n string result = \"\";\n for (int i = 0; i < s.size(); i += 2) result += s[i];\n for (int i = 1; i < s.size(); i += 2) result += s[i];\n return result;\n }\n vector<vector<char>> map((numRows-1)*ceil(double(s.size()) / (2 * numRows - 2)), vector<char>(numRows, NULL));\n int count = 0;\n int x = 0;\n int y = 0;\n map[x][y] = s[count];\n count++;\n while (count < s.size()) {\n for (int i = 0; i < numRows-1; i++) {\n if (count == s.size()) break;\n x += moves[0][0];\n y += moves[0][1];\n map[x][y] = s[count];\n count++;\n }\n for (int i = 0; i < numRows-1; i++) {\n if (count == s.size()) break;\n x += moves[1][0];\n y += moves[1][1];\n map[x][y] = s[count];\n count++;\n }\n }\n string result = \"\";\n for (int i = 0; i < numRows; i++) {\n for (int j = 0; j < map.size(); j++) {\n if (map[j][i] == NULL) continue;\n result += map[j][i];\n }\n }\n return result;\n}\n};",
"memory": "38168"
} |
6 | <p>The string <code>"PAYPALISHIRING"</code> is written in 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>
<pre>
P A H N
A P L S I I G
Y I R
</pre>
<p>And then read line by line: <code>"PAHNAPLSIIGYIR"</code></p>
<p>Write the code that will take a string and make this conversion given a number of rows:</p>
<pre>
string convert(string s, int numRows);
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 3
<strong>Output:</strong> "PAHNAPLSIIGYIR"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 4
<strong>Output:</strong> "PINALSIGYAHRPI"
<strong>Explanation:</strong>
P I N
A L S I G
Y A H R
P I
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "A", numRows = 1
<strong>Output:</strong> "A"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists of English letters (lower-case and upper-case), <code>','</code> and <code>'.'</code>.</li>
<li><code>1 <= numRows <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string convert(string s, int numRows) {\n vector<vector<char>> result;\n \n for (int i = 0; i < s.size();){\n int j = 0;\n vector<char> temp;\n while (j++ < numRows){\n if (i < s.size())\n temp.push_back(s[i++]);\n else\n temp.push_back(' ');\n }\n result.push_back(temp);\n\n if (i >= s.size())\n break;\n\n j = numRows - 2;\n while (j > 0){\n temp.clear();\n for (int k = 0; k < j; k++){\n temp.push_back(' ');\n }\n if (i < s.size())\n temp.push_back(s[i++]);\n else\n temp.push_back(' ');\n\n for (int k = j + 1; k < numRows; k++){\n temp.push_back(' ');\n }\n result.push_back(temp);\n j--;\n }\n }\n\n int rows = result.size(), columns = numRows;\n string s_new = \"\";\n for (int j = 0; j < columns; j++){\n for (int i = 0; i < rows; i++){\n char ch = result[i][j];\n if (result[i][j] != ' '){\n s_new += ch;\n }\n }\n }\n cout << s_new << endl;\n return s_new;\n }\n};",
"memory": "38581"
} |
6 | <p>The string <code>"PAYPALISHIRING"</code> is written in 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>
<pre>
P A H N
A P L S I I G
Y I R
</pre>
<p>And then read line by line: <code>"PAHNAPLSIIGYIR"</code></p>
<p>Write the code that will take a string and make this conversion given a number of rows:</p>
<pre>
string convert(string s, int numRows);
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 3
<strong>Output:</strong> "PAHNAPLSIIGYIR"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 4
<strong>Output:</strong> "PINALSIGYAHRPI"
<strong>Explanation:</strong>
P I N
A L S I G
Y A H R
P I
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "A", numRows = 1
<strong>Output:</strong> "A"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists of English letters (lower-case and upper-case), <code>','</code> and <code>'.'</code>.</li>
<li><code>1 <= numRows <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string convert(string s, int numRows) {\n if(numRows == 1){\n return s;\n }\n char** m = new char*[numRows];\n for (int i = 0; i < numRows; i++) {\n m[i] = new char[s.length()];\n }\n for (int i = 0; i < numRows; i++) {\n for (int j = 0; j < s.length(); j++) {\n m[i][j] = ' ';\n }\n }\n int i = 0, j = 0, c = 0;\n while (c < s.length()) {\n for (i; i < numRows && c < s.length(); i++) {\n m[i][j] = s[c];\n c++;\n }\n i -= 2;\n j++;\n while (i >= 0 && j < s.length() && c < s.length()) {\n m[i][j] = s[c];\n c++;\n i--;\n j++;\n }\n j--;\n i = 1;\n }\n //showMatrix(m, numRows, s.length());\n c = 0;\n for (i = 0; i < numRows; i++) {\n for (j = 0; j < s.length(); j++) {\n if (m[i][j] == ' ') {\n continue;\n } else {\n s[c] = m[i][j];\n c++;\n }\n }\n }\n return s;\n }\n};",
"memory": "38993"
} |
6 | <p>The string <code>"PAYPALISHIRING"</code> is written in 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>
<pre>
P A H N
A P L S I I G
Y I R
</pre>
<p>And then read line by line: <code>"PAHNAPLSIIGYIR"</code></p>
<p>Write the code that will take a string and make this conversion given a number of rows:</p>
<pre>
string convert(string s, int numRows);
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 3
<strong>Output:</strong> "PAHNAPLSIIGYIR"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 4
<strong>Output:</strong> "PINALSIGYAHRPI"
<strong>Explanation:</strong>
P I N
A L S I G
Y A H R
P I
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "A", numRows = 1
<strong>Output:</strong> "A"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists of English letters (lower-case and upper-case), <code>','</code> and <code>'.'</code>.</li>
<li><code>1 <= numRows <= 1000</code></li>
</ul>
| 3 | {
"code": "struct ArrayPointer{\n int length;\n char *array; \n};\n\nclass Solution {\npublic:\n string convert(string s, int numRows) {\n if(numRows==1){return s;}\n int length=s.length();\n ArrayPointer A[numRows];\n for(int i=0;i<numRows;i++){\n A[i].length=0;\n char *temp=new char[length];\n A[i].array=temp;\n }\n bool rowincreasing=false;\n int storerow=0;\n for(int i=0;i<length;i++){\n (A[storerow].array)[A[storerow].length]=s[i];\n (A[storerow].length)++;\n if(storerow==0 || storerow==numRows-1){rowincreasing=!rowincreasing;}\n if(rowincreasing){storerow++;}\n else{storerow--;}\n\n }\n string returner=\"\";\n for(int i=0;i<numRows;i++){\n for(int j=0;j<A[i].length;j++){\n returner+=(A[i].array)[j];\n }\n //delete [](A[i].array);\n }\n return returner;\n }\n };",
"memory": "38993"
} |
6 | <p>The string <code>"PAYPALISHIRING"</code> is written in 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>
<pre>
P A H N
A P L S I I G
Y I R
</pre>
<p>And then read line by line: <code>"PAHNAPLSIIGYIR"</code></p>
<p>Write the code that will take a string and make this conversion given a number of rows:</p>
<pre>
string convert(string s, int numRows);
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 3
<strong>Output:</strong> "PAHNAPLSIIGYIR"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 4
<strong>Output:</strong> "PINALSIGYAHRPI"
<strong>Explanation:</strong>
P I N
A L S I G
Y A H R
P I
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "A", numRows = 1
<strong>Output:</strong> "A"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists of English letters (lower-case and upper-case), <code>','</code> and <code>'.'</code>.</li>
<li><code>1 <= numRows <= 1000</code></li>
</ul>
| 3 | {
"code": "struct ArrayPointer{\n int length;\n char *array; \n};\n\nclass Solution {\npublic:\n string convert(string s, int numRows) {\n if(numRows==1){return s;}\n int length=s.length();\n ArrayPointer A[numRows];\n for(int i=0;i<numRows;i++){\n A[i].length=0;\n char *temp=new char[length];\n A[i].array=temp;\n }\n bool rowincreasing=false;\n int storerow=0;\n for(int i=0;i<length;i++){\n (A[storerow].array)[A[storerow].length]=s[i];\n (A[storerow].length)++;\n if(storerow==0 || storerow==numRows-1){rowincreasing=!rowincreasing;}\n if(rowincreasing){storerow++;}\n else{storerow--;}\n\n }\n string returner=\"\";\n for(int i=0;i<numRows;i++){\n for(int j=0;j<A[i].length;j++){\n returner+=(A[i].array)[j];\n }\n }\n return returner;\n }\n };",
"memory": "39406"
} |
6 | <p>The string <code>"PAYPALISHIRING"</code> is written in 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>
<pre>
P A H N
A P L S I I G
Y I R
</pre>
<p>And then read line by line: <code>"PAHNAPLSIIGYIR"</code></p>
<p>Write the code that will take a string and make this conversion given a number of rows:</p>
<pre>
string convert(string s, int numRows);
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 3
<strong>Output:</strong> "PAHNAPLSIIGYIR"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 4
<strong>Output:</strong> "PINALSIGYAHRPI"
<strong>Explanation:</strong>
P I N
A L S I G
Y A H R
P I
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "A", numRows = 1
<strong>Output:</strong> "A"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists of English letters (lower-case and upper-case), <code>','</code> and <code>'.'</code>.</li>
<li><code>1 <= numRows <= 1000</code></li>
</ul>
| 3 | {
"code": "struct ArrayPointer{\n int length;\n char *array; \n};\n\nclass Solution {\npublic:\n string convert(string s, int numRows) {\n if(numRows==1){return s;}\n int length=s.length();\n ArrayPointer A[numRows];\n for(int i=0;i<numRows;i++){\n A[i].length=0;\n char *temp=new char[length];\n A[i].array=temp;\n }\n bool rowincreasing=false;\n int storerow=0;\n for(int i=0;i<length;i++){\n (A[storerow].array)[A[storerow].length]=s[i];\n (A[storerow].length)++;\n if(storerow==0 || storerow==numRows-1){rowincreasing=!rowincreasing;}\n if(rowincreasing){storerow++;}\n else{storerow--;}\n\n }\n string returner=\"\";\n for(int i=0;i<numRows;i++){\n for(int j=0;j<A[i].length;j++){\n returner+=(A[i].array)[j];\n }\n delete [](A[i].array);\n }\n return returner;\n }\n };",
"memory": "39406"
} |
6 | <p>The string <code>"PAYPALISHIRING"</code> is written in 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>
<pre>
P A H N
A P L S I I G
Y I R
</pre>
<p>And then read line by line: <code>"PAHNAPLSIIGYIR"</code></p>
<p>Write the code that will take a string and make this conversion given a number of rows:</p>
<pre>
string convert(string s, int numRows);
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 3
<strong>Output:</strong> "PAHNAPLSIIGYIR"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 4
<strong>Output:</strong> "PINALSIGYAHRPI"
<strong>Explanation:</strong>
P I N
A L S I G
Y A H R
P I
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "A", numRows = 1
<strong>Output:</strong> "A"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists of English letters (lower-case and upper-case), <code>','</code> and <code>'.'</code>.</li>
<li><code>1 <= numRows <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string convert(string s, int numRows) {\n if (numRows == 1) {\n return s;\n }\n bool r_growing = true; \n // TODO(caml): need ceil of double\n // int num_cols = std::ceil(s.size() / (numRows + numRows - 2)) * (numRows - 1);\n int num_cols = std::ceil(static_cast<double>(s.size()) / (numRows + numRows - 2)) * (numRows - 1);\n std::vector<std::vector<char>> matrix(/*size=*/numRows, \n /*val=*/std::vector<char>(/*size=*/num_cols, /*val=*/' ')); \n \n // std::cout << \"num_cols: \" << num_cols << std::endl;\n int r = 0; \n int c = 0; \n for (int i = 0; i < s.size(); ++i) {\n // std::cout << \"r: \" << r << \", c: \" << c << std::endl;\n matrix[r][c] = s[i]; \n if (r == numRows - 1) {\n r_growing = false; \n r -= 1; \n } else if (r == 0) {\n r_growing = true;\n r += 1; \n } else {\n r += (r_growing ? 1 : -1);\n }\n if (!r_growing) {\n c += 1; \n }\n }\n std::string out; \n out.reserve(matrix.size() * matrix[0].size()); \n for (const auto& row : matrix) {\n for (const char c : row) {\n if (c != ' ') {\n out.push_back(c); \n }\n \n }\n }\n return out; \n }\n};",
"memory": "39818"
} |
6 | <p>The string <code>"PAYPALISHIRING"</code> is written in 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>
<pre>
P A H N
A P L S I I G
Y I R
</pre>
<p>And then read line by line: <code>"PAHNAPLSIIGYIR"</code></p>
<p>Write the code that will take a string and make this conversion given a number of rows:</p>
<pre>
string convert(string s, int numRows);
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 3
<strong>Output:</strong> "PAHNAPLSIIGYIR"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 4
<strong>Output:</strong> "PINALSIGYAHRPI"
<strong>Explanation:</strong>
P I N
A L S I G
Y A H R
P I
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "A", numRows = 1
<strong>Output:</strong> "A"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists of English letters (lower-case and upper-case), <code>','</code> and <code>'.'</code>.</li>
<li><code>1 <= numRows <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n void insert_row(string& ans, string s, int jump, int interval){\n for(int i = interval; i < s.size(); i += jump){\n ans.push_back(s[i]);\n // Add middle element for rows between the first and last row\n if (interval > 0 && interval < jump - interval && i + jump - 2 * interval < s.size()) {\n ans.push_back(s[i + jump - 2 * interval]);\n }\n }\n }\n string convert(string s, int numRows) {\n if (numRows == 1) return s;\n int jump = 2 * numRows - 2; \n string ans = \"\";\n for(int i = 0; i < numRows; i++){\n insert_row(ans, s, jump, i);\n }\n return ans;\n }\n};",
"memory": "40231"
} |
6 | <p>The string <code>"PAYPALISHIRING"</code> is written in 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>
<pre>
P A H N
A P L S I I G
Y I R
</pre>
<p>And then read line by line: <code>"PAHNAPLSIIGYIR"</code></p>
<p>Write the code that will take a string and make this conversion given a number of rows:</p>
<pre>
string convert(string s, int numRows);
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 3
<strong>Output:</strong> "PAHNAPLSIIGYIR"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 4
<strong>Output:</strong> "PINALSIGYAHRPI"
<strong>Explanation:</strong>
P I N
A L S I G
Y A H R
P I
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "A", numRows = 1
<strong>Output:</strong> "A"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists of English letters (lower-case and upper-case), <code>','</code> and <code>'.'</code>.</li>
<li><code>1 <= numRows <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string convert(string s, int numRows) {\n char **mem=new char*[numRows];\n int *lens=new int[numRows]; \n for(int i=0;i<numRows;i++){\n mem[i]=new char[s.length()];\n lens[i]=0;\n }\n int row=0;\n int direction=-1;\n for(int i=0;i<s.length();i++){\n mem[row][lens[row]]=s[i];\n if(row==0 || row==numRows-1){\n direction*=-1;\n }\n lens[row]++;\n row+=(numRows!=1) *direction;\n }\n string res=\"\";\n int br=0;\n for(int i=0;i<numRows;i++){\n for(int j=0;j<lens[i];j++){\n res+=mem[i][j];\n br++;\n }\n }\n return res;\n }\n\n};",
"memory": "40643"
} |
6 | <p>The string <code>"PAYPALISHIRING"</code> is written in 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>
<pre>
P A H N
A P L S I I G
Y I R
</pre>
<p>And then read line by line: <code>"PAHNAPLSIIGYIR"</code></p>
<p>Write the code that will take a string and make this conversion given a number of rows:</p>
<pre>
string convert(string s, int numRows);
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 3
<strong>Output:</strong> "PAHNAPLSIIGYIR"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 4
<strong>Output:</strong> "PINALSIGYAHRPI"
<strong>Explanation:</strong>
P I N
A L S I G
Y A H R
P I
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "A", numRows = 1
<strong>Output:</strong> "A"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists of English letters (lower-case and upper-case), <code>','</code> and <code>'.'</code>.</li>
<li><code>1 <= numRows <= 1000</code></li>
</ul>
| 3 | {
"code": "struct rowGap {\n int first;\n int second;\n};\n\n/* 3:\n4, 0\n2, 2\n0, 4\n*/\n\n/* 4:\n6, 0\n4, 2\n2, 4\n0, 6\n*/\n\nclass Solution {\npublic:\n\n rowGap getRowGap(string s, int rowNum, int numRows) {\n if (numRows == 1) {\n return {.first = 0, .second = 1};\n } else {\n return {\n .first = (numRows - 1 - rowNum) * 2,\n .second = rowNum * 2\n };\n }\n }\n\n string convert(string s, int numRows) {\n string newS = \"\";\n for (int rowNum = 0; rowNum < numRows; rowNum++) {\n rowGap gap = getRowGap(s, rowNum, numRows);\n bool onFirst = true;\n bool changed = true;\n int charOn = rowNum;\n while (charOn < s.size()) {\n if (changed) {\n newS += s[charOn];\n }\n int gapSize = onFirst ? gap.first : gap.second;\n charOn += gapSize;\n changed = gapSize > 0;\n onFirst = !onFirst;\n }\n }\n return newS;\n }\n};",
"memory": "40643"
} |
6 | <p>The string <code>"PAYPALISHIRING"</code> is written in 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>
<pre>
P A H N
A P L S I I G
Y I R
</pre>
<p>And then read line by line: <code>"PAHNAPLSIIGYIR"</code></p>
<p>Write the code that will take a string and make this conversion given a number of rows:</p>
<pre>
string convert(string s, int numRows);
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 3
<strong>Output:</strong> "PAHNAPLSIIGYIR"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 4
<strong>Output:</strong> "PINALSIGYAHRPI"
<strong>Explanation:</strong>
P I N
A L S I G
Y A H R
P I
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "A", numRows = 1
<strong>Output:</strong> "A"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists of English letters (lower-case and upper-case), <code>','</code> and <code>'.'</code>.</li>
<li><code>1 <= numRows <= 1000</code></li>
</ul>
| 3 | {
"code": "#include <vector>\n#include <string>\n\nclass Solution {\npublic:\n string convert(string s, int numRows) {\n if (numRows == 1) return s;\n string reserve = s;\n vector<string> myset;\n while(s.length()){\n if(s.length()<numRows) {\n string temp = s;\n while (temp.length()<numRows) temp += \" \";\n myset.push_back(temp);\n s = \"\";\n } else {\n myset.push_back(s.substr(0,numRows));\n s = s.substr(numRows);\n }\n if(s.length())\n for(int i = numRows-2; i>0&& !s.empty(); i-- ){\n string t(numRows, ' ');\n t[i] = s[0];\n s = s.substr(1, s.length());\n myset.push_back(t);\n }\n }\n\n string worker = \"\";\n for(int i = 0; i<numRows; i++){\n for(int j=0; j<myset.size(); j++){\n if(worker.length()==reserve.length()) break;\n if((myset[j][i] != ' ')&&(myset[j][i]!='\\u0000')&&(myset[j][i]!='\\0')) {\n worker += myset[j][i]; \n }\n }\n }\n return worker;\n }\n};",
"memory": "41056"
} |
6 | <p>The string <code>"PAYPALISHIRING"</code> is written in 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>
<pre>
P A H N
A P L S I I G
Y I R
</pre>
<p>And then read line by line: <code>"PAHNAPLSIIGYIR"</code></p>
<p>Write the code that will take a string and make this conversion given a number of rows:</p>
<pre>
string convert(string s, int numRows);
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 3
<strong>Output:</strong> "PAHNAPLSIIGYIR"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 4
<strong>Output:</strong> "PINALSIGYAHRPI"
<strong>Explanation:</strong>
P I N
A L S I G
Y A H R
P I
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "A", numRows = 1
<strong>Output:</strong> "A"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists of English letters (lower-case and upper-case), <code>','</code> and <code>'.'</code>.</li>
<li><code>1 <= numRows <= 1000</code></li>
</ul>
| 3 | {
"code": "#include <iostream>\n#include <string>\nusing namespace std;\n\nclass Solution {\npublic:\n string convert(string s, int numRows) {\n // Edge case: if numRows is 1, return the string as no conversion is needed\n if (numRows == 1) return s;\n \n // Define 'f' as the length of the string 's'\n int f = s.length();\n \n // Dynamically allocate arrays\n char** arr = new char*[1]; // Only one row needed\n arr[0] = new char[f];\n char** sr = new char*[numRows]; // Zigzag pattern array\n for (int i = 0; i < numRows; ++i) {\n sr[i] = new char[f](); // Initialize all elements to 0\n }\n\n // Fill the single-row array\n int index = 0;\n for (int j = 0; j < f; j++) {\n arr[0][j] = s[index++];\n }\n\n // Zigzag conversion logic\n int j = 0, i = 0, m = 0;\n bool goingDown = false; // Direction flag\n \n for (int k = 0; k < f; ++k) {\n sr[i][j] = arr[0][k];\n \n // Change direction when reaching the top or bottom row\n if (i == 0 || i == numRows - 1) {\n goingDown = !goingDown;\n }\n \n // Move either down or up depending on the direction\n if (goingDown) {\n i++; // Move down the rows\n } else {\n i--; // Move up the rows\n j++; // Move horizontally right when going up\n }\n }\n\n // Read the zigzag pattern row by row to form the result\n string result;\n for (int i = 0; i < numRows; i++) {\n for (int j = 0; j < f; j++) {\n if (sr[i][j] != 0) { // Avoid appending empty cells\n result += sr[i][j];\n }\n }\n }\n\n // Deallocate memory\n delete[] arr[0];\n delete[] arr;\n for (int i = 0; i < numRows; ++i) {\n delete[] sr[i];\n }\n delete[] sr;\n\n return result;\n }\n};\n",
"memory": "41056"
} |
6 | <p>The string <code>"PAYPALISHIRING"</code> is written in 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>
<pre>
P A H N
A P L S I I G
Y I R
</pre>
<p>And then read line by line: <code>"PAHNAPLSIIGYIR"</code></p>
<p>Write the code that will take a string and make this conversion given a number of rows:</p>
<pre>
string convert(string s, int numRows);
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 3
<strong>Output:</strong> "PAHNAPLSIIGYIR"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 4
<strong>Output:</strong> "PINALSIGYAHRPI"
<strong>Explanation:</strong>
P I N
A L S I G
Y A H R
P I
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "A", numRows = 1
<strong>Output:</strong> "A"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists of English letters (lower-case and upper-case), <code>','</code> and <code>'.'</code>.</li>
<li><code>1 <= numRows <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string convert(string s, int numRows) {\n int i,j,k,g=2*(numRows-1),n=s.size();\n if (n==numRows || numRows==1) {\n return s;\n }\n string res;\n for (i=g,k=0;i>=0,k<numRows;i=i-2,k++) {\n int var=g-k*2,evenOdd;\n if (i==0) {\n var=g;\n }\n for(j=k,evenOdd=0;j<n;j=j+var,evenOdd++) {\n res=res+s[j];\n if (i!=g && i!=0) {\n var = evenOdd%2==0 ? g-k*2 : k*2;\n }\n }\n }\n return res;\n }\n};",
"memory": "41468"
} |
6 | <p>The string <code>"PAYPALISHIRING"</code> is written in 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>
<pre>
P A H N
A P L S I I G
Y I R
</pre>
<p>And then read line by line: <code>"PAHNAPLSIIGYIR"</code></p>
<p>Write the code that will take a string and make this conversion given a number of rows:</p>
<pre>
string convert(string s, int numRows);
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 3
<strong>Output:</strong> "PAHNAPLSIIGYIR"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 4
<strong>Output:</strong> "PINALSIGYAHRPI"
<strong>Explanation:</strong>
P I N
A L S I G
Y A H R
P I
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "A", numRows = 1
<strong>Output:</strong> "A"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists of English letters (lower-case and upper-case), <code>','</code> and <code>'.'</code>.</li>
<li><code>1 <= numRows <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string convert(string s, int numRows) {\n if(numRows==1)\n return s;\n int length = s.length();\n string ans = \"\";\n for(int i=0;i<numRows;i++){\n int idx = i;\n int deltaSouth = 2 * (numRows-1-i);\n int deltaNorth = 2 * i;\n\n bool goingSouth = true;\n\n while(idx<length){\n ans = ans + s[idx];\n if(i==0){\n idx += deltaSouth;\n }else if(i==numRows-1){\n idx += deltaNorth;\n }\n else{\n if(goingSouth){\n idx += deltaSouth;\n // goingSouth = false;\n }else{\n idx += deltaNorth;\n // goingSouth = true;\n }\n }\n goingSouth = !goingSouth;\n }\n }\n\n return ans;\n }\n};",
"memory": "41468"
} |
6 | <p>The string <code>"PAYPALISHIRING"</code> is written in 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>
<pre>
P A H N
A P L S I I G
Y I R
</pre>
<p>And then read line by line: <code>"PAHNAPLSIIGYIR"</code></p>
<p>Write the code that will take a string and make this conversion given a number of rows:</p>
<pre>
string convert(string s, int numRows);
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 3
<strong>Output:</strong> "PAHNAPLSIIGYIR"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 4
<strong>Output:</strong> "PINALSIGYAHRPI"
<strong>Explanation:</strong>
P I N
A L S I G
Y A H R
P I
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "A", numRows = 1
<strong>Output:</strong> "A"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists of English letters (lower-case and upper-case), <code>','</code> and <code>'.'</code>.</li>
<li><code>1 <= numRows <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string convert(string s, int numRows) { \n string all[numRows][s.size()];\n int row=0;\n int col=0;\n int cur=0;\n while(cur < s.size()){\n while(row < numRows && cur<s.size()){\n all[row++][col] = s[cur++];\n }\n row=max(0,row-2);\n while(row>0 && cur<s.size()){\n all[row--][++col]=s[cur++];\n }\n col++;\n \n }\n string ans=\"\";\n for(int i=0; i<numRows ; i++){\n for(int j=0;j<s.size();j++){\n ans+=all[i][j];\n }\n }\n return ans;\n }\n};",
"memory": "41881"
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.