"LeetCode Zero Basic Guide" (Lesson 10) Simple recursion

172. Zero after factorial

Calculate factorial

Calculate the factorial of n directly. One of the main issues to consider in this calculation is that there must be overflow. The way we deal with it is to first see if there is a 0 at the end of it. If there is a 0, first remove the 0 and add one to the answer number, and then keep only the last few digits of the result of the factorial, so that it will not affect the subsequent calculation and will not overflow.

class Solution {
    
    
public:
    int trailingZeroes(int n) {
    
    
        int ans=0,res=1;
        for(int i=1;i<=n;i++) {
    
    
            res*=i;
            while(res%10==0&&res!=1) res/=10,ans++;
            res%=100000;
        }
        return ans;
    }
};

insert image description here

looking for factor 5

The final result has a 0 at the end, which is a factor of 10. By decomposing the prime factors again, you can find that there are two factors, 2 and 5, and the smaller one is the number of 10. Then, if you observe another step, you can find that the number of factors of 2 must be much more than the factor of 5, so the problem is transformed into finding the number of 5.

class Solution {
    
    
public:
    int trailingZeroes(int n) {
    
    
        int ans=0;
        for(int i=1;i<=n;i++) {
    
    
            int j=i;
            while(j%5==0) {
    
    
                ans++;
                j/=5;
            }
        }
        return ans;
    }
};

insert image description here

The recursive version looks for factor 5

class Solution {
    
    
public:
    int trailingZeroes(int n) {
    
    
        if(n==0) return 0;
        int sum=0,m=n;
        while(m%5==0) m/=5,sum++;
        return sum+trailingZeroes(n-1); 
    }
};

insert image description here

1342. Number of operations to change numbers to 0

simulation

Simulate according to the meaning of the question

class Solution {
    
    
public:
    int numberOfSteps(int num) {
    
    
        int ans=0;
        while(num) {
    
    
            if(num%2) num--;
            else num/=2;
            ans++;
        }
        return ans;
    }
};

recursive version

class Solution {
    
    
public:
    int numberOfSteps(int num) {
    
    
        if(num==0) return 0;
        if(num%2) return 1+numberOfSteps(num-1);
        return 1+numberOfSteps(num/2);
    }
};

insert image description here

222. The number of nodes in a complete binary tree

recursion

The number of nodes is equal to the root node plus the number of children of the left child plus the number of children of the right child

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
    
    
public:
    int countNodes(TreeNode* root) {
    
    
        if(root==nullptr) return 0;
        return countNodes(root->left)+countNodes(root->right)+1;
    }
};

insert image description here

LCP 44. Opening Ceremony Fireworks

dfs

The main idea of ​​the title is to give you a tree and let you look for different values ​​in it. We can find the difference, it is just a hash table to store the number of occurrences. Because the value of this question is given in the form of a tree, we add a tree traversal and search directly here.

/**
 * 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:
    unordered_map<int,int>mp;
    void dfs(TreeNode *root) {
    
    
        if(root==NULL) return;
        mp[root->val]++;
        dfs(root->left),dfs(root->right);
    }
    int numColor(TreeNode* root) {
    
    
        mp.clear();
        dfs(root);
        return mp.size();
    }
};

insert image description here

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=324034381&siteId=291194637