adding leetcode problems
This commit is contained in:
24
leetcode/p263_uglynumber.cpp
Normal file
24
leetcode/p263_uglynumber.cpp
Normal file
@@ -0,0 +1,24 @@
|
||||
class Solution {
|
||||
public:
|
||||
// use a non-type parameter as the template parameter list.
|
||||
template <int F> // F represents factor, and it has to be constant
|
||||
// reduce factor n, has to be reference, because we want to change n.
|
||||
void reduceFactor(int& n){
|
||||
while(n%F==0){
|
||||
n = n/F;
|
||||
}
|
||||
}
|
||||
bool isUgly(int n) {
|
||||
// ugly number has to be a positive integer.
|
||||
if(n<=0){
|
||||
return false;
|
||||
}
|
||||
// reduce factor 2
|
||||
reduceFactor<2>(n);
|
||||
// reduce factor 3
|
||||
reduceFactor<3>(n);
|
||||
// reduce factor 5
|
||||
reduceFactor<5>(n);
|
||||
return (n==1);
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user