一个数末尾有多少零的个数(
)取决于
能分出多少个10
每有一个 10 后面就有几个零 又因为
所以又等价于找出有多少对 5
所以答案就是
class Solution {
public:
int trailingZeroes(int n) {
int res = 0;
while(n) res += n / 5,n /= 5;
return res;
}
};
class Solution {
public:
int numberOfSteps(int num) {
int ans = 0;
while(num)
{
if(1 & num) num -= 1;
else num >>= 1;
ans ++;
}
return ans;
}
};
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int countNodes(TreeNode* root) {
if (!root) return 0;
auto l = root->left, r = root->right;
int x = 1, y = 1;
while (l) l = l->left, x ++ ;
while (r) r = r->right, y ++ ;
if (x == y) return (1 << x) - 1;
return countNodes(root->left) + 1 + countNodes(root->right);
}
};
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int> a = vector<int>(1001,0);
void dg(TreeNode* root){
if(root){
a[root->val] = 1;
if(root->left){
dg(root->left);
}
if(root->right){
dg(root->right);
}
}
}
int numColor(TreeNode* root) {
dg(root);
int sum = 0;
for(int i = 0; i < a.size(); i++){
if(a[i]){
sum++;
}
}
return sum;
}
};