【数据结构算法(一)】递归篇(常见实例讲解)

🌈键盘敲烂,年薪30万🌈

本篇讲解实例:

  • 斐波那契、兔子问题、猴子吃桃问题、跳台阶问题、汉诺塔、杨辉三角

用到的递归思想:

  • 无记忆递归、记忆递归(重点掌握)

目录

一、斐波那契:

①无记忆多路递归:

②⭐记忆递归:

二、兔子问题:

三、跳台阶问题:

四、汉诺塔问题:

五:杨辉三角问题:

①无记忆递归:

②⭐记忆递归:

六、猴子吃桃问题:


一、斐波那契:

问题描述:

这个数列的每个数字都是前两个数字之和,数列的第一个和第二个数规定为1

①无记忆多路递归:
  • 时间复杂度:O(n^2) -  很恐怖
public class FibonaciNoMemory {
    // 1 1 2 3 5 8 13 21 34 55……
    public static void main(String[] args) {
        int n = 10;
        //无记忆性的递归
        int ans2 = noMemoryRecursion(n);
        System.out.println(ans2);

    }

    private static int noMemoryRecursion(int n) {
        if(n == 1 || n == 2){
            return 1;
        }
        return noMemoryRecursion(n-1) + noMemoryRecursion(n-2);

    }
}
②⭐记忆递归:
  • 时间复杂度:O(n)
public class FibonaciRemind {
    public static void main(String[] args) {
        int n = 10;
        int ans = remindRecursion(n);
        System.out.println(ans);
    }
    private static int remindRecursion(int n) {
        int[] cache = new int[n+1];
        Arrays.fill(cache, -1);
        cache[0] = 1; cache[1] = 1;
        return help(n-1, cache);
    }

    private static int help(int n, int[] cache) {
        if(cache[n] != -1){
            return cache[n];
        }
        int val = help(n-1, cache) + help(n-2, cache);
        cache[n] = val;
        return val;
    }
}

 

二、兔子问题:

问题描述:

有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?

代码同斐波那契差不多,多了个求和,这个兔子问题就是列昂纳多·斐波那契引申出的。

public class a06_rabbit {
    public static void main(String[] args) {
        int month = 10;
        int count = getCount(month);
        System.out.printf("第十个月,共%d只兔子", count);
    }

    private static int getCount(int month) {
        int[] cache = new int[month];

        cache[0] =  1;cache[1] = 1;

        help(month-1, cache);
        int total = 1;
        for (int i = 0; i < month; i++) {
            total += cache[i];
        }
        return total;
    }

    private static int help(int month, int[] cache) {
        if(cache[month] != 0){
            return cache[month];
        }
        cache[month] = help(month - 1, cache) + help(month - 2, cache);
        return cache[month];
    }
}

 

三、跳台阶问题:

问题描述:

鸡哥跳台阶,有时跳一阶,有时跳二阶,问,若有10层台阶,有多少种跳法

public class SkipStairs {
    public static void main(String[] args) {
        int n = 10;
        int ans = getCount(n);
        System.out.printf("共有%d种跳法", ans);
    }

    private static int getCount(int n) {
        return help(n);
    }

    private static int help(int n) {
        if(n == 1){
            return 1;
        }
        if(n == 2){
            return 2;
        }
        return help(n-1) + help(n-2);

    }
}

 

四、汉诺塔问题:

问题描述:

有三根柱子,编号为A、B、C,开始时在柱子A上有一些个圆盘,它们按照从下到上的顺序递增(最下面的最大,最上面的最小)。现在要将这些圆盘从柱子A移动到柱子C,中间可以借助柱子B,但有一些规则需要遵守:

  1. 每次只能移动一个圆盘。
  2. 移动过程中,大圆盘不能放在小圆盘上面。
public class Demo1 {
    static LinkedList<Integer> a = new LinkedList<>();
    static LinkedList<Integer> b = new LinkedList<>();
    static LinkedList<Integer> c = new LinkedList<>();
    public static void main(String[] args) {
        a.addLast(3);
        a.addLast(2);
        a.addLast(1);
        move(3, a, b, c);

    }
    private static void move(int n, LinkedList<Integer> a, LinkedList<Integer> b, LinkedList<Integer> c) {
        if(n == 0){
            return;
        }
        //转移n-1个到b - 要借助c
        move(n-1, a, c, b);
        //将最大的移到C
        c.add(a.removeLast());
        myPrint();
        //将n-1个到c - 要借助a
        move(n-1, b, a, c);
    }
    private static void myPrint() {
        System.out.println(a);
        System.out.println(b);
        System.out.println(c);
        System.out.println("===============");
    }
}

 

五:杨辉三角问题:

问题描述:有个三角形,每一行的该数等于上一行同列数+上一行前一列的数

①无记忆递归:
public class Demo2 {
    public static void main(String[] args) {
        int n = 6;
        print(n);
    }

    private static void printSpace(int n){
        for (int i = 0; i < n; i++) {
            System.out.print(" ");
        }
    }

    private static void print(int n) {
        for (int i = 0; i < n; i++) {
            printSpace((n-i-1)*2);
            for (int j = 0; j <= i; j++) {
                System.out.printf("%-4d", getElement(i, j));
            }
            System.out.println();
        }
    }

    private static int getElement(int row, int col){
        if(col == 0 || col == row){
            return 1;
        }
        return getElement(row-1, col-1) + getElement(row-1, col);

    }
}
②⭐记忆递归:
public class Demo1 {
    public static void main(String[] args) {
        int n = 6;
        print(n);
    }

    private static void printSpace(int n){
        for (int i = 0; i < n; i++) {
            System.out.print(" ");
        }
    }

    private static void print(int n) {
        int[][] cache = new int[n][];
        for (int i = 0; i < n; i++) {
            printSpace((n-i-1)*2);
            cache[i] = new int[i+1];
            for (int j = 0; j <= i; j++) {
                System.out.printf("%-4d", getElement(cache, i, j));
            }
            System.out.println();
        }
    }

    private static int getElement(int[][] cache, int row, int col){
        if(cache[row][col] > 0){
            return cache[row][col];
        }

        if(col == 0 || col == row){
            cache[row][col] = 1;
            return 1;
        }
        cache[row][col] = getElement(cache, row-1, col-1) + getElement(cache, row-1, col);
        return cache[row][col];

    }
}

 

六、猴子吃桃问题:

问题描述:

有一只猴子摘了一堆桃子,第一天它吃了其中的一半,并再多吃了一个;第二天它又吃了剩下的桃子的一半,并再多吃了一个;以后每天都吃了前一天剩下的一半并再多吃了一个。到第n天想再吃时,发现只剩下一个桃子。问这堆桃子原来有多少个?

public class MonkeyEatPeach {

    public static void main(String[] args) {
        int days = 9; // 假设猴子在第9天时发现只剩下一个桃子

        // 调用计算桃子数量的方法
        int result = calculatePeaches(days);

        // 输出结果
        System.out.println("猴子摘的桃子总数为:" + result);
    }

    // 计算桃子数量的方法
    public static int calculatePeaches(int days) {
        if(days == 1){
            return 1;
        }
        return (calculatePeaches(days - 1) + 1) * 2;
    }
}

  • 118
    点赞
  • 95
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 99
    评论
数据结构算法Python讲义》是一本介绍数据结构算法的教材,它使用Python语言作为教学工具,旨在帮助读者理解和掌握这两个重要的计算机科学领域。 这本讲义首先介绍了数据结构的概念和基本知识,如数组、链表、栈、队列和树等。对于每种数据结构,讲义都详细说明了其定义、特点和常见操作,并通过实例和代码演示了它们的使用方法。此外,讲义还探讨了如何选择合适的数据结构来解决实际问题,并讨论了不同数据结构之间的比较和权衡。 在介绍完数据结构后,讲义转向算法讲解。它首先讲解算法的基本概念和特性,如时间复杂度和空间复杂度,然后深入讲解常见算法设计技巧,如归、分治法、贪心算法和动态规划。对于每种算法,讲义都给出了详细的原理解释和代码实现,并通过实例和练习题帮助读者理解和掌握。 此外,讲义还包含了一些高级主题,如图算法、排序算法和搜索算法。它详细讲解了图的表示方式和常见的图算法,如深度优先搜索和广度优先搜索。对于排序算法,讲义介绍了常见的排序算法,如冒泡排序、插入排序和快速排序,并给出了它们的实现代码。此外,讲义还探讨了搜索算法,如二分搜索和回溯算法,并通过实例说明它们的应用。 总的来说,《数据结构算法Python讲义》通过简洁明了的语言和丰富的实例,帮助读者理解和掌握数据结构算法的基本概念和技巧。无论是初学者还是有一定基础的读者,都可以从中受益,提高编程能力。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

不会就选C.

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

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

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

打赏作者

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

抵扣说明:

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

余额充值