{
  "type": "code_options",
  "author": "csdn.net",
  "source": "solution.md",
  "exercise_id": "1f3bdab9bd094d6e911da6bcb99cc804",
  "keywords": "字符串",
  "title": "Z 字形变换",
  "desc": [
    {
      "content": "\n<div class=\"notranslate\">\n<p>将一个给定字符串 <code>s</code> 根据给定的行数 <code>numRows</code> ，以从上往下、从左到右进行&nbsp;Z 字形排列。</p>",
      "language": "markdown"
    },
    {
      "content": "\n<p>比如输入字符串为 <code>\"PAYPALISHIRING\"</code>&nbsp;行数为 <code>3</code> 时，排列如下：</p>",
      "language": "markdown"
    },
    {
      "content": "\n<pre>\nP   A   H   N\nA P L S I I G\nY   I   R</pre>",
      "language": "markdown"
    },
    {
      "content": "\n<p>之后，你的输出需要从左往右逐行读取，产生出一个新的字符串，比如：<code>\"PAHNAPLSIIGYIR\"</code>。</p>",
      "language": "markdown"
    },
    {
      "content": "\n<p>请你实现这个将字符串进行指定行数变换的函数：</p>",
      "language": "markdown"
    },
    {
      "content": "\n<pre>string convert(string s, int numRows);</pre>",
      "language": "markdown"
    },
    {
      "content": "\n<p>&nbsp;</p>",
      "language": "markdown"
    },
    {
      "content": "\n<p><strong>示例 1：</strong></p>",
      "language": "markdown"
    },
    {
      "content": "\n<pre><strong>输入：</strong>s = \"PAYPALISHIRING\", numRows = 3\n<strong>输出：</strong>\"PAHNAPLSIIGYIR\"\n</pre>\n<strong>示例 2：</strong>",
      "language": "markdown"
    },
    {
      "content": "\n<pre><strong>输入：</strong>s = \"PAYPALISHIRING\", numRows = 4\n<strong>输出：</strong>\"PINALSIGYAHRPI\"\n<strong>解释：</strong>\nP     I    N\nA   L S  I G\nY A   H R\nP     I\n</pre>",
      "language": "markdown"
    },
    {
      "content": "\n<p><strong>示例 3：</strong></p>",
      "language": "markdown"
    },
    {
      "content": "\n<pre><strong>输入：</strong>s = \"A\", numRows = 1\n<strong>输出：</strong>\"A\"\n</pre>",
      "language": "markdown"
    },
    {
      "content": "\n<p>&nbsp;</p>",
      "language": "markdown"
    },
    {
      "content": "\n<p><strong>提示：</strong></p>",
      "language": "markdown"
    },
    {
      "content": "\n<ul>\n<li><code>1 &lt;= s.length &lt;= 1000</code></li>\n<li><code>s</code> 由英文字母（小写和大写）、<code>','</code> 和 <code>'.'</code> 组成</li>\n<li><code>1 &lt;= numRows &lt;= 1000</code></li>\n</ul>\n</div>",
      "language": "markdown"
    }
  ],
  "answer": [
    {
      "content": "",
      "language": "java"
    }
  ],
  "prepared": [
    [
      {
        "content": "",
        "language": "java"
      }
    ],
    [
      {
        "content": "",
        "language": "java"
      }
    ],
    [
      {
        "content": "",
        "language": "java"
      }
    ]
  ],
  "template": {
    "content": "class Solution {\n\tpublic String convert(String s, int numRows) {\n\t\tif (numRows == 1)\n\t\t\treturn s;\n\t\tint len = s.length();\n\t\tif (len <= numRows)\n\t\t\treturn s;\n\t\tint cycle_len = 2 * numRows - 2;\n\t\tint full_cycles = len / cycle_len;\n\t\tint left = len % cycle_len;\n\t\tStringBuilder r = new StringBuilder();\n\t\tint i;\n\t\tfor (i = 0; i < full_cycles; ++i) {\n\t\t\tr.append(s.charAt(i * cycle_len));\n\t\t}\n\t\tif (left > 0) {\n\t\t\tr.append(s.charAt(i * cycle_len));\n\t\t}\n\t\tfor (i = 0; i < numRows - 2; ++i) {\n\t\t\tint j;\n\t\t\tfor (j = 0; j < full_cycles; ++j) {\n\t\t\t\tr.append(s.charAt(j * cycle_len + i + 1));\n\t\t\t\tr.append(s.charAt(j * cycle_len + i + 1 + cycle_len - 2 * (i + 1)));\n\t\t\t}\n\t\t\tif (left > 0) {\n\t\t\t\tif (j * cycle_len + i + 1 < len)\n\t\t\t\t\tr.append(s.charAt(j * cycle_len + i + 1));\n\t\t\t\tif (j * cycle_len + i + 1 + cycle_len - 2 * (i + 1) < len)\n\t\t\t\t\tr.append(s.charAt(j * cycle_len + i + 1 + cycle_len - 2 * (i + 1)));\n\t\t\t}\n\t\t}\n\t\tfor (i = 0; i < full_cycles; ++i)\n\t\t\tr.append(s.charAt(i * cycle_len + numRows - 1));\n\t\tif (left >= numRows)\n\t\t\tr.append(s.charAt(i * cycle_len + numRows - 1));\n\t\treturn r.toString();\n\t}\n}",
    "language": "java"
  },
  "node_id": "dailycode-bcd5c08f84344295bc2f185d6944f7c2",
  "license": "csdn.net",
  "created_at": 1637894159,
  "topic_link": "https://bbs.csdn.net/topics/600469917"
}