Nav: << previous: 5.最长回文子串 | next: 7.整数反转 >>


Description

tab: English
 
<p>The string <code>&quot;PAYPALISHIRING&quot;</code> is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)</p>
 
<pre>
P   A   H   N
A P L S I I G
Y   I   R
</pre>
 
<p>And then read line by line: <code>&quot;PAHNAPLSIIGYIR&quot;</code></p>
 
<p>Write the code that will take a string and make this conversion given a number of rows:</p>
 
<pre>
string convert(string s, int numRows);
</pre>
 
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
 
<pre>
<strong>Input:</strong> s = &quot;PAYPALISHIRING&quot;, numRows = 3
<strong>Output:</strong> &quot;PAHNAPLSIIGYIR&quot;
</pre>
 
<p><strong class="example">Example 2:</strong></p>
 
<pre>
<strong>Input:</strong> s = &quot;PAYPALISHIRING&quot;, numRows = 4
<strong>Output:</strong> &quot;PINALSIGYAHRPI&quot;
<strong>Explanation:</strong>
P     I    N
A   L S  I G
Y A   H R
P     I
</pre>
 
<p><strong class="example">Example 3:</strong></p>
 
<pre>
<strong>Input:</strong> s = &quot;A&quot;, numRows = 1
<strong>Output:</strong> &quot;A&quot;
</pre>
 
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
 
<ul>
	<li><code>1 &lt;= s.length &lt;= 1000</code></li>
	<li><code>s</code> consists of English letters (lower-case and upper-case), <code>&#39;,&#39;</code> and <code>&#39;.&#39;</code>.</li>
	<li><code>1 &lt;= numRows &lt;= 1000</code></li>
</ul>
 
 
 
---
 
[submissions](https://leetcode.com/problems/zigzag-conversion/submissions/) | [solutions](https://leetcode.com/problems/zigzag-conversion/solutions/)
 
 
tab: 中文
 
<p>将一个给定字符串 <code>s</code> 根据给定的行数 <code>numRows</code> ,以从上往下、从左到右进行 Z 字形排列。</p>
 
<p>比如输入字符串为 <code>"PAYPALISHIRING"</code> 行数为 <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> </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> </p>
 
<p><strong>提示:</strong></p>
 
<ul>
	<li><code>1 <= s.length <= 1000</code></li>
	<li><code>s</code> 由英文字母(小写和大写)、<code>','</code> 和 <code>'.'</code> 组成</li>
	<li><code>1 <= numRows <= 1000</code></li>
</ul>
 
 
 
---
 
[提交记录](https://leetcode.cn/problems/zigzag-conversion/submissions/) | [题解](https://leetcode.cn/problems/zigzag-conversion/solution/)
 
 

Solutions & Notes

properties:
  note.updated:
    displayName: Last Updated
  note.relative_links:
    displayName: Related Links
  note.desc:
    displayName: Description
  note.grade:
    displayName: Rating
  note.program_language:
    displayName: Language
  note.time_complexity:
    displayName: TC
  note.space_complexity:
    displayName: SC
views:
  - type: table
    name: Solutions & Notes
    filters:
      and:
        - file.hasLink(this.file)
        - file.tags.containsAny("leetcode/solution", "leetcode/note")
    order:
      - file.name
      - desc
      - program_language
      - time_complexity
      - space_complexity
      - grade
      - relative_links
      - updated
    sort:
      - property: grade
        direction: ASC
      - property: time_complexity
        direction: ASC
      - property: program_language
        direction: ASC
    columnSize:
      file.name: 104
      note.space_complexity: 65
      note.grade: 126
 

Similar Problems

properties:
  note.lcTopics:
    displayName: Topics
  note.lcAcRate:
    displayName: AC Rate
  note.favorites:
    displayName: Favorites
  note.grade:
    displayName: Rating
  note.translatedTitle:
    displayName: Title (CN)
  note.lcDifficulty:
    displayName: Difficulty
views:
  - type: table
    name: Similar Problems
    filters:
      and:
        - file.hasLink(this.file)
        - similarQuestions.contains(this.file)
    order:
      - file.name
      - translatedTitle
      - lcTopics
      - lcDifficulty
      - lcAcRate
      - grade
      - favorites
    sort:
      - property: file.name
        direction: ASC
      - property: lcTopics
        direction: DESC
    columnSize:
      note.translatedTitle: 240
      note.lcTopics: 347
      note.lcAcRate: 75
      note.grade: 122