leetcode 263|264|313. Ugly Number 1|2 313. Super Ugly Number
2017-11-10 10:37
344 查看
263. Ugly Number
Write a program to check whether a given number is an ugly number.
Ugly numbers are positive numbers whose prime factors only include
For example,
not ugly since it includes another prime factor
Note that
264. Ugly Number II
Write a program to find the
Ugly numbers are positive numbers whose prime factors only include
For example,
numbers.
Note that
not exceed 1690.
利用set自动排序的功能。
还有另外一种方法,val2 val3 val5里面中最小的那个就是下一个填进去的值,填进去之后,index后移,index2 就是 下一个*2的候选值的下标
class Solution {
public:
int nthUglyNumber(int n)
{
vector<int> vc(n, 1);
int index2 = 0;
int index3 = 0;
int index5 = 0;
int val2 = 2;
int val3 = 3;
int val5 = 5;
for (int i = 1; i < n; i++)
{
int insert = min(val2, min(val3, val5));
vc[i] = insert;
if (insert == val2)
{
val2 = vc[++index2] * 2;
}
if (insert == val3)
{
val3 = vc[++index3] * 3;
}
if (insert == val5)
{
val5 = vc[++index5] * 5;
}
}
return vc[n - 1];
}
};
313. Super Ugly Number
Write a program to find the nth super ugly number.
Super ugly numbers are positive numbers whose all prime factors are in the given prime list
size
Note:
(1)
(2) The given numbers in
(3) 0 <
106, 0 <
1000.
(4) The nth super ugly number is guaranteed to fit in a 32-bit signed integer.
方法和上面那个一模一样。
Write a program to check whether a given number is an ugly number.
Ugly numbers are positive numbers whose prime factors only include
2, 3, 5.
For example,
6, 8are ugly while
14is
not ugly since it includes another prime factor
7.
Note that
1is typically treated as an ugly number.
class Solution { public: bool isUgly(int num) { if (num == 0) return false; while (num % 2 == 0) { num = num/2; } while (num % 3 == 0) { num = num/3; } while (num % 5 == 0) { num = num/5; } return num == 1; } };
264. Ugly Number II
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, 12is the sequence of the first
10ugly
numbers.
Note that
1is typically treated as an ugly number, and n does
not exceed 1690.
利用set自动排序的功能。
class Solution { public: int nthUglyNumber(int n) { int count = 0; set<long long> se; se.insert(1); while (1) { long long key = *se.begin(); se.erase(key); if ( ++count == n) return key; se.insert(key * 2); se.insert(key * 3); se.insert(key * 5); } return -1; } };
还有另外一种方法,val2 val3 val5里面中最小的那个就是下一个填进去的值,填进去之后,index后移,index2 就是 下一个*2的候选值的下标
class Solution {
public:
int nthUglyNumber(int n)
{
vector<int> vc(n, 1);
int index2 = 0;
int index3 = 0;
int index5 = 0;
int val2 = 2;
int val3 = 3;
int val5 = 5;
for (int i = 1; i < n; i++)
{
int insert = min(val2, min(val3, val5));
vc[i] = insert;
if (insert == val2)
{
val2 = vc[++index2] * 2;
}
if (insert == val3)
{
val3 = vc[++index3] * 3;
}
if (insert == val5)
{
val5 = vc[++index5] * 5;
}
}
return vc[n - 1];
}
};
313. Super Ugly Number
Write a program to find the nth super ugly number.
Super ugly numbers are positive numbers whose all prime factors are in the given prime list
primesof
size
k. For example,
[1, 2, 4, 7, 8, 13, 14, 16, 19, 26, 28, 32]is the sequence of the first 12 super ugly numbers given
primes=
[2, 7, 13, 19]of size 4.
Note:
(1)
1is a super ugly number for any given
primes.
(2) The given numbers in
primesare in ascending order.
(3) 0 <
k≤ 100, 0 <
n≤
106, 0 <
primes[i]<
1000.
(4) The nth super ugly number is guaranteed to fit in a 32-bit signed integer.
方法和上面那个一模一样。
class Solution { public: int nthSuperUglyNumber(int n, vector<int>& primes) { vector<int> index(primes.size(), 0); //index[i] 存的是primes第i位应该和 index[i]相乘 vector<int> ret(n, 1); //存结果数组 vector<int> val(primes); for (int i = 1; i < n; i++) { ret[i] = *min_element(val.begin(), val.end()); for (int j = 0; j < val.size(); j++) { if (ret[i] == val[j]) { val[j] = ret[++index[j]] * primes[j]; } } } return ret[n - 1]; } };
相关文章推荐
- LeetCode(263) Ugly Number (264)Ugly Number II
- leetcode263_264:Ugly Number & Ugly Number II
- LeetCode - 263. Ugly Number & 264. Ugly Number II & 313. Super Ugly Number
- [leetcode 263 264]Ugly Number I II
- <LeetCode OJ> Ugly Number / Ugly Number II【263 / 264】
- 【LeetCode】(263)Ugly Number(Easy)
- leetcode 313. Super Ugly Number-超级丑数
- [LeetCode#264]Ugly Number II
- 【leetcode】313. Super Ugly Number【M】【14】
- 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 313. Super Ugly Number
- leetcode263: Ugly Number
- leetcode 313. Super Ugly Number 超级丑数
- LeetCode 313. Super Ugly Number
- [LeetCode] 264. Ugly Number II
- 263. Ugly Number LeetCode