6 - ZigZag Conversion
Written on November 15, 2015
Tweet
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)
public class Solution {
public String convert(String s, int numRows) {
if(numRows<=1) return s;
StringBuilder sb = new StringBuilder();
int cycle = 2 * numRows - 2; //每个cycle里有多少个数,不是多少个cycle
for (int i = 0; i < numRows; i++) {
for (int j = 0; j < s.length(); j++) {
if (j % cycle == i || j % cycle == (cycle - i)) {
//除了第一个行最后一个行,其他行都有两个数满足条件
sb.append(s.charAt(j));
}
}
}
return sb.toString();
}
}
class Solution(object):
def convert(self, s, numRows):
"""
:type s: str
:type numRows: int
:rtype: str
"""
buckets = [[] for _ in range(numRows)]
i = 0
while i < len(s):
for j in range(numRows):
if i < len(s):
buckets[j].append(s[i])
i += 1
for j in reversed(range(1, numRows-1)):
if i < len(s):
buckets[j].append(s[i])
i += 1
return "".join([item for row in buckets for item in row])