美团笔试记录

笔试题目记录

一、美团(4.9)

1、哈希表

2、对一对人先按照身高排序,再按照名字排序,重写比较方法

import java.util.*;

public class mt_1 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int[] heights = new int[n];
        for (int i = 0; i < n; i++) {
            heights[i] = scanner.nextInt();
        }
        String[] str = new String[n];
        for (int i = 0; i < n; i++) {
            str[i] = scanner.next();
        }
        ArrayList<Person> res = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            Person p = new Person(heights[i],str[i]);
            res.add(p);
        }
        Collections.sort(res, (p1, p2) -> {
            if (p1.height != p2.height) {
                return p1.height - p2.height;
            } else {
                String s1 = p1.name;
                String s2 = p2.name;
                return s1.compareTo(s2);
            }});
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < n; i++) {
            sb.append(res.get(i).name+' ');
        }
        System.out.println(sb.toString().trim());
    }

    static class Person{
        int height;
        String name;
        public Person(int height, String name){
            this.height = height;
            this.name = name;
        }
    }

Comparator的接口实现:

排序其实是由三个数字同时决定的:

if(o1.compareTo(o2) > 0 ){
    return ?;
}
//这里o1表示位于前面的字符,o2表示后面的字符
//上面的条件是,o1比o2大,这个时候,我们需要需要调整它们的顺序
//如果你想升序,那么o1比o2大就是我想要的;所以返回1,类比成true;表示我不想调整顺序

//如果你想降序,那么o1比o2大不是我想要的;所以返回-1,类比成flase;表示我想调整顺序

3、(图)给出地点,判断路径通畅与否:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        //选择n+1后面不用转换
        int[][] matrix = new int[n+1][n+1];
        
        int m = scanner.nextInt();
        int[][] arr = new int[m][2];

        for (int i = 0; i < m; i++) {
            arr[i][0] = scanner.nextInt();
        }
        for (int j = 0; j < m; j++) {
            arr[j][1] = scanner.nextInt();
        }

        for (int[] ints : arr) {
            matrix[ints[0] - 1][ints[1] - 1] = 1;
        }

        int k = scanner.nextInt();

        for (int i = 0; i < k; i++) {
            int a = scanner.nextInt();
            int b = scanner.nextInt();
            if (matrix[a - 1][b - 1] == 0) {
                System.out.println("No");
            } else {
                System.out.println("Yes");
            }
        }
    }
}xxxxxxxxxx public class Main {    private static char[] colors;    private static int count = 0;    public static void main(String[] args) {        Scanner scanner = new Scanner(System.in);        int n = scanner.nextInt();        colors = new char[n];        for (int i = 0; i < colors.length; i++) {            colors[i] = (char) ('a' + i);        }        char[][] matrix = new char[2][2];        for (char[] m : matrix) {            Arrays.fill(m, '*');        }        answer(matrix, 0, 0);        System.out.println(count);    }    public static void answer(char[][] matrix, int row, int column) {        if (row == 2) {            count++;            return;        }        if (column == 2) {            answer(matrix,row + 1, 0);            return;        }        for (int i = 0; i < colors.length; i++) {            if (!isValid(matrix, row, column, colors[i])) {                continue;            }            matrix[row][column] = colors[i];            answer(matrix, row, column + 1);            matrix[row][column] = '*';  // 也可以不加这行        }    }    public static boolean isValid(char[][] matrix, int row, int column, char c) {        for (int j = 0; j < column; j++) {            if (matrix[row][j] == c) {                return false;            }        }        for (int i = 0; i < row; i++) {            if (matrix[i][column] == c) {                return false;            }        }        return true;    }}import java.util.Scanner;public class Main {    public static void main(String[] args) {        Scanner scanner = new Scanner(System.in);        int n = scanner.nextInt();        int[][] matrix = new int[n][n];        int m = scanner.nextInt();        int[][] arr = new int[m][2];        for (int i = 0; i < m; i++) {            arr[i][0] = scanner.nextInt();        }        for (int j = 0; j < m; j++) {            arr[j][1] = scanner.nextInt();        }        for (int[] ints : arr) {            matrix[ints[0] - 1][ints[1] - 1] = 1;        }        int k = scanner.nextInt();        for (int i = 0; i < k; i++) {            int a = scanner.nextInt();            int b = scanner.nextInt();            if (matrix[a - 1][b - 1] == 0) {                System.out.println("No");            } else {                System.out.println("Yes");            }        }    }}

你可能感兴趣的:(java,java)