Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 KB | None | 0 0
  1. 0 4 8
  2. 1 3 5 7 9
  3. 2 6
  4.  
  5. #include <cmath>
  6. #include <iostream>
  7. #include <string>
  8.  
  9. class Solution {
  10. public:
  11. std::string convert(const std::string& s, const int numRows) {
  12. if (numRows == 1) { return s; }
  13. const int size = s.size();
  14. std::string converted = "";
  15. const int num = numRows + (numRows - 2);
  16. int row = 0;
  17. int i = 0;
  18. int sub = 0;
  19. bool useDiff = true;
  20. while (row < numRows) {
  21. converted += s[row];
  22. if (row == 0 || row == numRows - 1) {
  23. i = row + num;
  24. while (i < size) {
  25. std::string str(1, s[i]);
  26. converted.append(str);
  27. i += num;
  28. }
  29. } else {
  30. int diff = std::abs(num - sub);
  31. i = diff;
  32. while (i < size && row + i < size) {
  33. if (useDiff) {
  34. std::string str(1, s[row + i]);
  35. converted.append(str);
  36. useDiff = false;
  37. i += sub;
  38. } else {
  39. std::string str(1, s[row + i]);
  40. converted.append(str);
  41. useDiff = true;
  42. i += diff;
  43. }
  44. }
  45. }
  46. useDiff = true;
  47. sub += 2;
  48. row++;
  49. }
  50.  
  51. return converted;
  52. }
  53. };
  54.  
  55. int main()
  56. {
  57. Solution solution;
  58. std::string s = "0123456789";
  59. int numRows = 3;
  60. std::cout << s << 'n';
  61. s = solution.convert(s, numRows);
  62. std::cout << s << 'n';
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement