LeetCode6. Z 字形变换
题解模拟就好了。链接:https://leetcode-cn.com/problems/zigzag-conversion/submissions/
class Solution {
public:
string convert(string s, int numRows) {
if (numRows == 1)return s;
char temp[1005][1005] = {'\0'};
string str1="";
int flag = 1;
int i = 1, j = 1;
//模拟Z字变换;
for (int k = 0; k < s.length(); k++) {
temp[i][j] = s[k];
if (flag == 1) {
if (i == numRows) {
flag = 0;
j++;
i--;
}
else i++;
}
else {
if (i == 1) {
flag = 1;
i++;
}
else {
j++;
i--;
}
}
}
//提取变换后的排序;
for (int i1 = 1; i1 <= numRows; i1++) {
for (int j1 = 1; j1 <= j; j1++) {
if (temp[i1][j1] != '\0') str1 += temp[i1][j1];
}
}
return str1;
}
};