Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 0 4 8
- 1 3 5 7 9
- 2 6
- #include <cmath>
- #include <iostream>
- #include <string>
- class Solution {
- public:
- std::string convert(const std::string& s, const int numRows) {
- if (numRows == 1) { return s; }
- const int size = s.size();
- std::string converted = "";
- const int num = numRows + (numRows - 2);
- int row = 0;
- int i = 0;
- int sub = 0;
- bool useDiff = true;
- while (row < numRows) {
- converted += s[row];
- if (row == 0 || row == numRows - 1) {
- i = row + num;
- while (i < size) {
- std::string str(1, s[i]);
- converted.append(str);
- i += num;
- }
- } else {
- int diff = std::abs(num - sub);
- i = diff;
- while (i < size && row + i < size) {
- if (useDiff) {
- std::string str(1, s[row + i]);
- converted.append(str);
- useDiff = false;
- i += sub;
- } else {
- std::string str(1, s[row + i]);
- converted.append(str);
- useDiff = true;
- i += diff;
- }
- }
- }
- useDiff = true;
- sub += 2;
- row++;
- }
- return converted;
- }
- };
- int main()
- {
- Solution solution;
- std::string s = "0123456789";
- int numRows = 3;
- std::cout << s << 'n';
- s = solution.convert(s, numRows);
- std::cout << s << 'n';
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement