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) 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: string convert(string s, int numRows);
Example 1: Input: s = “PAYPALISHIRING”, numRows = 3 Output: “PAHNAPLSIIGYIR” Example 2: Input: s = “PAYPALISHIRING”, numRows = 4 Output: “PINALSIGYAHRPI” Explanation: P I N A L S I G Y A H R P I Example 3: Input: s = “A”, numRows = 1 Output: “A”
Constraints:
1 <= s.length <= 1000
s consists of English letters (lower-case and upper-case), ',' and '.'.
1 <= numRows <= 1000
- code arr[row] += c will create a new string each time, slow
class Solution {
public String convert(String s, int numRows) {
if (numRows == 1) return s;
String[] arr = new String[numRows];
Arrays.fill(arr, "");
int direction = -1;
int row = 0;
for (char c: s.toCharArray()){
arr[row] += c;
if (row == 0) direction = 1;
if (row == numRows - 1) direction = -1;
row += direction;
}
return String.join("", arr);
}
}
- code
class Solution {
public String convert(String s, int numRows) {
if (numRows == 1) return s;
StringBuilder[] arr = new StringBuilder[numRows];
for (int i = 0; i < numRows; i++) arr[i] = new StringBuilder();
int direction = -1;
int row = 0;
for (char c: s.toCharArray()){
arr[row].append(c);
if (row == 0) direction = 1;
if (row == numRows - 1) direction = -1;
row += direction;
}
for (int i = 1; i < numRows; i++)
arr[0].append(arr[i]);
return arr[0].toString();
}
}
- code
class Solution:
def convert(self, s: str, numRows: int) -> str:
if numRows == 1:
return s
# rows = [[]] * numRows wrong way, append will apply to all sub-list
rows = [[] for i in range(numRows)]
flag = True
step = 0
for v in s:
rows[step].append(v)
if flag:
if step == len(rows) - 1:
step -= 1
flag = False
else:
step += 1
else:
if step == 0:
step += 1
flag = True
else:
step -= 1
return "".join([x for row in rows for x in row])
# res = []
# for row in rows:
# res.extend(row)
# return "".join(res)
c
class Solution:
def convert(self, s: str, numRows: int) -> str:
# below this check, code only works on 2 or more numRows
if numRows < 2:
return s
# sets initial variables
direction = 1
row = 0
output = ['']*numRows
# loops through characters in strings
for c in s:
# adds current character to "row" in the array
output[row] = output[row] + c
# checks to see if at last available row (-1 because array starts at 0)
if row == numRows-1:
direction = -1
# same as before just switches directions
elif row == 0:
direction = 1
# next row is determined by direction positive or negative
row += direction
# returns the output list as a single string using nothing ('') as the "separator"
return ''.join(output)