[学习报告]《LeetCode零基础指南》(第9讲)简单递归

[学习报告]《LeetCode零基础指南》(第9讲)简单递归

学习内容:https://blog.csdn.net/WhereIsHeroFrom/article/details/120875679

一、今日知识点总结

函数调用自己,要有跳出条件判断

什么是简单递归,可以干嘛?

新收小白的一道坎 - 递归

含义:函数内调用函数自身(函数自己调自己)

记住主要三点

  1. 实现一个函数,这个函数会调用自己。每次调用,函数传参不一样
  2. 递归要有出口,即满足一定条件后需要return,否则可能出现 死递归==引起栈溢出
  3. 根据递推式来补充你的递归调用内容

要注意递归出口的判断及递推关系

二、今日解题

image-20220322225738377

172. 阶乘后的零

难度中等

//突破点:制造10的情况: 
//5! = 5*4*3*2*1,有1个0,制造0的组合 5*2=10
//10!= 10*9*8*7*6*5*4*3*2*1 有2个0 ==> 5*2 , 10 = 5*2
// 由此看出,计算末尾有多少个0,其实是找出隐含多少个5*2,也就是找出多少个因子为5和因子为2的
// 且因子为2的必然比因子为5的多,则只要出现多少个为5的因子,即可组合出多少个10



//阶乘递推式
/*
unsigned long long jiecheng(n){
    if(n<=1){
        return 1;
    }
    return n*jiecheng(n-1);
}
*/

int trailingZeroes(int n){
    
    int cnt = 0;
    for(int i =1;i<=n;i++){
        int k = i;
        while(k>0){
            if(k%5 == 0){//有一个因子=5
                cnt++;
                k /= 5 ;
            }else{
                break;
            }
        }
    }
    
    return cnt;
}
//以下解法:算出阶乘后,再模10判断多少个0,阶乘计算很快就溢出了

unsigned long long jiecheng(n){
    if(n<=1){
        return 1;
    }
    return n*jiecheng(n-1);
}

int trailingZeroes(int n){
    unsigned long long r = jiecheng(n);
    int cnt = 0;
    while(1){
        
        if(r%10 == 0){
            cnt++;
            r /=10;
        }else{
            break;
        }
    }
    return cnt;
}
342. 将数字变成 0 的操作次数

因为是对一个数的反复操作,使用递归

//解法一
void step(int n,int *cnt){
    if(n == 0){
        return;
    }
    if(n&1){
        //奇数
        n--;
    }else{
        //偶数
        n /= 2;
    }
    (*cnt)++;
    step(n,cnt);
}


int numberOfSteps(int num){

    int cnt = 0;
    step(num,&cnt);
    return cnt;

}
//解法二,递归中通过返回 1+递归函数的方式,合计操作次数
int numberOfSteps(int num){

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

    if(num&1){
        num--;
    }else{
        num /= 2;
    }

   return 1+numberOfSteps(num);

}
222. 完全二叉树的节点个数

二叉树的每一个节点,有一个父节点和两个子节点。每个子节点也可能有两个子节点

父节点数量=1,左子节点若有,则为1+子节点数;右子节点若有,则有1+子节点数

cnt = 1 + 两个子节点的子节点 == 1 +f(left) + f(right)

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */


int count(struct TreeNode *root){
    if(root == NULL){
        return 0;
    }

    return count(root->left) + count(root->right) + 1;
}

int countNodes(struct TreeNode* root){

  
 return count(root);

}
LCP 44. 开幕式焰火

定义一个数组,记录出现过颜色种类,遍历二叉树,检查每个节点的值是否已经在该数组中,若不再则加入到该数组中。数组的长度就是颜色的种类

遍历二叉树,使用递归,递归函数中,若节点为空,则该节点不需要再递归下去。

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */

void count(struct TreeNode* root,int *colors,int *cnt){
    if(root == NULL){
        return;
    }
    bool b = false;
    for(int i = 0;i<*cnt;i++){
        if(root->val == colors[i]){
            b = true;
            break;
        }
    }
    if(!b){
        colors[*cnt] = root->val;
        (*cnt)++;
    }

    count(root->left,colors,cnt);
    count(root->right,colors,cnt);

}

int numColor(struct TreeNode* root){

    int *colors = (int *)malloc( sizeof(int) * 10001);
    int rSize = 0;
    count(root,colors,&rSize);
    return rSize;

}

三、今日收获

使用递归解决二叉树的遍历

四、今日疑问

五、其他参考

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

忘词木头人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值