• 数据结构(递归,链表实现递归)


    a.宏观描述:本质上说,递归将原问题转化为更小的同一问题。

    b.递归本身也是一个函数,来完成某一功能。

    1.递归终止的条件

    2.递归操作

    1.猴子吃桃问题

    猴子第一天偷吃了该堆桃子的一半,觉得好吃多吃了一个;第二天吃了该堆桃子的一半,觉得好吃多吃了又一个,...  ,到了第四天,只剩下了一个桃子,问猴子一个偷吃了多少桃子?

    一般做法:

    1. package com.ffyc.lesson.deliver;
    2. //猴子吃桃问题
    3. public class MonkeyEatPeach {
    4. public int eatPeach1(){
    5. return (eatPeach2()+1)*2;
    6. }
    7. public int eatPeach2(){
    8. return (eatPeach3()+1)*2;
    9. }
    10. public int eatPeach3(){
    11. return (eatPeach4()+1)*2;
    12. }
    13. public int eatPeach4(){
    14. return 1;
    15. }
    16. public static void main(String[] args) {
    17. MonkeyEatPeach monkeyEatPeach=new MonkeyEatPeach();
    18. //int result =monkeyEatPeach.eatPeach1();
    19. int result =monkeyEatPeach.eatPeach();
    20. System.out.println(result);
    21. }
    22. }

    递归做法:

    1. package com.ffyc.lesson.deliver;
    2. //猴子吃桃问题
    3. public class MonkeyEatPeach {
    4. //递归方法
    5. public int eatPeach(int days){
    6. //递归终止条件
    7. if(days==1){
    8. return 1;
    9. }
    10. //递归操作
    11. return (eatPeach(days-1)+1)*2;
    12. }
    13. public static void main(String[] args) {
    14. MonkeyEatPeach monkeyEatPeach=new MonkeyEatPeach();
    15. //int result =monkeyEatPeach.eatPeach1();
    16. int result =monkeyEatPeach.eatPeach(4);
    17. System.out.println(result);
    18. }
    19. }

    2.数组求和问题

    一般做法:

    1. public class SumArr {
    2. public int sum(int[] arr){
    3. int sum=0;
    4. //对入参进行判断
    5. if(arr==null){
    6. return sum;
    7. }
    8. int length=arr.length;
    9. for(int i=0;i
    10. sum+=arr[i];
    11. }
    12. return sum;
    13. }
    14. public static void main(String[] args) {
    15. int[] arr={1,3,5,7,9};
    16. SumArr sumArr=new SumArr();
    17. System.out.println(sumArr.sum(arr));
    18. }
    19. }

    递归做法:

    1. public class SumArr {
    2. public int sum(int[] arr){
    3. //对入参进行判断
    4. if(arr==null){
    5. return 0;
    6. }
    7. return sumDG(arr,0);//求整个数组的和,其左边界是0
    8. }
    9. /**
    10. *
    11. * @param arr
    12. * @param l 表示所求数组的左边界
    13. * @return
    14. */
    15. public int sumDG(int[] arr,int l){
    16. //1.递归中终止条件
    17. if(l==arr.length){
    18. return 0;
    19. }
    20. //2.递归操作
    21. return arr[l]+sumDG(arr,l+1);
    22. }
    23. public static void main(String[] args) {
    24. int[] arr={1,3,5,7,9};
    25. SumArr sumArr=new SumArr();
    26. System.out.println(sumArr.sum(arr));
    27. }
    28. }

    3.链表实现递归

    链表具有天然的递归结构。

    将下面列表从0之后断开,则:

    链表中删除元素问题

    1. public void deleteNode(T val){
    2. /*
    3. 此处代码在递归方法中可实现
    4. //入参判断
    5. if(this.header==null){
    6. return;
    7. }
    8. */
    9. //递归操作
    10. this.header=deleteNode(this.header,val);
    11. }
    12. //递归方法(从以node为头结点的链表中删除元素val)
    13. public Node deleteNode(Node node ,T val){
    14. //递归终止的条件
    15. if(node==null){
    16. return null;
    17. }
    18. //递归操作
    19. Node e=node;
    20. Node nextHeader=node.next;
    21. if(e.val==val){
    22. return nextHeader;
    23. }else{
    24. e.next=deleteNode(nextHeader,val);
    25. return e;
    26. }
    27. }

    4.链表的旋转

    一般做法:

    1. public class Solution206 {
    2. public class ListNode {
    3. int val;
    4. ListNode next;
    5. ListNode() {
    6. }
    7. ListNode(int val) {
    8. this.val = val;
    9. }
    10. ListNode(int val,ListNode next) {
    11. this.val = val;
    12. this.next = next;
    13. }
    14. }
    15. public ListNode reverseList(ListNode head){
    16. //一般做法:
    17. ListNode pre=null;
    18. ListNode cur=head;
    19. while(cur!=null){
    20. //先保存cur的下一个结点
    21. ListNode next=cur.next;
    22. cur.next=pre;
    23. pre=cur;
    24. cur=next;
    25. }
    26. //此时pre指向的是新链表的头结点
    27. return pre;
    28. }
    29. }

  • 相关阅读:
    el-checkbox复选框如何修改尺寸大小
    音视频入门基础:AAC专题(6)——FFmpeg源码中解码ADTS格式的AAC的Header的实现
    优化|优化处理可再生希尔伯特核空间的非参数回归中的协变量偏移
    CDH集成的kerberos迁移实战
    【python】基础语法
    Apache APISIX不编写任何代码的情况下,简单实现一个 API 实践
    设计模式——模板方法
    2023NOIP A层联测9 紫罗兰
    【java8】函数式接口
    食品制造业SCM系统供应商管理模块提升企业采购管理效率,数字化升级供应链
  • 原文地址:https://blog.csdn.net/qq_69032468/article/details/133715243