Home > Software engineering >  Lintcode4 Ugly Number II solution answer key
Lintcode4 Ugly Number II solution answer key

Time:09-21

[title description]

Ugly number is a number that only have the factors of 2, 3 and 5.

Design an algorithm to find the NTH ugly number. The first 10 ugly Numbers are 1, 2, 3, 4, 5, 6, 8, 9, 10, 12...

Notice: Note that 1 is typically treated as an ugly number.

Design an algorithm to find only prime factor 2,3,5 n large number, number of eligible, such as: 1, 2, 3, 4, 5, 6, 8, 9, 10, 12...

Note: we can think 1 is an ugly,

[title link]

http://www.lintcode.com/en/problem/ugly-number-ii/

[title]

This is more than list the Merge Sort of an extension,

For any one ugly number - K, 2 * K, 3 * K, and 5 * K are ugly number, so that new ugly number from existing ugly number, with,3,5 {2} multiplication,

If

Ugly Number: 1, 2, 3, 4, 5, 6, 8, 10,...

So 1 2 * 2 * 2, 3, 4 * 2 * 2 * 5 6 * 2 * 2 8 10 * 2... * 2

1 2 * 3 * 3 3 * 3 * 3 4 5 6 * 3 * 3 * 3 8 to 10 * 3... * 3

1 * 5 5 3 * 2 * 4 * 5 * 5 * 5 * 6 8 5 10 * 5... * 5

Are ugly number, as long as the new produce ugly number through the merge sort is added to the original ugly number in the array is ok, until you find the first N,

[the answer link]

http://www.jiuzhang.com/solutions/ugly-number-ii/
  • Related