题目: https://oj.leetcode.com/problems/zigzag-conversion/ https://oj.leetcode.com/problems/zigzag-conversion/
需要找到每行各种字符对应在源字符的位置,即找出两者之间的规律。这题就比较好解了。
代码:
class Solution {
public:
string convert(string s, int nRows) {
if (s.length() == 0 || nRows <= 0) return "";
if (nRows == 1) return s;
string res = s;
int nums = 2 * nRows - 2;
int index = 0;
for (int i = 0; i<nRows; i++)
{
for (int j = i; j<s.length(); j += nums)
{
res[index++] = s[j];
if (i != 0 && i != nRows-1 && j + nums - 2 * i<s.length())
{
res[index++] = s[j + nums - 2 * i];
}
}
}
return res;
}
};