9.6 腾讯后台开发笔试

1. 第一题:

思路:有序的,直接遍历比较就可以了

import java.util.*;
// AC
public class Main1 {

    public static class ListNode
    {
        int val;
        ListNode next;

        ListNode(int x)
        {
            val = x;
        }
    }

    public static ListNode createLinkedList(int arr[])
    {
        if(arr.length == 0)
        {
            return null;
        }
        ListNode head = new ListNode(arr[0]);
        ListNode current = head;
        for(int i = 1; i < arr.length; i++)
        {
            current.next = new ListNode(arr[i]);
            current = current.next;
        }
        return head;
    }

    public static void getPublicNode(ListNode a, ListNode b)
    {
        while(a != null && b != null)
        {
            if(a.val < b.val)
            {
                b = b.next;
            }
            else if(a.val > b.val)
            {
                a = a.next;
            }
            else
            {
                System.out.print(a.val + " ");
                a = a.next;
                b = b.next;
            }
        }
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int a[] = new int[n];
        for(int i = 0; i < n; i++)
        {
            a[i] = sc.nextInt();
        }
        int m = sc.nextInt();
        int b[] = new int[m];
        for(int i = 0; i < m; i++)
        {
            b[i] = sc.nextInt();
        }

        ListNode list1 = createLinkedList(a);
        ListNode list2 = createLinkedList(b);

        getPublicNode(list1, list2);
    }
}

// 测试1:
// 输入:
// 6
// 6 5 4 3 2 1
// 5
// 6 5 3 2 1
//
// 输出:
// 6 5 3 2 1

2. 第三题:

思路:字符串次数前k个(前k大),后k个(前k小),哈希表排序

import java.util.*;
// AC
public class Main3 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int K = sc.nextInt();
        HashMap<String, Integer> map = new HashMap<>();
        String temp;
        for (int i = 0; i < N; ++i) {
            temp = sc.next();
            map.put(temp, map.getOrDefault(temp, 0) + 1);
        }
        ArrayList<Map.Entry<String, Integer>> list = new ArrayList<>(map.entrySet());
        list.sort((o1, o2) -> !o1.getValue().equals(o2.getValue()) ? o2.getValue() - o1.getValue()
                : o1.getKey().compareTo(o2.getKey()));

        for (int i = 0; i < K; ++i) {
            System.out.println(list.get(i).getKey() + " " + list.get(i).getValue());
        }
        list = new ArrayList<>(map.entrySet());
        list.sort((o1, o2) -> !o1.getValue().equals(o2.getValue()) ? o1.getValue() - o2.getValue()
                : o1.getKey().compareTo(o2.getKey()));
        for (int i = 0; i < K; ++i) {
            System.out.println(list.get(i).getKey() + " " + list.get(i).getValue());
        }
    }
}

// 测试1:
// 输入:
// 4 2
// 1 2 3 4
//
// 输出:
// 1 1
// 2 1
// 1 1
// 2 1

// 测试2:
// 输入:
// 4 2
// 1 2 1 4
//
// 输出:
// 1 2
// 2 1
// 2 1
// 4 1

3. 第四题:

思路:
找规律,原中位数有俩 mid1(a) 和 mid2(b)。
删除的数字 < mid2,则中位数为 mid2。
否则,为mid1

import java.util.*;
// AC
public class Main4 {

    // 方法1: 通过 55%
    // public static void main(String[] args) {
    //     Scanner sc = new Scanner(System.in);
    //     int n = sc.nextInt();
    //     int a[] = new int[n];
    //     int b[] = new int[n];
    //     for(int i = 0; i < n; i++)
    //     {
    //         a[i] = sc.nextInt();
    //         b[i] = a[i];
    //     }
    //     Arrays.sort(b);

    //     int count = 0;
    //     int temp[] = new int[b.length - 1];

    //     for(int i = 0; i < a.length; i++)
    //     {
    //         for(int j = 0; j < b.length; j++)
    //         {
    //             if(b[j] != a[i])
    //             {
    //                 temp[count] = b[j];
    //                 count++;
    //             }
    //         }
    //         System.out.println(temp[(count - 1)  / 2 ]);
    //         count = 0;
    //     }
    // }

    // 方法2: AC
    // 思路:找规律,原中位数有俩 mid1(a) 和 mid2(b)。
    // 删除的数字 < mid2,则中位数为 mid2。
    // 否则,为mid1
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = sc.nextInt();
        }
        sc.close();
        int[] arr1 = arr.clone();
        Arrays.sort(arr);
        // System.out.println(Arrays.toString(arr));
        // System.out.println(Arrays.toString(arr1));
        int a = arr[n / 2 - 1];
        int b = arr[n / 2];
        for (int i = 0; i < n; i++) {
            if (arr1[i] < b) System.out.println(b);
            else System.out.println(a);
        }
    }
}

// 测试1:
// 输入:
// 6
// 1 2 3 4 5 6
//
// 输出:
// 4 4 4 3 3 3

// 测试2:
// 输入:
// 5
// 2 6 10 13 15
//
// 输出:
// 10 10 10 6 6

4. 第二题、第五题不会做,GG。

参考:

  1. 9.6 腾讯后台开发笔试
  2. 腾讯笔试 9.6(附代码)
  3. 【JAVA】腾讯2021校园招聘-后台&综合-第二次笔试
  4. 腾讯9.6笔试 客户端
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

dev_zyx

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值