输入: s = "LEETCODEISHIRING", numRows = 3
输出: "LCIRETOESIIGEDHN"
示例 2:
输入: s = "LEETCODEISHIRING", numRows = 4
输出: "LDREOEIIECIHNTSG"
解释:
L D R
E O E I I
E C I H N
T S G
思路
按顺序遍历字符串的s为单个字符为c ,设置flag =1
res[i] += c
更新i += flag
到达Z的转折点为 flag = -flag
/** * @param{string} s * @param{number} numRows * @return{string} */varconvert=function(s, numRows) {if( numRows ===1) return slet res =newArray(numRows).fill('')let flag =-1let i =0for (constcof s) {if(i ===0|| i === numRows -1 )flag =-flag res[i] += c i += flag }returnres.join('')};