Код: Выделить всё
/**
* @param {string} s
* @param {number} numRows
* @return {string}
*/
var convert = function(s, numRows) {
/* Get each Nths value:
e.g. s = [0123456789]
get each value that will be an integer when value / 3
nth value = 0369
then (value - 1)/3
147
then (value-2)/3
258
up untill any number is repeated
*/
const characters = [...s] /* convert the string into an array to work with individual values*/
for(i = 0; i < characters.length; i++){
newarr = [] // new array to store the return
if(i % numRows === 0){ /* trying to see whether the index I'm working with can be divided by the number of rows*/
newarr.push(characters[i%numRows]) // add the answer to newarr
}
}return newarr
};
Строка «PAYPALISHIRING» (переменная s) записывается зигзагообразным узором на заданном количестве строк, вот так
Код: Выделить всё
P A H N
A P L S I I G
Y I R
Ввод: s = "PAYPALISHIRING", numRows = 3
Вывод: "PAHNAPLSIIGYIR"
Подробнее здесь: https://stackoverflow.com/questions/798 ... else-to-wo
Мобильная версия