Skip to content

Commit 932a251

Browse files
committed
https://leetcode.cn/problems/rearrange-spaces-between-words/
1 parent 3503ebe commit 932a251

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ leetcode 测试
1010

1111
##### 包含的内容如下
1212

13+
https://leetcode.cn/problems/rearrange-spaces-between-words/
14+
1315
https://leetcode.cn/problems/count-complete-tree-nodes/
1416

1517
https://leetcode.cn/problems/broken-calculator

deno.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
"rules": {
2323
"include": [],
2424
"exclude": [
25-
"no-explicit-any"
25+
"no-explicit-any",
26+
"ban-ts-comment"
2627
]
2728
}
2829
},
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// deno-lint-ignore-file ban-ts-comment
2+
export default function reorderSpaces(text: string): string {
3+
const words = text.split(/\s+/g).filter(Boolean);
4+
5+
//@ts-ignore
6+
const spaces = Array.prototype.reduce.call(
7+
text,
8+
//@ts-ignore
9+
(a, v) => a + Number(v === " "),
10+
0
11+
) as number;
12+
13+
if (words.length <= 1) return words.join("") + " ".repeat(spaces);
14+
const sep = " ".repeat(Math.floor(spaces / (words.length - 1)));
15+
16+
return words.join(sep) + " ".repeat(spaces % (words.length - 1));
17+
}

0 commit comments

Comments
 (0)