adding leetcode problems

This commit is contained in:
Jidong Xiao
2023-09-26 12:19:09 -04:00
parent b4a5e201ee
commit 6eb5eb10a1
3 changed files with 46 additions and 0 deletions

View 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);
}
};