高级农民
- 积分
- 2625
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2020-1-21
- 最后登录
- 1970-1-1
|
第4问有点麻烦,首先需要排序,用max Heap,其次排序需要根据tag对应的q值,需要一个HashMap,最后还需要一个set记录已经去重,后面遇到*号不要再加了
所有测试都通过了
- private static final String SPLITTER = ", ";
- private static final String WILD_CARD = "*";
- private static final String WEIGHT_SEPARATOR = ";";
- public List<String> getSupportedLanguagesForBoth4(String headers, Set<String> supportedLanguagesForSever) {
- List<String> res = new ArrayList<>();
- if (headers == null || headers.isEmpty() || supportedLanguagesForSever == null || supportedLanguagesForSever.isEmpty()) {
- return res;
- }
- Map<String, Set<String>> tagMap = new HashMap<>();
- for (String curLang: supportedLanguagesForSever) {
- String curTag = curLang.substring(0, 2);
- tagMap.putIfAbsent(curTag, new HashSet<>());
- tagMap.get(curTag).add(curLang);
- }
- Map<String, Double> weightMap = new HashMap<>();
- PriorityQueue<String> maxHeap = new PriorityQueue<>((a, b) -> Double.compare(weightMap.get(b), weightMap.get(a)));
- Set<String> tagSetAlreadySet = new HashSet<>();
- String[] supportedLanguagesForClient = headers.split(SPLITTER);
- for (String curHeaderTag: supportedLanguagesForClient) {
- String[] curHeaderTagArray = curHeaderTag.split(WEIGHT_SEPARATOR);
- String curTag = curHeaderTagArray[0];
- double curTagWeight = Double.parseDouble(curHeaderTagArray[1].substring(2));
- if (supportedLanguagesForSever.contains(curTag)) {
- tagSetAlreadySet.add(curTag);
- weightMap.put(curTag, curTagWeight);
- maxHeap.offer(curTag);
- } else if (tagMap.containsKey(curTag)){
- for (String curLang: tagMap.get(curTag)) {
- if (tagSetAlreadySet.add(curLang)) {
- weightMap.put(curLang, curTagWeight);
- maxHeap.offer(curLang);
- }
- }
- } else if (curTag.equals(WILD_CARD)) {
- for (String curLang : supportedLanguagesForSever) {
- if (!tagSetAlreadySet.contains(curLang)) {
- weightMap.put(curLang, curTagWeight);
- maxHeap.offer(curLang);
- }
- }
- }
- }
- while (!maxHeap.isEmpty()) {
- res.add(maxHeap.poll());
- }
- return res;
- }
复制代码 |
|