牛客网编程巅峰挑战赛青铜白银黄金组第3场

哎,就只会做一题,我好菜啊!
第一题:

public class Solution {
    public int Minimumdays (int n, int[] DEF) {
        // write code here
        Arrays.sort(DEF);
        int currDay = 0;
        for(int i = 0; i < n; ++i){
            if(currDay >= DEF[i]){
                currDay++;
            }else{
                currDay = DEF[i] + 1;
            }
        }
        return currDay - 1;
    }
}

第二题:快速幂

public class Solution {
    /**
     * 返回c[n]%1000000007的值
     * @param n long长整型 即题目中的n
     * @return int整型
     */
    public int Answerforcn (long n) {
        // write code here
        // return 14 * pow(15, n)
        long MOD = 1000000007L;
        long x = 14L;
        long y = 15L;
        long res = 1;
        --n;
        while(n > 0){
            if((n & 1) == 1){
                res = (res * y ) % MOD; 
            }
            y = y * y % MOD;
            n >>= 1;
        }
        return (int)((14 * res) % MOD);
    }
}

第三题: 先BFS构建树结构,再DFS赋值并计算每个节点与其父节点的异或和,但是使用节点对象重建树结构会超内存!

import java.util.*;

public class Solution {

    private class TreeNode{
        public int val;
        public TreeNode[] children;
        public TreeNode(int k, int v){
            val = v;
            children = new TreeNode[k];
        }
    }

    private TreeNode bfs(int k, int[] a){
        // create tree;
        if(a == null){return null;}
        int len = a.length;
        TreeNode[] nodes = new TreeNode[len];
        for(int i = 0; i < a.length; ++i){
            nodes[i] = new TreeNode(k, -1);
        }
        for(int i = 0; i < len; ++i){
            for(int j = 0; j < k; ++j){
                if(i * k + j + 1 < len){
                    nodes[i].children[j] = nodes[i * k + j + 1];
                }else{
                    break;
                }
            }
        }
        return nodes[0];
    }

    private void dfs(TreeNode root, int[] a, int[] count, long[] res, int fv){
        if(root == null || count[0] >= a.length){return;}
        root.val = a[count[0]++];
        res[0] += (root.val ^ fv);
        for(TreeNode child: root.children){
            if(child != null){
                dfs(child, a, count, res, root.val);
            }
        }
    }

    /**
     * 
     * @param k int整型 表示完全k叉树的叉数k
     * @param a int整型一维数组 表示这棵完全k叉树的Dfs遍历序列的结点编号
     * @return long长整型
     */
    public long tree6 (int k, int[] a) {
        if(a == null || a.length == 0){return 0;}
        // write code here
        int[] count = {0};
        long[] res = {0L};
        TreeNode root = bfs(k, a);
        dfs(root, a, count, res, a[0]);
        return res[0];
    }
/*
    不通过
    您的代码已保存
    请检查是否存在数组越界等非法访问情况
    case通过率为80.00%
    Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
    at Solution$TreeNode.<init>(Solution.java:11)
    at Solution.bfs(Solution.java:21)
    at Solution.tree6(Solution.java:57)
    at Main.main(Main.java:27)
*/
}