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"
Example 1:
1 2
Input: s = "PAYPALISHIRING", numRows = 3 Output: "PAHNAPLSIIGYIR"
Example 2:
1 2 3 4 5 6 7
Input: s = "PAYPALISHIRING", numRows = 4 Output: "PINALSIGYAHRPI" Explanation:
classSolution(object): defconvert(self, s, numRows): """ :type s: str :type numRows: int :rtype: str """ size = len(s) if size <= numRows or numRows == 1: return s ans = '' i = 0 while i < numRows: j = i if i == 0or i == numRows - 1: while j < size: ans += s[j] j += 2*numRows - 2 if2 * numRows - 2 == 0: break else: while j < size: ans += s[j] j += 2*(numRows - i) - 2 if j >= size: break ans += s[j] j += 2*i i += 1 return ans