LeetCode - 6. Z 字形变换
程序员文章站
2022-04-17 17:04:41
...
暴力解法…
/**
* @param {string} s
* @param {number} numRows
* @return {string}
*/
var convert = function(s, numRows) {
let arr = [];
for (let i = 0; i < numRows; i++) {
arr.push([]);
}
let col = 0;
let idx = 0;
while (idx < s.length) {
for (let row = 0; row < numRows; row++) {
// console.log('down', row, col);
arr[row][col] = s[idx];
idx++;
}
for (let row = numRows - 2; row > 0; row--) {
col++;
// console.log('up', row, col);
arr[row][col] = s[idx];
idx++;
}
col++;
}
return arr.reduce((str, c) => {
return str + c.reduce((t, cur) => {
if (cur) {
return t + cur;
}
return t;
}, '');
}, '');
};
上一篇: hexo搭建个人博客
下一篇: 视觉SLAM理论与实践5