6. ZigZag Conversion
The string "PAYPALISHIRING"
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(易读的))
1 | P A H N |
1 | "PAHNAPLSIIGYIR" |
And then read line by line: "PAHNAPLSIIGYIR"
Write the code that will take a string and make this conversion given a number of rows:
1 | string convert(string text, int nRows); |
题意:
把一个字符串根据给的的行数,按之字形模式进行输出,然后按照输出的之字形模式按行读取,返回一个新的字符串。
思路:
方法一:
创建nRows个string数组,将s字符串按照0,1,2,…,nRows-2,nRows-1,然后nRows-2,…,2,1的顺序,组个把s串中的字符放入到各个string中。
具体来说,string数组下标先递增,直到nRows-1,则反向,直到0,再反向,……直到s字符串遍历完,再依次组合0~nRows-1个string元素。切记一点当到nRows-1返回的时候要从nRows-2开始,因为之字形返回最后一行数组s中字符元素,但是正向第一行是有的,所以从0开始。
1 | class Solution { |
方法二:
创建nRows个int数组,将s字符串的索引按照0,1,2,…,nRows-2,nRows-1,然后nRows-2,…,2,1,0的顺序装入int输出中。
具体来说,数组下标先递增,直到nRows-1,则反向,直到0,再反向,……直到s字符索引全部装入各数组,再依次组合0~nRows-1个数组。切记一点当到nRows-1返回的时候要从nRows-2开始记录索引,因为之字形返回最后一行数组没有索引元素,但是正向第一行是有索引元素的,所以从0开始。
1 | class Solution2 { |
Java Code:
1 | class Solution { |
1 | class Solution { |