[leetcode] Z字型变换
6. Z 字形变换 - 力扣(LeetCode)
class Solution {
public:string convert(string s, int numRows) {//处理边界情况if(s.size() == 1 || numRows == 1) return s;int n = s.size(), d = 2*numRows - 2;string ret;//处理第一行for(int i = 0; i < n; i += d){ret += s[i];}//处理中间行for(int k = 1; k < numRows - 1; k++){for(int i = k, j = d - k; i < n || j < n; i += d, j += d){if(i < n) ret += s[i];if(j < n) ret += s[j];}}//处理最后一行for(int i = numRows - 1; i < n; i += d){ret += s[i];}return ret;}
};