题目描述:
方法一:
class Solution: def convert(self, s: str, numRows: int) -> str: if not s: return "" if numRows == 1: return s s_Rows = [""] * numRows i = 0 n = len(s) while i < n: for j in range(numRows): if i < n: s_Rows[j] += s[i] i += 1 for j in range(numRows-2,0,-1): if i < n: s_Rows[j] += s[i] i += 1 return "".join(s_Rows)
方法二:
class Solution: def convert(self, s: str, numRows: int) -> str: if not s: return "" if numRows == 1: return s split_s_len = numRows * 2 - 2 data = [] n = len(s) for i in range(0, n,split_s_len): data.append(s[i:i+split_s_len]) #print(data) res = "" for i in range(numRows): for tmp in data: if i < len(tmp): if i == 0 or i == numRows-1: res += tmp[i] else: res += tmp[i] if split_s_len -i < len(tmp): res += tmp[split_s_len-i] return res