美文网首页
3个算法题目

3个算法题目

作者: 啊磊11 | 来源:发表于2021-03-21 17:23 被阅读0次

//二叉树展开为链表

public static void task37(TreeNode root){

if(root ==null){

return;

    }

task37(root.leftchild);

    task37(root.rightchild);

    TreeNode left = root.leftchild;

    TreeNode right = root.rightchild;

    root.leftchild =null;

    root.rightchild = left;

    TreeNode curr = root;

    while (curr !=null && curr.rightchild !=null){

curr = curr.rightchild;

    }

curr.rightchild = right;

}

//买卖股票的最佳时间

public static int task38(int[] nums){

int[][] dp =new int[nums.length][2];

    for(int i =0;i

if(i ==0){

dp[i][0] =0;

            dp[i][1] =0-nums[i];

continue;

        }

dp[i][0] = Math.max(dp[i-1][0], dp[i-1][1] + nums[i]);

        dp[i][1] = Math.max(dp[i-1][1], 0-nums[i]);

    }

return dp[nums.length-1][0];

}

//二叉树中的最大路径和

public static int max = Integer.MIN_VALUE;

public static int task39(TreeNode root){

if (root ==null){

return 0;

    }

int left = Math.max(task39(root.leftchild),0);

    int right = Math.max(task39(root.rightchild),0);

    max = Math.max(max, left + right + root.value);

    return Math.max(left,right) + root.value;

}

相关文章

  • 算法题目

    一、 分析: 一开始想多了,用了DFS回溯法,然后显示超出内存,可能是当n取值很大时递归太多的原因吧 然后又去搜索...

  • 算法题目

    ZERO 持续更新 请关注:https://zorkelvll.cn/blogs/zorkelvll/artic...

  • 算法题目

  • 图的结构 BFS DFS

    题目:BFS 一个队列,一个set集合 题目:DFS 题目:Kruskal算法 题目:Prim Dijkstra算法

  • 数据算法题目

    [单选题] 1000 个瓶子中有一瓶毒药,一只老鼠吃到毒药一周之内会死,如果要在一周之内检测出有毒药的一瓶,问至...

  • 算法题目集

    1、来自网易:为了找到自己满意的工作,牛牛收集了每种工作的难度和报酬。牛牛选工作的标准是在难度不超过自身能力值的情...

  • 基础算法题目

    爬楼梯 编辑距离 小顶堆 topk 快排序 二分查找 斐波那契数列 两数之和 最大回溯 题目形式:有一个数组,求其...

  • 2019算法题目

    讲完了基本的算法和数据结构之后,准备深入地讲解一下。 https://blog.csdn.net/qq_41681...

  • 算法设计题目

    括号匹配问题 假设表达式中运行包含两种括号:圆括号和方括号,其嵌套顺序随意。即()或者[([][])]都是正确的,...

  • 新手算法题目

    数组 Array力扣 485最大连续1的个数 | Max Consecutive One力扣 283 移动零 |...

网友评论

      本文标题:3个算法题目

      本文链接:https://www.haomeiwen.com/subject/hzqocltx.html