【2022-04-15】网易互娱机试题

本文介绍如何利用中序遍历和后序遍历数组重建二叉树,进而求解树的直径问题,并探讨如何通过自定义LRU缓存实现查询命中次数的统计。通过这两个实际问题,展示了算法在数据结构和计算机内存管理中的应用。

1. 输入树的中序遍历和后续遍历数组,求出树的直径。

在这里插入图片描述
题解:

import java.util.*;

public class Main {
    static int ans = 1;
    public static void main(String[] args) {
        // 中序遍历数组以及后序遍历数组
        int[] inorder = {4,2,1,5,7,3,6};
        int[] postorder = {4,2,7,5,6,3,1};
        // 根据数组构建完整树
        Solution solution = new Solution();
        TreeNode root = solution.buildTree(inorder, postorder);
        // 获得树的直径
        GetLen getLen = new GetLen(root);
        int len = getLen.getLen();
        // 打印输出
        System.out.println(len);
    }
}

// 树的直径类:LeetCode.543. 二叉树的直径
class GetLen{
    TreeNode root;
    int ans = 1;

    public GetLen(){}
    public GetLen(TreeNode root){
        this.root = root;
    }
    public int getLen(){
        dfs(root);
        return ans - 1;
    }
    public int dfs(TreeNode root){
        if (root == null)   return 0;
        int left = dfs(root.left);
        int right = dfs(root.right);
        ans = Math.max(ans, left+right+1);
        return Math.max(left, right) +1;
    }
}

// 构建树的类:LeetCode.106. 从中序与后序遍历序列构造二叉树
class Solution {
    Map<Integer, Integer> map;

    public TreeNode buildTree(int[] inorder, int[] postorder) {
        int len = inorder.length;
        map = new HashMap<>();
        for(int i = 0; i < len; i++){
            map.put(inorder[i], i);
        }
        TreeNode root = myBuilderTree(inorder, postorder, 0, len-1, 0, len-1);
        return root;
    }
    public TreeNode myBuilderTree(int[] inorder, int[] postorder, int inorder_left, int inorder_right, int postorder_left, int postorder_right){
        if(postorder_left > postorder_right){
            return null;
        }
        int rootIndex = map.get(postorder[postorder_right]);
        int leftsubTree_size = rootIndex - inorder_left;
        TreeNode root = new TreeNode(inorder[rootIndex]);
        root.left = myBuilderTree(inorder, postorder, inorder_left, rootIndex-1, postorder_left, postorder_left+leftsubTree_size-1);
        root.right = myBuilderTree(inorder, postorder, rootIndex+1, inorder_right, postorder_left+leftsubTree_size, postorder_right-1);
        return root;
    }
}

// 树节点的定义类
class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode() {}
    TreeNode(int val) { this.val = val; }
    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}

2. 查询缓存命中次数

在这里插入图片描述
题解:

import java.util.*;

public class Main2 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        String[] strs = sc.nextLine().split(",");
        int len = strs.length;
        // 去除输入的'['、']'符号
        strs[0] = strs[0].substring(1);
        strs[len-2] = strs[len-2].substring(0,strs[len-2].length()-1);
        // 得到输入数组和缓存容量
        int[] R = new int[len-1];
        for (int i = 0; i < len - 1; i++) {
            R[i] = Integer.parseInt(strs[i]);
        }
        int N = Integer.parseInt(strs[len-1]);
        // 调用官方给的方法
        stat_hit_count(R, N);
    }

    public static int stat_hit_count (int[] R, int N) {
        // write code here
        LRU_Cache lru = new LRU_Cache(N);
        for (int i = 0; i < R.length; i++) {
            lru.put(R[i], 1);
        }
        return lru.shotCount;
    }
}

// 手写LRU缓存
class LRU_Cache{
    // 记录命中次数
    int shotCount;
    // LRU的常用成员变量
    int capacity;
    int size;
    DLinkedList head;
    DLinkedList tail;
    Map<Integer, DLinkedList> map = new HashMap<>();

    public LRU_Cache(){}
    public LRU_Cache(int capacity){
        this.capacity = capacity;
        shotCount = 0;
        size = 0;
        head = new DLinkedList();
        tail = new DLinkedList();
        head.next = tail;
        tail.next = head;
    }
    public int get(int key){
        DLinkedList node = map.get(key);
        if(node == null){
            return -1;
        }else{
            removeNode(node);
            addToHead(node);
            return node.val;
        }
    }
    public void put(int key, int value){
        DLinkedList node = map.get(key);
        if(node != null){
            shotCount++;
            //System.out.println("添加失败");
            node.val = value;
            removeNode(node);
            addToHead(node);
        }else{
            DLinkedList newNode = new DLinkedList(key, value);
            map.put(key, newNode);
            addToHead(newNode);
            //System.out.println("添加成功");
            size++;
            if(size > capacity){
                DLinkedList delNode = tail.prev;
                removeNode(delNode);
                map.remove(delNode.key);
                size--;
            }
        }
    }

    public void removeNode(DLinkedList node){
        node.prev.next = node.next;
        node.next.prev = node.prev;
    }
    public void addToHead(DLinkedList node){
        node.next = head.next;
        node.prev = head;
        head.next.prev = node;
        head.next = node;
    }
}

// 自定义双向链表类
class DLinkedList{
    int key;
    int val;
    DLinkedList next;
    DLinkedList prev;
    public DLinkedList(){}
    public DLinkedList(int key, int val){
        this.key = key;
        this.val = val;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值