大佬帮我看看猿辅导笔试第一题为什么通不过

自己本地IDE能过 为什么提交就是0呢,感谢大佬
import java.util.ArrayList;
import java.util.Scanner;

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

        while (sc.hasNextInt()) {
            int n = sc.nextInt();
            int[] nodenum = new int[n];
            for (int i = 0; i < n; i++) {
                nodenum[i] = sc.nextInt();
            }
            ArrayList<Integer> ans = help(nodenum);
            System.out.println(ans);
        }

    }
    public static ArrayList<Integer> help(int[] arr) {
        ArrayList<Integer> list=new ArrayList<>();
        int i=0;
        int index=(int) (Math.pow(2, i)-1);
        int nextIndex=(int) (Math.pow(2, i+1)-1);
        while(nextIndex<arr.length) {
            list.add(arr[index]);
            i++;
            index=(int) (Math.pow(2, i)-1);
            nextIndex=(int) (Math.pow(2, i+1)-1);
        }
        for(int j=index;j<arr.length;j++) {
            list.add(arr[j]);
        }
        index=index-1;
        while(index>0) {
            list.add(arr[index]);
            i--;
            index=(int) (Math.pow(2, i)-1)-1;
        }
        return list;
    }

}


#猿辅导##笔试题型#
全部评论
是我输入输出格式不对,没法正常退出么...请问大佬们如何修改😥
点赞
送花
回复
分享
发布于 2020-08-22 20:59
第二个for循环底层叶子结点的错了 完全二叉树 有可能倒数第二层也有叶子结点 要跳上去
点赞
送花
回复
分享
发布于 2020-08-22 21:05
滴滴
校招火热招聘中
官网直投
没考虑两层叶结点的情况,比如8  1 2 3 4 5  6 7 8
点赞
送花
回复
分享
发布于 2020-08-22 21:05

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务