Skip to content

Commit e1d15da

Browse files
committed
feat: add question 1324
1 parent ce69dda commit e1d15da

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

1324.竖直打印单词.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* 1. 找到最大单词长度
3+
* 2. 以 0 ~ 最大长度 为下标遍历各单词
4+
* 3. 删除末尾空格
5+
*/
6+
7+
/**
8+
* @param {string} s
9+
* @return {string[]}
10+
*/
11+
var printVertically = function(s) {
12+
const words = s.split(' ');
13+
let max = 0;
14+
for (const word of words) {
15+
max = Math.max(max, word.length);
16+
}
17+
18+
const result = [];
19+
for (let i = 0; i < max; i++) {
20+
result[i] = [];
21+
for (let j = 0; j < words.length; j++) {
22+
result[i].push(words[j][i] || ' ');
23+
}
24+
while (result[i][result[i].length - 1] === ' ') {
25+
result[i].pop();
26+
}
27+
result[i] = result[i].join('');
28+
}
29+
30+
return result;
31+
};

0 commit comments

Comments
 (0)