首页 > 网易笔试思路讨论
头像
RealLei
编辑于 2020-08-08 16:53
+ 关注

网易笔试思路讨论

  1. 是偶数就拆成一堆2,是奇数就先拿出3,剩下偶数拆成一堆2,实际上还是num/2, 所以直接加num/2就行了

  2. 给的m个数放在一个Queue中,先入先出保证题目给的顺序;同时也加入一个HashSet记录都有哪些数存在;剩下的(n-m)个数,从1开始遍历加入另一个Queue(名叫composite),加的时候判断不在set里面才能加入; 然后两个queue比较队列首部的值,小的就加入res; 输出res

public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int m = in.nextInt();
        int[] res = new int[n]; // res

        Queue<Integer> q = new LinkedList<>();
        Set<Integer> set = new HashSet<>();

        for (int i = 0; i < m; i++) {
            int k = in.nextInt();
            q.offer(k);
            set.add(k);
        }

        //int[] tmp = new int[n-m]; // 存放需要的剩下的(n-m)个数
        Queue<Integer> composite = new LinkedList<>(); // Queue比较方便
        for (int i = 1; ; i++) {
            if (!set.contains(i)) {
                composite.offer(i);
            }
            if (composite.size() == n-m) break;
        }

        // 这里: Queue中是m个本来就有的数按顺序排放; tmp是(n-m)个数按从小到大排列
        for (int i = 0; i < n; i++) {
            if ( q.peek() < composite.peek()) {
                res[i] = q.poll();
            } else {
                res[i] = composite.poll();
            }
        }

        for (int i = 0; i < n; i++) {
            System.out.print(res[i] + " ");
        }
    }

3.对每一组: 从小到大排序;求和,看是不是偶数,如果是,从后往前累加看能不能得到整个数组和的一半,如果可以就不用丢,往结果res中加入0; 如果不行,从前往后遍历丢(从小到大丢),再判断是不是偶数,如果是偶数就从后往前加,重复 (从后往前:因为这样greedy:快)
因为每组都最多15个元素,所以可以这样直接暴力greedy计算
下面是代码,但是一直说 NullPointerException, 不知道为什么...

public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // number of batches
        int numBatch = in.nextInt();
        List<Integer> res = new ArrayList<>();

        for (int i = 0; i < numBatch; i++) {

            int num = in.nextInt(); // num个元素
            int[] arr = new int[num];
            int sum = 0;
            for (int j = 0; j < num; j++) {
                arr[j] = in.nextInt(); // 得到这组所有的数字
                sum += arr[j];
            }
            Arrays.sort(arr); //30 60 5 15 30 -->   5 15 30 30 60

            int count = 0; // 累积丢掉的和
            int index = 0; // 丢掉之后数之后,arr看作从index开始的数组
            int tmp = 0; // 从后往前加累加和
            int k = arr.length-1;

            while (index < arr.length) {
                if (sum % 2 == 0) {
                    while (tmp <= sum/2) {
                        tmp += arr[k--];
                        if (tmp == sum/2) {
                            res.add(0);
                            break;
                        }
                    }
                    // tmp > sum/2: 不可能相等了,  需要arr丢掉前面比较小的数,然后继续循环判断
                    count += arr[index];
                    sum -= arr[index++];
                } else {
                    while (index < arr.length) {
                        count += arr[index];
                        sum -= arr[index++];
                        if (sum % 2 == 0) {
                            while (tmp <= sum/2) {
                                tmp += arr[k--];
                                if (tmp == sum/2) {
                                    res.add(count); // 丢掉的累积和
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }

        for (int i = 0; i < numBatch; i++) {
            System.out.print(res.get(i) + " ");
        }
    }
  1. 构建树,不知道是像经典的二叉树那样创建一个TreeNode数据结构,还是说直接一个数组就行了... 感觉只能暴力做,写了一小半就没时间了...

全部评论

(6) 回帖
加载中...
话题 回帖

推荐话题

相关热帖

近期热帖

历年真题 真题热练榜 24小时
技术(软件)/信息技术类
查看全部

近期精华帖

热门推荐