263. Ugly Number
class Solution {
public:
bool isUgly(int num) {
int ungly_set[] = {2, 3, 5};
if(num <= 0)
return false;
while(num != 1) {
for(int i = 0; i <= 2; i++) {
if(!(num%ungly_set[i])) {
num = num/ungly_set[i];
break;
}
else if(i == 2)
return false;
}
}
return true;
}
};
258. Add Digits
典型的digit root的题目 digital root。
class Solution {
public:
int addDigits(int num) {
return 1 + (num - 1) % 9;
}
};
326. Power of Three
class Solution {
public:
bool isPowerOfThree(int n) {
if(n <= 0)
return false;
int f = floor(log10(n)/log10(3));
int c = ceil(log10(n)/log10(3));
return f == c;
}
};
class Solution {
public:
bool isPowerOfThree(int n) {
return fmod(log10(n)/log10(3), 1)==0;
}
};
231. Power of Two
class Solution {
public:
bool isPowerOfTwo(int n) {
return fmod(log10(n)/log10(2), 1) == 0;
}
};
class Solution {
public:
bool isPowerOfTwo(int n) {
return n > 0 && !(n&(n-1));
}
};
class Solution {
public:
bool isPowerOfTwo(int n) {
return!(n&n-1)*n>0;
}
};