金山云Java方向9.16笔试

第一题:用long也越界了好像,n也太大了。过了91%。
public class Main {
    public static void main(String[] args) {
        Scanner in=new Scanner(System.in);
        while (in.hasNext())
        {
            int n=in.nextInt();
            if(n<0)
            {
                System.out.println(0);
                continue;
            }
            long[] nums=new long[n];
            long pre=0;
            nums[0]=1;
            for(int i=1;i<n;i++)
            {
                nums[i]=nums[i-1]+pre;
                pre=nums[i-1];
            }
            for(int i=0;i<n;i++)
            {
                for(int j=0;j<=i;j++)
                {
                    System.out.print(nums[j]+" ");
                }
                for(int k=i-1;k>=0;k--)
                {
                    System.out.print(nums[k]+" ");
                }
                System.out.println();
            }
        }
    }
}
第二题:反序列化二叉树+最近公共祖先。两道原题的结合版。AC
public class Main {
    static class TreeNode{
        public int val;
        public TreeNode left;
        public TreeNode right;
        public TreeNode(int val)
        {
            this.val=val;
        }
    }

    static TreeNode p=new TreeNode(-1);
    static TreeNode q=new TreeNode(-1);
    static int pVal;
    static int qVal;
    public static void main(String[] args) {
        Scanner in=new Scanner(System.in);
        while (in.hasNextLine())
        {
            String id=in.nextLine();
            if(id==null || id.equals(""))
            {
                System.out.println(-1);
                continue;
            }
            String[] ids = id.split(" ");
            LinkedList<Integer> queue=new LinkedList<>();
            for(int i=0;i<ids.length;i++)
            {
                queue.offer(Integer.valueOf(ids[i]));
            }
            String virus=in.nextLine();
            String[] viruses = virus.split(" ");
            pVal=Integer.valueOf(viruses[0]);
            qVal=Integer.valueOf(viruses[1]);
            TreeNode root=bulidTree(queue);
            TreeNode res=getRes(root);
            if(res==null)
            {
                System.out.println(-1);
            }else
            {
                System.out.println(res.val);
            }
        }
    }

    private static TreeNode getRes(TreeNode root) {
        if(root==null || root==p || root==q) return root;
        TreeNode left=getRes(root.left);
        TreeNode right=getRes(root.right);

        if(left==null) return  right;
        if(right==null) return left;
        return root;
    }

    private static TreeNode bulidTree(LinkedList<Integer> queue) {
        int val=queue.poll();
        if(val==-1)
        {
            return null;
        }
        TreeNode root=new TreeNode(val);
        if(pVal==root.val)
        {
            p=root;
        }
        if(qVal==root.val)
        {
            q=root;
        }
        root.left=bulidTree(queue);
        root.right=bulidTree(queue);
        return root;
    }
}










#金山云##笔试题目#
全部评论
``` public class Main {     public static void main(String[] args) {         Scanner scanner = new Scanner(System.in);         int n = scanner.nextInt();         for (int i = 1; i <= n; i++) {             int m=0,k=1,t=0;             Stack stack = new Stack();             System.out.printf(k+" ");             stack.push(k);             for (int j = 2; j <= i; j++) {                 if(j != i){                     stack.push(m+k);                 }                 System.out.printf(m+k+" ");                 t=m;                 m=k;                 k=t+k;             }             for (int j = i+1; j <= (2*i-1); j++) {                 System.out.printf(stack.pop()+ " ");             }             System.out.println("");         }         scanner.close();     } } ```
点赞 回复 分享
发布于 2020-09-17 16:12
测开只对了一道,又凉了么😫
点赞 回复 分享
发布于 2020-09-17 13:04
n小于1,不输出,可以AC
点赞 回复 分享
发布于 2020-09-17 10:17
小于0不输出就行了
点赞 回复 分享
发布于 2020-09-16 21:18
第一题怎么搞都是91%泪目,第二题不想建树,想了好久想不出来,只有去建树,写不完了,难受
点赞 回复 分享
发布于 2020-09-16 21:17

相关推荐

当年还在美团那个倒霉的&nbsp;Peppr&nbsp;团队工作时,我一直有个疑问:这群人每天到底在自嗨什么。每次开会一堆人围着一堆“看起来很高级”的文档转,模板统一、名词复杂、页数感人,每一页都在暗示一件事:“你不懂,是因为你不专业。”但现实是——代码照样写在&nbsp;💩&nbsp;山上,该出问题还是会出问题,这真的很逗,系统一出问题,文档的唯一作用就是证明:“我们当初确实认真写过文档。”所以本质区别到底是什么?是代码质量提升了,还是大家在精神层面完成了一次“工程师&nbsp;cosplay”?有句话说得好潮水退去才知道谁在裸泳。还记得当时的马哥、明哥(图&nbsp;1&nbsp;左)最爱反复强调一句话:“所有场景一定要想到。”、“这个场景为什么没考虑到?”不过他们这些话我是真的听进去了。不然我也不会在一年多前就说:这个项目活不过两年。顺带一提,那段时间还有个固定节目。每次下楼,总能听见我明哥在吐槽不同的人。我从他身后绕过去,经常能听到他一边抽烟一边说:“xx&nbsp;这小子太坑了,回头我一定要跟马哥说说。”于是深谙人情世故但真不会抽烟的我也会从口袋掏出一支低尼古丁含量的烟给自己点上,假意自己什么都没听到什么都不知道,只是来抽烟的。后来我才明白,这可能也是团队文化的一部分:问题永远在别人身上,而我们,永远在复盘里😂。
秋招白月光
点赞 评论 收藏
分享
评论
1
6
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务