【JAVA】騰訊2021校園招聘-後臺&綜合-第二次筆試

愛做夢的魚發表於2020-09-06

前言

祝所有人都有理想的offer,offer拿到手軟,you can, you can fly,you can,you can catch the stars,you can stand shoulder to shoulder with the sun在這裡插入圖片描述

一、連結串列的公共部分(全A)

程式碼

package tengxun;

import java.util.Scanner;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author: 
 * @Email: 
 * @Date: 2020/9/6
 * @Time: 20:03
 * @Version: 1.0
 * @Description: Description
 */
public class First {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] arr1 = new int[n];
        for (int i = 0; i < n; i++) {
            arr1[i] = sc.nextInt();
        }
        int m = sc.nextInt();
        int[] arr2 = new int[m];
        for (int i = 0; i < m; i++) {
            arr2[i] = sc.nextInt();
        }
        sc.close();
        int p1 = 0, p2 = 0;
        while (p1 < n && p2 < m) {
            if (arr1[p1] == arr2[p2]) {
                System.out.print(arr1[p1] + " ");
                p1++;
                p2++;
            } else if (arr1[p1] < arr2[p2]) {
                p2++;
            } else {
                p1++;
            }
        }
    }
}

二、通知傳遞(全A)

程式碼

package tengxun;

import java.util.HashSet;
import java.util.Scanner;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author: 
 * @Email: 
 * @Date: 2020/9/6
 * @Time: 20:46
 * @Version: 1.0
 * @Description: Description
 */
public class Second {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        int[][] teams = new int[m][];
        for (int i = 0; i < m; i++) {
            int len = sc.nextInt();
            int[] team = new int[len];
            for (int j = 0; j < len; j++) {
                team[j] = sc.nextInt();
            }
            teams[i] = team;
        }
        sc.close();
        HashSet<Integer> knows = new HashSet<>();
        knows.add(0);
        for (int k = 0; k < m; k++) {
            for (int i = 0; i < m; i++) {
                for (int j = 0; j < teams[i].length; j++) {
                    if (knows.contains(teams[i][j])) {
                        for (int j2 = 0; j2 < teams[i].length; j2++) {
                            knows.add(teams[i][j2]);
                        }
                        break;
                    }
                }
            }
        }
        System.out.println(knows.size());
    }
}

三、字串次數(A0.6)

程式碼

package tengxun;

import java.util.HashMap;
import java.util.Scanner;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author: 
 * @Email: 
 * @Date: 2020/9/6
 * @Time: 21:12
 * @Version: 1.0
 * @Description: Description
 */
public class Third {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int k = sc.nextInt();
        HashMap<String, Integer> map1 = new HashMap<>();
        while (n-- > 0) {
            String str = sc.next();
            map1.put(str, map1.getOrDefault(str, 0) + 1);
        }
        sc.close();

        int max = 0;
        String maxString = null;
        HashMap<String, Integer> map2 = (HashMap<String, Integer>) map1.clone();
        for (int i = 0; i < k; i++) {
            for (String string2 : map2.keySet()) {
                if (map2.get(string2) > max ||
                        (map2.get(string2) == max && string2.compareTo(maxString) < 0)) {
                    max = map2.get(string2);
                    maxString = string2;
                }
            }
            map2.remove(maxString);
            System.out.println(maxString + " " + max);
            max = 0;
        }

        int min = Integer.MAX_VALUE;
        String minString = null;
        for (int i = 0; i < k; i++) {
            for (String string1 : map1.keySet()) {
                if (map1.get(string1) < min ||
                        (map1.get(string1) == min && string1.compareTo(minString) < 0)) {
                    min = map1.get(string1);
                    minString = string1;
                }
            }
            map1.remove(minString);
            System.out.println(minString + " " + min);
            min = Integer.MAX_VALUE;
        }
    }
}

四、中位數(全A)

程式碼

package tengxun;

import java.util.Arrays;
import java.util.Scanner;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author: 
 * @Email: 
 * @Date: 2020/9/6
 * @Time: 20:25
 * @Version: 1.0
 * @Description: Description
 */
public class Main{
    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);
        }
    }
}

五、紅黑棋(交卷之後五分鐘做出來的,沒有提交,所以不知道A多少)

程式碼

import java.util.Scanner;

public class C {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int[] list = new int[2 * n];
        String color = scanner.next();
        for (int i = 0; i < list.length; i++) {
            if (color.charAt(i) == 'R') {
                list[i] = 1;
            } else {
                list[i] = -1;
            }
        }
        for (int i = 0; i < list.length; i++) {
            list[i] *= scanner.nextInt();
        }
        scanner.close();
        int num = 0;
        int r = 1;
        int b = -1;
        for (int i = 0; i < list.length; i++) {
            if (list[i] > 0) {
                if (list[i] != r) {
                    for (int j = i + 1; j < list.length; j++) {
                        if (list[j] == r) {
                            num += j - i;
                            for (int j2 = j; j2 > i; j2--) {
                                list[j2] = list[j2 - 1];
                            }
                            list[j] = r;
                            break;
                        }
                    }
                }
                r++;
            } else {
                if (list[i] != b) {
                    for (int j = i + 1; j < list.length; j++) {
                        if (list[j] == b) {
                            num += j - i;
                            for (int j2 = j; j2 > i; j2--) {
                                list[j2] = list[j2 - 1];
                            }
                            list[j] = b;
                            break;
                        }
                    }
                }
                b--;
            }
        }
        System.out.println(num);

    }

}

相關文章