• 0915 算法题


    0915 算法题

    重建二叉树

    左子树长度为i-1,左闭右开就是i,i为根节点在中序遍历中的位置。左子树确定后,右子树的下标位置也就确定了。
    root.left=reConstructBinaryTree(Arrays.copyOfRange(pre,1,1+i),Arrays.copyOfRange(vin,0,i));
    root.right=reConstructBinaryTree(Arrays.copyOfRange(pre,i+1,m),Arrays.copyOfRange(vin,i+1,n));

    import java.util.*;
    /**
     * Definition for binary tree
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    public class Solution {
        public TreeNode reConstructBinaryTree(int [] pre,int [] vin) {
            int m=pre.length;
            int n=vin.length;
            if(m==0 | n==0)
            {
                return null;
            }
            
            TreeNode root=new TreeNode(pre[0]);
            for(int i=0;i
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    判断是否为二叉搜索树树的合法后序遍历

    最后一个元素为根节点,找到右子树的起始点,若起始点之后全部大于根节点为true,反之为false。再递归检查左右子树。

    public boolean checkValid(int [] sequence)
    {
        if(sequence.length)
        {
            return true;
        }
        int root=sequence[sequence.length-1];
        for(int i=0;i=root)
            {
                break;
            }
        }
        for(int j=i;j
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    滑动窗口中位数 lt 480

    给定数组nums,以及大小为k的窗口,每次向右滑动1个数字,请给出窗口每次滑动后的中位数,以数组形式返回。
    通过一个大顶堆和小顶堆维护了窗口的中位数值,直接从堆中取出即可。

    class Solution {
        public double[] medianSlidingWindow(int[] nums, int k) {
            /*
            滑窗+对顶堆:
            我们创建两个堆left和right,其中left是大顶堆存储小的一半元素,right为小顶堆存储大的一半元素
            假定right存储的元素数目总是>=left存储的元素数目
            1.当窗口元素总数为奇数时:中位数为排序k/2的数字,此时直接right堆顶就是答案
            2.当窗口元素总数为偶数时:中位数为排序k/2与(k-1)/2的均值,此时将left堆顶与right堆顶取均值即可\
            还要注意的是:窗口滑动过程中我们加入与删除元素后记得调整堆使得堆平衡
             */
            int len = nums.length;
            int cnt = len - k + 1;  // 滑窗个数
            double[] res = new double[cnt];
            // Integer.compare(b, a)逻辑为:(x < y) ? -1 : ((x == y) ? 0 : 1) 只比较不会加减
            PriorityQueue left = new PriorityQueue<>((a, b) -> Integer.compare(b, a)); // 大顶堆(注意不要b-a防止溢出)
            PriorityQueue right = new PriorityQueue<>((a, b) -> Integer.compare(a, b)); // 小顶堆
            // 初始化堆:[0,k-1] 使得right>=left
            for (int i = 0; i < k; i++) {
                right.add(nums[i]);
            }
            for (int i = 0; i < k / 2; i++) {
                left.add(right.poll()); // 弹出最小的数字给left
            }
            // 首个中位数加入res
            res[0] = getMid(left, right);
            // 这里的i代表即将加入窗口的右端元素
            for (int i = k; i < len; i++) {
                int a = nums[i], b = nums[i - k];   // a为即将加入窗口的元素,b为即将退出窗口的元素
                if (a >= right.peek()) {
                    right.add(a);
                } else {
                    left.add(a);
                }
                if (b >= right.peek()) {
                    right.remove(b);
                } else {
                    left.remove(b);
                }
                // 调整堆
                adjust(left, right);
                // 该窗口中位数加入结果
                res[i - k + 1] = getMid(left, right);
            }
            return res;
        }
    
        // 调整堆使得堆平衡
        private void adjust(PriorityQueue left, PriorityQueue right) {
            while (left.size() > right.size()) right.add(left.poll());  // 左边比右边多,左边必定不符合条件,往右边搬
            while (right.size() > left.size() + 1) left.add(right.poll());  // 右边比左边多1以上,右边必定多了,往左边搬
        }
    
        // 根据left与right两个堆返回中位数
        private double getMid(PriorityQueue left, PriorityQueue right) {
            if (left.size() == right.size()) return left.peek() / 2.0 + right.peek() / 2.0; // 范围不知道防止溢出
            else return (double) right.peek();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58

    二叉树前序遍历

    import java.util.*;
    
    /*
     * public class TreeNode {
     *   int val = 0;
     *   TreeNode left = null;
     *   TreeNode right = null;
     *   public TreeNode(int val) {
     *     this.val = val;
     *   }
     * }
     */
    
    public class Solution {
        /**
         * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
         *
         * 
         * @param root TreeNode类 
         * @return int整型一维数组
         */
        
        LinkedList result=new LinkedList<>();
        public int[] preorderTraversal (TreeNode root) {
            // write code here
            answer(root);
            
            int[] res=new int[result.size()];
            for (int i = 0; i < result.size(); i++) {
                res[i]=result.get(i);
            }
            
            return res;
        }
        public void answer (TreeNode root) {
            if(root==null) return;
            result.add(root.val);
            answer(root.left);
            answer(root.right);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41

    任务分配所需的工人数

    给定二维数组nx2,表示n个任务,2用来表示开始时间和结束时间。例如[[1,3],[2,4]]
    一个工人同时只能处理一个任务,如图所示例子需要两个工人处理。

    #暴力法:先排序,然后遍历每个任务,查找是否有开始时间小于当前任务结束时间的任务,如果有则表示两者相交,统计出最大相交次数。时间复杂度为O(n^2)。
    #挑选:每次遍历取不相交的最大任务子集,多少次遍历取完就是需要多少个工人。
    
    • 1
    • 2

    给定数组,查找符合要求的数子集

    给定数组,查找三个数,使得他们互相相乘的和最大,输出最大值
    给定数组,查找n个数,使得他们互相相乘的和最大,输出这些数

    使用回溯法,这是一个子集树空间,第i层表示第i个元素取或者不取。当取到n个数,或者到达数组结尾则退出递归。
    
    • 1
  • 相关阅读:
    【图像处理:OpenCV-Python基础操作】
    基于51单片机和DHT11温湿度传感器的智能加湿器的设计与实现
    element -plus table的二次封装
    QT day2
    永久删除怎么恢复?小白适用
    shell编程
    Altera(Intel)时序约束文件SDC
    Python学习基础笔记八——字典
    常见编写JavaScript代码时容易出现的错误(3)
    dspe-peg-cy7.5;磷脂-聚乙二醇-CY7.5吲哚菁绿
  • 原文地址:https://blog.csdn.net/m0_50689246/article/details/126909662