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 8a21023

Browse files
Daily Solutions With JS
1 parent cd10eda commit 8a21023

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

‎Medium/264. Ugly Number II.js‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
var nthUglyNumber = function(n) {
2+
if (!n) return 0;
3+
4+
let i2 = 0, i3 = 0, i5 = 0;
5+
let out = [1];
6+
7+
while (!out[n-1]) {
8+
let c2 = out[i2] * 2;
9+
let c3 = out[i3] * 3;
10+
let c5 = out[i5] * 5;
11+
let next = Math.min(Math.min(c2, c3), c5);
12+
out.push(next);
13+
14+
if (next === c2) i2++;
15+
if (next === c3) i3++;
16+
if (next === c5) i5++;
17+
}
18+
19+
return out[n-1];
20+
};

0 commit comments

Comments
(0)

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