class Solution {
public:
string convert(string s, int numRows) {
int len = s.size();
string ans;
if (numRows <= 1) return s;
for (int i = 0 ; i < numRows; i++) {
int c1 = 2*numRows - 2*(i+1);
int c2 = 2*i;
int cnt = i;
ans += s[cnt];
while(cnt < len) {
if (c1 != 0) {
cnt += c1;
if (cnt < len) {
ans += s[cnt];
}
}
if (c2 != 0) {
cnt += c2;
if (cnt < len) {
ans += s[cnt];
}
}
}
}
return ans;
}
};
Z字形变换 leetcode
最新推荐文章于 2022-10-17 22:02:33 发布