跳转至

6. Zigzag Conversion

6. Zigzag Conversion - Medium

Description

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
2
3
P   A   H   N
A P L S I I G
Y   I   R

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 s, int numRows);

这道题还是比较经典了,找到规律马上就解决了。

Solution

首先当numRows = 1时,那么就不用处理,直接返回s就行了;

如果numRows > 1时,那我们就需要把字符串分成numRows行了

评论

回到页面顶部