《九日集训》第十五轮 (第九讲) 简单递归

本文主要探讨了递归编程的概念和注意事项,并通过分析LeetCode中的多个题目,如阶乘尾部零、数值归零步数等,展示了递归在解决实际问题中的应用。同时,文中强调了记忆化技术在减少重复计算中的作用,以及在递归过程中正确设置终止条件的重要性。
摘要由CSDN通过智能技术生成

前言

记录几个面试常见题型

一、知识点

递归,先列出所有情况,再处理
记忆化,可以减少重复列出的
递归注意终止条件

二、题目

1、https://leetcode-cn.com/problems/factorial-trailing-zeroes/submissions/

1.1 分析
阶乘求尾零,还有指数求

1.2代码

class Solution {
public:
    int trailingZeroes(int n) {

        if(n < 5)
        {
            return 0;
        }

        return n/5 + trailingZeroes(n / 5);

    }
};

2、https://leetcode-cn.com/problems/number-of-steps-to-reduce-a-number-to-zero/
2.1题目分析
读懂题目
2.2代码


class Solution {
public:
    int numberOfSteps(int num) {

        if(num == 0)
        {
            return 0;
        }

        if(num % 2 == 1)
        {
            return numberOfSteps(num - 1) + 1;

        }else{

            return numberOfSteps(num / 2) + 1;
        }

    }
};

3https://leetcode-cn.com/problems/count-complete-tree-nodes/submissions/
3.1题目分析
先找总的情况
3.2代码

class Solution {
public:
    int countNodes(TreeNode* root) {

        if(root == nullptr) return 0;

        return countNodes(root -> left) + 1 +countNodes(root -> right);

    }
};

4https://leetcode-cn.com/problems/sZ59z6/
4.1题目分析
先根据二叉树性质遍历,然后寻找结果
4.2代码

/**
 * 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;
    }
};

 

5https://leetcode-cn.com/problems/integer-replacement/submissions/
5.1题目分析
两种情况合并的一步,记忆搜索法,避免一些重复探索
5.2代码

class Solution {
private:
    unordered_map<int, int> memo;

public:
    int integerReplacement(int n) {
        if (n == 1) {
            return 0;
        }
        if (memo.count(n)) {
            return memo[n];
        }
        if (n % 2 == 0) {
            return memo[n] = 1 + integerReplacement(n / 2);
        }
        return memo[n] = 2 + min(integerReplacement(n / 2), integerReplacement(n/ 2 + 1));
    }
};

6https://leetcode-cn.com/problems/range-sum-of-bst/submissions/
6.1题目分析
递归,深搜,分情况
6.2代码

class Solution {
public:
    int rangeSumBST(TreeNode *root, int low, int high) {
        if (root == nullptr) {
            return 0;
        }
        if (root->val > high) {
            return rangeSumBST(root->left, low, high);
        }
        if (root->val < low) {
            return rangeSumBST(root->right, low, high);
        }
        return root->val + rangeSumBST(root->left, low, high) + rangeSumBST(root->right, low, high);
    }
};


7https://leetcode-cn.com/problems/er-cha-shu-de-shen-du-lcof/submissions/
7.1题目分析
求深度和结点数有点类似
7.2代码

class Solution {
public:
    int maxDepth(TreeNode* root) {


        if(root == NULL)
            return 0;

        return max(maxDepth(root -> left) , maxDepth(root -> right)) + 1;

    }
};

8 https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/
8.1题目分析
同上题
8.2代码

class Solution {
public:
    int maxDepth(TreeNode* root) {


        if(root == NULL)
            return 0;

        return max(maxDepth(root -> left) , maxDepth(root -> right)) + 1;

    }
};

9https://leetcode-cn.com/problems/invert-binary-tree/submissions/
9.1题目分析
如何递下去重复一个情况, 找到终止条件
9.2代码

class Solution {
public:
    TreeNode* invertTree(TreeNode* root) {
        if (root == nullptr) {
            return nullptr;
        }
        TreeNode* left = invertTree(root->left);
        TreeNode* right = invertTree(root->right);
        root->left = right;
        root->right = left;
        return root;
    }
};

10https://leetcode-cn.com/problems/bP4bmD/submissions/
10.1题目分析
dfs ,终止条件,初始条件
10.2代码

class Solution {
private:
    void dfs(vector<vector<int>> &graph , vector<vector<int>>& ans, vector<int>& tmp , int i  , int last)
    {
         if (i == last){
            ans.push_back(tmp);
            return;
        }
        for (auto n : graph[i]){
            tmp.push_back(n);
            dfs(graph, ans, tmp, n, last);
            tmp.pop_back();
        }

    }
public:
    vector<vector<int>> allPathsSourceTarget(vector<vector<int>>& graph) {
        vector<vector<int>> ans;
        vector<int> tmp;
		tmp.push_back(0);
		int lastNode = graph.size() - 1;
		dfs(graph, ans, tmp, 0, lastNode);
		return ans;	

    }

};
class Solution {
public:
    int n;
    vector<vector<int>> g;
    vector<vector<int>> ans;
    vector<int> path;

    void dfs(int u) {
        path.push_back(u);
        if (u == n - 1) ans.push_back(path);
        for (auto v: g[u]) dfs(v);
        path.pop_back();
    }

    vector<vector<int>> allPathsSourceTarget(vector<vector<int>>& graph) {
        g = graph;
        n = g.size();
        dfs(0);
        return ans;
    }
};

三、做题记录

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值