金山云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

相关推荐

10-19 10:28
已编辑
成都理工大学 后端工程师
团孝子已上线feeling:面了很多家公司,能感受到目前只有小公司+外包喜欢问八股。大厂虽然也问八股,但是是从实习、项目中进行提问,并且大厂会问很深,面试官也会对你的回答进行思考➕追问,所以准备大厂面试前一定要备好相关资料。对于算法,我做的是codetop前100+力扣hot100+力扣高频150,面试中实感hot100就足够,基本上只要是hot100就秒答。对于项目和八股,我做的也是烂大街的星球项目,八股则是看小林和问ai,自己也写了很多技术博客和画了很多思维导图,并且自己也尝试用嘴巴说出来,不只停留于纸面。运气也很重要,必须要让面试官/HR看到简历才行,所以建议投递时间是下午两点。tl:第一岗位9.9&nbsp;投递9.10&nbsp;一面(一面评价:最近见过最强的大三,结束五分钟后约二面,都晚上九点了不下班吗)9.11&nbsp;二面(三道算法a出两道,反问评价:经验不够等横向,我实习生要啥经验)9.21挂(实习时间过短+其他原因,想要一年实习的,为什么不招个正职)第二岗位10.10投递10.11约面(主管打电话,说看到我之前投递记录了想要我挂qa职进去干后端,同意)10.14&nbsp;一面(无八股,主动说确实很强,意愿很强)10.16&nbsp;oc其余,友邦,东软,东华,惠择,用友oc已拒京东测开一面挂(投后端被测开捞)腾讯测试已拒(投后端被测开捞)ps:表扬惠择的主管面,没怎么问技术(可能是一面面试官沟通过了),全程一起讲大道理,解答了心中很多疑惑,也告诉我以面试官角度来看怎么选候选人,如果可以下次一定选惠择
HeaoDng:美团好像可以触发一面通
点赞 评论 收藏
分享
在看牛客的社畜很积极:身高体重那一行信息去掉,学校那一行的信息放上面,找半天都没找到你是哪个学校什么专业的
点赞 评论 收藏
分享
评论
1
6
分享

创作者周榜

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