# Z 字形变换

<div class="notranslate">
    <p>将一个给定字符串 <code>s</code> 根据给定的行数 <code>numRows</code> ，以从上往下、从左到右进行&nbsp;Z 字形排列。</p>

    <p>比如输入字符串为 <code>"PAYPALISHIRING"</code>&nbsp;行数为 <code>3</code> 时，排列如下：</p>

    <pre>
    P   A   H   N
    A P L S I I G
    Y   I   R</pre>

    <p>之后，你的输出需要从左往右逐行读取，产生出一个新的字符串，比如：<code>"PAHNAPLSIIGYIR"</code>。</p>

    <p>请你实现这个将字符串进行指定行数变换的函数：</p>

    <pre>string convert(string s, int numRows);</pre>

    <p>&nbsp;</p>

    <p><strong>示例 1：</strong></p>

    <pre><strong>输入：</strong>s = "PAYPALISHIRING", numRows = 3
<strong>输出：</strong>"PAHNAPLSIIGYIR"
</pre>
    <strong>示例 2：</strong>

    <pre><strong>输入：</strong>s = "PAYPALISHIRING", numRows = 4
<strong>输出：</strong>"PINALSIGYAHRPI"
<strong>解释：</strong>
P     I    N
A   L S  I G
Y A   H R
P     I
</pre>

    <p><strong>示例 3：</strong></p>

    <pre><strong>输入：</strong>s = "A", numRows = 1
<strong>输出：</strong>"A"
</pre>

    <p>&nbsp;</p>

    <p><strong>提示：</strong></p>

    <ul>
        <li><code>1 &lt;= s.length &lt;= 1000</code></li>
        <li><code>s</code> 由英文字母（小写和大写）、<code>','</code> 和 <code>'.'</code> 组成</li>
        <li><code>1 &lt;= numRows &lt;= 1000</code></li>
    </ul>
</div>

以下程序实现了这一功能，请你填补空白处内容：

```cpp
class Solution
{
public:
	string convert(string s, int numRows)
	{
		if (numRows == 1)
			return s;
		int len = s.size();
		if (len <= numRows)
			return s;
		int cycle_len = 2 * numRows - 2;
		int full_cycles = len / cycle_len;
		int left = len % cycle_len;
		string r;
		int i;
		for (i = 0; i < full_cycles; ++i)
		{
			r += s[i * cycle_len];
		}
		if (left)
			r += s[i * cycle_len];
		for (i = 0; i < numRows - 2; ++i)
		{
			int j;
			for (j = 0; j < full_cycles; ++j)
			{
				r += s[j * cycle_len + i + 1];
				r += s[j * cycle_len + i + 1 + cycle_len - 2 * (i + 1)];
			}
			if (left)
			{
				_____________________;
			}
		}
		for (i = 0; i < full_cycles; ++i)
			r += s[i * cycle_len + numRows - 1];
		if (left >= numRows)
			r += s[i * cycle_len + numRows - 1];
		return r;
	}
};
```

## template

```cpp
class Solution
{
public:
	string convert(string s, int numRows)
	{
		if (numRows == 1)
			return s;
		int len = s.size();
		if (len <= numRows)
			return s;
		int cycle_len = 2 * numRows - 2;
		int full_cycles = len / cycle_len;
		int left = len % cycle_len;
		string r;
		int i;
		for (i = 0; i < full_cycles; ++i)
		{
			r += s[i * cycle_len];
		}
		if (left)
			r += s[i * cycle_len];
		for (i = 0; i < numRows - 2; ++i)
		{
			int j;
			for (j = 0; j < full_cycles; ++j)
			{
				r += s[j * cycle_len + i + 1];
				r += s[j * cycle_len + i + 1 + cycle_len - 2 * (i + 1)];
			}
			if (left)
			{
				if (j * cycle_len + i + 1 < len)
					r += s[j * cycle_len + i + 1];
				if (j * cycle_len + i + 1 + cycle_len - 2 * (i + 1) < len)
					r += s[j * cycle_len + i + 1 + cycle_len - 2 * (i + 1)];
			}
		}
		for (i = 0; i < full_cycles; ++i)
			r += s[i * cycle_len + numRows - 1];
		if (left >= numRows)
			r += s[i * cycle_len + numRows - 1];
		return r;
	}
};
```

## 答案

```cpp
if (j * cycle_len + i + 1 < len)
	r += s[j * cycle_len + i + 1];
if (j * cycle_len + i + 1 + cycle_len - 2 * (i + 1) < len)
	r += s[j * cycle_len + i + 1 + cycle_len - 2 * (i + 1)];
```

## 选项

### A

```cpp
if (j * cycle_len + i + 1 < len)
	r += s[j * cycle_len + i];
if (j * cycle_len + i + cycle_len - 2 * (i + 1) < len)
	r += s[j * cycle_len + i + cycle_len - 2 * (i + 1)];
```

### B

```cpp
if (j * cycle_len + i + 1 < len)
	r += s[j * cycle_len + i + 1];
if (j * cycle_len + i + 1 + cycle_len - 1 * (i + 1) < len)
	r += s[j * cycle_len + i + 1 + cycle_len - 1 * (i + 1)];
```

### C

```cpp
if (j * cycle_len + i - 1 < len)
	r += s[j * cycle_len + i + 1];
if (j * cycle_len + i - 1 + cycle_len - 1 * (i + 1) < len)
	r += s[j * cycle_len + i - 1 + cycle_len - 1 * (i + 1)];
```