topic:
The number of a given string according to the given row, to down, left to right from the Z-shaped arrangement.
Example, the input string is the number of rows "LEETCODEISHIRING" is 3, arranged as follows:
LCIR
ETOESIIG
EDHN
After that, you need the output from left to right read line by line, produce a new string, such as: "LCIRETOESIIGEDHN".
You will realize this string conversion function specified number of lines:
string convert (string s, int numRows );
Example 1:
Input: s = "LEETCODEISHIRING", numRows = 3
Output: "LCIRETOESIIGEDHN"
Example 2:
Input: s = "LEETCODEISHIRING", numRows = 4
Output: "LDREOEIIECIHNTSG"
explanation:
L D R
E O E I I
E C I H N
T S G
/** * @param {string} s * @param {number} numRows * @return {string} */ var convert = function(s, numRows) { if(numRows<2){ return s; } let str = new Array(); for(let i=0;i<numRows;i++){ str[i] = ""; } let index =0; let flag = -1; let j=0; while(index<s.length){ str[j]+= s[index]; index ++; if(j==0|| j==numRows-1){ flag = -flag; } j +=flag; } return str.join("") };
Ways: primarily through the use of a flag array direction and converting the character string into the array corresponding traverse into, and finally merge array
Source: https://leetcode-cn.com/problems/zigzag-conversion/solution/zzi-xing-bian-huan-by-jyd/
Source: stay button (LeetCode)
link: https: //leetcode-cn.com/problems/zigzag-conversion
copyrighted by deduction from all networks. Commercial reprint please contact the authorized official, non-commercial reprint please indicate the source.