Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b008602

Browse files
Add solution to 1768.
1 parent 7316cce commit b008602

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* @param {string} word1
3+
* @param {string} word2
4+
* @return {string}
5+
*/
6+
var mergeAlternately = function (word1, word2) {
7+
const L1 = word1.length;
8+
const L2 = word2.length;
9+
10+
if (L1 === 0 && L2 === 0) {
11+
return word1;
12+
} else {
13+
if (L1 === 0) {
14+
return word1;
15+
} else if (L2 === 0) {
16+
return word2;
17+
}
18+
}
19+
20+
let out = new Array(L1 + L2);
21+
let i = 0;
22+
let upper = Math.min(L1, L2);
23+
24+
while (i < upper) {
25+
out.push(word1[i]);
26+
out.push(word2[i]);
27+
28+
i++;
29+
}
30+
31+
if (L1 === L2) {
32+
return out.join("");
33+
}
34+
35+
if (i === L1) {
36+
out.push(word2.slice(i));
37+
} else {
38+
out.push(word1.slice(i));
39+
}
40+
41+
return out.join("");
42+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /