攜程2018 秋招編程題 - java

第一題:

兩個排好序的數組 求中位數

import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Scanner;

public class Main {


    public static void main(String[] args) {
        
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            int count = 0;
            PriorityQueue<Integer> minHeap = new PriorityQueue<>();
            PriorityQueue<Integer> maxHeap = new PriorityQueue<Integer>(15, new Comparator<Integer>() {
                @Override
                public int compare(Integer o1, Integer o2) {
                    return o2 - o1;
                }
            });
            
            int n = scanner.nextInt();
            for (int i = 0; i < n; i++) {
                int num = scanner.nextInt();
                if (count % 2 == 0) {
                    maxHeap.offer(num);
                    int filteredMaxNum = maxHeap.poll();
                    minHeap.offer(filteredMaxNum);
                } else {
                    minHeap.offer(num);
                    int filteredMinNum = minHeap.poll();
                    maxHeap.offer(filteredMinNum);
                }
                count++;
            }
            int m = scanner.nextInt();
            for (int i = 0; i < m; i++) {
                int num = scanner.nextInt();
                if (count % 2 == 0) {
                    maxHeap.offer(num);
                    int filteredMaxNum = maxHeap.poll();
                    minHeap.offer(filteredMaxNum);
                } else {
                    minHeap.offer(num);
                    int filteredMinNum = minHeap.poll();
                    maxHeap.offer(filteredMinNum);
                }
                count++;
            }

            if (count % 2 == 0) {
                System.out.println(new Double((minHeap.peek() + maxHeap.peek())) / 2);
            } else {
                System.out.println(new Double((minHeap.peek())));
            }
        }
    }
}

第二題

輸入一個無序數組,輸出數組中不存在的最小正整數

import java.util.*;

public class Main {
    /*請完成下面這個函數,實現題目要求的功能
    當然,你也可以不按照下面這個模板來作答,完全按照自己的想法來 ^-^
    ******************************開始寫代碼******************************/
    static int findMinMis(int[] A) {
        if (A == null) {
            return 0;
        }
        Arrays.sort(A);
        int num = -1;
        for (int i = 1; i < A.length; i++) {
            if (A[i] - A[i - 1] != 1) {
                if (A[i - 1] + 1 > 0) {
                    num = A[i - 1] + 1;
                    break;
                }
            }
        }
        if (num == -1) {
            if (A[0] > 1) {
                num = A[0] - 1;
            } else {
                num = A[A.length - 1] + 1;
            }
            if (A[A.length - 1] < 0) {
                num = 1;
            }
        }
        return num;

    }

    /******************************結束寫代碼******************************/


    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int res;

        int _A_size = 0;
        _A_size = Integer.parseInt(in.nextLine().trim());
        int[] _A = new int[_A_size];
        int _A_item;
        for (int _A_i = 0; _A_i < _A_size; _A_i++) {
            _A_item = Integer.parseInt(in.nextLine().trim());
            _A[_A_i] = _A_item;
        }

        res = findMinMis(_A);
        System.out.println(String.valueOf(res));
    }
}

第三題

去除重複字符(包括特殊字符)

import java.util.LinkedHashSet;
import java.util.Scanner;
import java.util.Set;

public class Main {
    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            String str = scanner.nextLine();
            Set<Character> set = new LinkedHashSet<>();
            for (int i = 0; i < str.length(); i++) {
                Character c = str.charAt(i);
                if (set.add(c)) {
                    System.out.print(c);
                }
            }
            System.out.println();
        }

    }
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章