[LeetCode]006.ZigZag Conversion

Question

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 text, int nRows);

convert("PAYPALISHIRING", 3) should return "PAHNAPLSIIGYIR".

Solution

  • C
1
2
3
char *convert(char *s, int nRows) {
}
  • C++
1
2
3
4
5
6
class Solution {
public:
string convert(string s, int nRows) {
}
};
  • Java
1
2
3
4
5
public class Solution {
public String convert(String s, int nRows) {
}
}
  • JavaScript
1
2
3
4
5
6
7
8
/**
* @param {string} s
* @param {number} nRows
* @returns {string}
*/
var convert = function(s, nRows) {
};
  • Python
1
2
3
class Solution:
# @return a string
def convert(self, s, nRows):
  • Swift
1
Hello World
坚持原创技术分享,您的支持将鼓励我继续创作!