Ugly Number II
描述
Write a function to find the n
-th ugly number.
Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 is the sequence of the first 10 ugly numbers.
Note that 1 is typically treated as an ugly number.
Hint:
- The naive approach is to call
isUgly()
for every number until you reach then
-th one. Most numbers are not ugly. Try to focus your effort on generating only the ugly ones. - An ugly number must be multiplied by either 2, 3, or 5 from a smaller ugly number.
- The key is how to maintain the order of the ugly numbers. Try a similar approach of merging from three sorted lists: , , and .
- Assume you have , the ugly number. Then must be .
分析
根据提示中的信息,我们知道丑陋序列可以拆分成3个子序列:
- 1x2, 2x2, 3x2, 4x2, 5x2, ...
- 1x3, 2x3, 3x3, 4x3, 5x3, ...
- 1x5, 2x5, 3x5, 4x5, 5x5, ...
每次从三个列表中取出当前最小的那个加入序列,直到第n
个为止。