leetcode:数学: Ugly Number II(264)
2016-08-30 15:51
351 查看
Write a program 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.
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.
class Solution { public: int nthUglyNumber(int n) { vector<int> res(1, 1); int i2 = 0, i3 = 0, i5 = 0; while (res.size() < n) { int m2 = res[i2] * 2, m3 = res[i3] * 3, m5 = res[i5] * 5; int mn = min(m2, min(m3, m5)); if (mn == m2) ++i2; if (mn == m3) ++i3; if (mn == m5) ++i5; res.push_back(mn); } return res.back(); } };
相关文章推荐
- LeetCode 264. Ugly Number II
- [leetcode] 264.Ugly Number II
- 【LeetCode】264. Ugly Number II
- [leetcode] 264. Ugly Number II
- Leetcode 264. Ugly Number II
- leetCode #264 Ugly Number II
- Leetcode练习 #264 Ugly Number II
- LeetCode - 263. Ugly Number & 264. Ugly Number II & 313. Super Ugly Number
- LeetCode 264: Ugly Number II
- leetcode - 264. Ugly Number II
- [leetcode]264. Ugly Number II
- (LeetCode 264) Ugly Number II
- [LeetCode] 264. Ugly Number II
- leetcode 264. Ugly Number II
- [LeetCode]264. Ugly Number II
- leetcode 264. Ugly Number II
- leetcode 264: Ugly Number II
- Leetcode 263. Ugly Number & 264. Ugly Number II
- [leetcode 263 264]Ugly Number I II
- LeetCode 264: Ugly Number II