[蓝桥杯] 查找整数(使用散列映射)

    static Scanner in = new Scanner(System.in);

    public static void main(String[] args) {
        int leng = in.nextInt();
        HashMap<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < leng; i++) {
            map.putIfAbsent(in.nextInt(), i + 1);
        }
        System.out.println(map.getOrDefault(in.nextInt(), -1));
    }

发表评论