腾讯暑期实习正式批前端笔试题解

第一题的主要思路就是判断前面的模块有没有建好,所以初始化从最低层模块判断,然后往前找断层,即该模块没有运过来,此时打印left到right的模块,如果left=right,那么打印空格,无法操作
假设有6个模块,初始化right = 6;
第一天运来 3,arr[right] == 0; 此时left = right;无法操作,打印空格
第二天运来 5,arr[right] == 0; 打印空格
第三天运来 6,arr[right] == 1, 找到left = 4, 打印 4 + 1 到 right(6), right变为4
第四天运来 4,同理打印4 3
第五天运来 2,打印2
第六天运来 1,打印1
import java.util.*;
/**
 * @author deng
 * @description
 * @create 2019-04-05 下午4:07
 */
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int len = scanner.nextInt() + 1;
        int arr[] = new int[len];
        int right = len - 1;                // 从最低层开始建设
        StringBuffer s = new StringBuffer();
        for (int i = 0; i < len - 1; i++) {
            int n = scanner.nextInt();
            arr[n] = 1;
            int left = getLeftBoundry(arr, right); // 判断今天运来材料后能建几个模块
            if (left < right) {                    // 可建模块数大于1
                for (int j = left; j < right; j++) {
                    // 这块我忘了要不要按运来的顺序输出了,如果要的话,还需要一个额外的数组保存顺序,如[6, 1, 5, 2, 4, 3] left = 5, right = 6,就输出6 5
                    // 然后根据遍历该顺序数组并判断值在不在left和right区间,然后就能按运来的顺序打印输出了
                    s.append(String.valueOf(j + 1) + " ");
                }
                s.append("\n");
                right = left;                      // 更新边界
            }else{
                s.append(" \n");
            }
        }
        System.out.print(s);
    }
    public static int getLeftBoundry(int arr[], int right) {
        for (int i = right; i >= 0; i--) {
            if (arr[i] == 0) {
                return i;
            }
        }
        return 0;
    }
}
两组测试用例

  1. 3
    3 1 2
    3

    1 2

  2. 4
    2 4 1 3

    4

    1 2 3
第二题用栈,或者字符串也能解决,原理和用栈差不多。找到一对,替换为空串,直到无法替换时,看 count 的数值确定输赢

import java.util.*;

/**
 * @author deng
 * @description
 * @create 2019-04-05 下午4:07
 */
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Stack<Integer> stack = new Stack<>();
        int size = scanner.nextInt();
        for (int i = 0; i < size; i++) {
            int n = scanner.nextInt();
            int count = 0;            // 计算牛牛是否最后拿
            while (n != 0) {
                if (!stack.isEmpty() && stack.peek() == n % 10) {
                    count++;
                    stack.pop();
                } else {
                    stack.push(n % 10);
                }
                n /= 10;
            }
            if (count % 2 == 1) {    // 其实从这里来看,根本不用关心栈里有没有数据, 只需要判断牛牛是否拿到最后一对数字对
                System.out.print("you are win!\n");
            } else {
                System.out.print("oh no.\n");
            }
            stack.clear();
        }
    }
}
两组测试用例
2
12212
113
oh no.
you are win!

2
12233
112233
oh no.
you are win!

第三题没想出来,题目也没保存,谁存了啊,评论区分享下啊

以上都是个人思考的解法,欢迎交流。
最后吐槽下,牛客体验没leetcode好,我都快被这个编辑器逼疯了,代码粘贴过来就一行...我找了个markdown编辑器转换才拿到格式
#腾讯##笔试题目##前端##题解#
全部评论
这编辑器...样式错乱
点赞 回复
分享
发布于 2019-04-06 10:48
考试当然不会告诉哪个测试样例没通过啊。。。
点赞 回复
分享
发布于 2019-04-06 10:53
百信银行
校招火热招聘中
官网直投
+1 牛客的这个编辑体验是真的难受
点赞 回复
分享
发布于 2019-04-06 11:27
你们都是用c来答题吗?可以用js吗?但是用这个会不会太慢了呢...
点赞 回复
分享
发布于 2019-04-06 21:36
第三题:把 1 到 2*n 的数分为相同大小的两组 A 和 B (即长度都是n)。然后按照从小到大排序之后,对于 1 <= i <= n,均满足 abs(A[i] - B[i]) >= K。计算出所有满足条件的分配方案数。 输入:输入两个整数 n (1 <= n <= 50), k (1 <= k <= 10); 输出:输出一个整数 示例: 输入: 2 2 输出: 2 说明: A = {1, 2} B = {3, 4} A = {3, 4} B = {1, 2}
点赞 回复
分享
发布于 2019-04-07 09:47

相关推荐

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