ZigZag Conversion

Jun 19, 2016


```python

#!/usr/bin/env python

coding=utf-8

def convert(s, numRows): “”” :type s: str :type numRows: int :rtype: str “”” ret=”” len_s=len(s) if numRows == 1 or len_s <= numRows: ret = s elif numRows == 2: ret = s[0::2] ret += s[1::2] else: cols=len_s/(numRows*2-2) if len_s % (numRows * 2 -2) > 0: cols += 1 for row in range(numRows): for col in range(cols): index = row + col * (numRows * 2 - 2) if index >= len_s: break else: ret += s[index]

            index += (numRows - row) * 2 -1 -1
            if row > 0 and row < numRows - 1 and index < len_s:
                ret += s[index]
return ret

convert(“PAYPALISHIRING”,3)