|
| 1 | +class Solution { |
| 2 | +public: |
| 3 | + string convert(string s, int numRows) { |
| 4 | + if(s.size()<3 || numRows == 1) |
| 5 | + return s; |
| 6 | + int l = s.length(); |
| 7 | + //cout<<"\nString length: "<<l<<", size: "<<s.size(); |
| 8 | + string answer=""; |
| 9 | + vector<bool> isP; |
| 10 | + int j=0; |
| 11 | + bool f=true; |
| 12 | + int jump=0; |
| 13 | + |
| 14 | + for(int i=0;i<l;i++) { |
| 15 | + isP.push_back(true); |
| 16 | + } |
| 17 | + for(int i=0;i<numRows;i++) { |
| 18 | + f= true; |
| 19 | + for(int next=i;next<l;) { |
| 20 | + if(isP[next]) { |
| 21 | + answer.push_back(s[next]); |
| 22 | + isP[next] = false; |
| 23 | + } |
| 24 | + if(f) { |
| 25 | + jump = 2*(numRows-i-1); |
| 26 | + f = false; |
| 27 | + if(!jump){ |
| 28 | + continue; |
| 29 | + } |
| 30 | + |
| 31 | + } else { |
| 32 | + jump = 2*i; |
| 33 | + f = true; |
| 34 | + if(!jump){ |
| 35 | + continue; |
| 36 | + } |
| 37 | + } |
| 38 | + next += jump; |
| 39 | + } |
| 40 | + } |
| 41 | + return answer; |
| 42 | + } |
| 43 | +}; |
| 44 | + |
| 45 | +string stringToString(string input) { |
| 46 | + assert(input.length() >= 2); |
| 47 | + string result; |
| 48 | + for (int i = 1; i < input.length() -1; i++) { |
| 49 | + char currentChar = input[i]; |
| 50 | + if (input[i] == '\\') { |
| 51 | + char nextChar = input[i+1]; |
| 52 | + switch (nextChar) { |
| 53 | + case '\"': result.push_back('\"'); break; |
| 54 | + case '/' : result.push_back('/'); break; |
| 55 | + case '\\': result.push_back('\\'); break; |
| 56 | + case 'b' : result.push_back('\b'); break; |
| 57 | + case 'f' : result.push_back('\f'); break; |
| 58 | + case 'r' : result.push_back('\r'); break; |
| 59 | + case 'n' : result.push_back('\n'); break; |
| 60 | + case 't' : result.push_back('\t'); break; |
| 61 | + default: break; |
| 62 | + } |
| 63 | + i++; |
| 64 | + } else { |
| 65 | + result.push_back(currentChar); |
| 66 | + } |
| 67 | + } |
| 68 | + return result; |
| 69 | +} |
| 70 | + |
| 71 | +int stringToInteger(string input) { |
| 72 | + return stoi(input); |
| 73 | +} |
| 74 | + |
| 75 | +int main() { |
| 76 | + string line; |
| 77 | + while (getline(cin, line)) { |
| 78 | + string s = stringToString(line); |
| 79 | + getline(cin, line); |
| 80 | + int numRows = stringToInteger(line); |
| 81 | + |
| 82 | + string ret = Solution().convert(s, numRows); |
| 83 | + |
| 84 | + string out = (ret); |
| 85 | + cout << out << endl; |
| 86 | + } |
| 87 | + return 0; |
| 88 | +} |
0 commit comments