搜狗第一题-自定义Map

这道题是三道题里面最简单的一道,直接继承LinkedHashMap,然后重写removeEldestEntry方法
 import java.util.*;

    public class Main<K, V> extends LinkedHashMap<K, V> {

    private int maxSize;

    public Main(int maxSize) {
        super(maxSize + 1, 0.999999f, true);
        this.maxSize = maxSize + 1;
    } 

    @ Override
    protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
        if (size() > maxSize - 1) {
            System.out.println(eldest.getKey() + " " + eldest.getValue());
            return true;
        }
        return false;
    }

    private static Main<String, Long> map;

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int size = in.nextInt();
        map = new Main<>(size);
        in.nextLine();
        while (in.hasNext()) {
            String[] inputs = in.nextLine().split(" ");
            putEntry(inputs[0], Long.parseLong(inputs[1]));
        }
    }

    private static void putEntry(String key, Long value) {
        if (map.containsKey(key)) {
            if (value > map.get(key)) {
                map.put(key, value);
            }
        } else {
            map.put(key, value);
        }
    }
}
#搜狗##题解##笔试题目#
全部评论
卧槽大佬牛逼,我要这么牛逼说不定就a了😥😥😥
点赞 回复 分享
发布于 2019-09-08 18:37
大佬,请问,那个输入那块怎么处理,我用while就一直会死循环,停止不了输入就输不出来结果
点赞 回复 分享
发布于 2019-09-08 18:15
import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Test {     public static void main(String[] args){         Scanner in = new Scanner(System.in);         int count = in.nextInt();         HashMap<String,Integer> a=new HashMap<String, Integer>(count);         LinkedList<String> q=new LinkedList<String>();         in.nextLine();         ArrayList<String> h=new ArrayList<String>();         while(in.hasNext()) {          String str=in.nextLine();          if(str.equals("end")) {          break;          }          h.add(str);         }         for(String k:h) {            String[] strs=k.split(" ");          String x=strs[0];int y=Integer.valueOf(strs[1]);          if(a.containsKey(x)) {          int c=a.get(x);              if(c<y) {              a.put(x, y);              q.remove(x);              q.add(x);              }          }else if(a.size()<count) {          a.put(x, y);          q.push(x);          }else {          String out=q.pop();          int outt=a.get(out);          System.out.println(out+" "+outt);          a.put(x, y);          q.push(x);          }         }     } } 求帮忙看下 这个对不对啊? 还有就是 我始终没搞清楚如何算是输入结束呢?这里我自己测试的时候加end才行 他题里面的那个要怎么写才能表示输入结束?
点赞 回复 分享
发布于 2019-09-08 18:08
收到面试通知了吗大佬
点赞 回复 分享
发布于 2019-09-08 23:37
我也是继承LinkedHashMap.. 忘了有removeEldestEntry这个函数,,而且他输入的那个真的可以当时间戳来用???我还特地加了一个属性。。
点赞 回复 分享
发布于 2019-09-08 18:16
祖传手写只能90%的哭了,老是超时
点赞 回复 分享
发布于 2019-09-08 18:13
我这个真的是全手工定制map啊,过了50%,数组越界还是超时给忘记了,太难了 package 笔试.搜狗; import java.util.*; class map { public ArrayList<HashMap<String, Integer>> list = new ArrayList<>(); public ArrayList<HashMap<String, Integer>> taotaiList = new ArrayList<>(); int size; public map(int n) { size = n; } public void insert(String key, Integer value) { if (isHave(key) != null) { //存在 HashMap<String, Integer> have = isHave(key); if (have.get(key) < value) { //需要更新,删除以前,插入新的 //删除 deleteMap(key); have.put(key, value); list.add(have); } } else { //不存在 HashMap<String, Integer> newMap = new HashMap<>(); newMap.put(key, value); if (list.size() < size) { //list大小满足,直接保存 list.add(newMap); } else { taotaiList.add(list.get(0)); list.remove(0); insert(key, value); } } } /**  * 判断是否存在  * 存在返回这个map  * 不存在返回null  *  * @param key  * @return  */ public HashMap<String, Integer> isHave(String key) { HashMap<String, Integer> oldMap = null; for (HashMap<String, Integer> map1 : list) { if (map1.containsKey(key)) { oldMap = map1; } } return oldMap; } /**  * 删除相同的key之前的数据  *  * @param key  */ public void deleteMap(String key) { for (int i = 0; i < list.size(); i++) { if (list.get(i).containsKey(key)) { list.remove(list.get(i)); } } } } public class T1 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); map map = new map(n); ArrayList<String> list = new ArrayList<>(); String next = sc.nextLine(); while (sc.hasNextLine()) { list.add(sc.nextLine()); } for (String s1 : list) { map.insert(s1.split(" ")[0], Integer.parseInt(s1.split(" ")[1])); } for (int i = 0; i < map.taotaiList.size(); i++) { Set<String> set = map.taotaiList.get(i).keySet(); for (String s : set) { System.out.println(s + " " + map.taotaiList.get(i).get(s)); } } } }
点赞 回复 分享
发布于 2019-09-08 18:09
我靠 我也是用linkedhashmap 但是忘了重写
点赞 回复 分享
发布于 2019-09-08 18:04
还是大佬厉害,我的过了50%,提示数组越界 import java.util.Arrays; import java.util.Comparator; import java.util.HashMap; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); int length = input.nextInt(); Node[] list = new Node[length]; int size = 0; int time = 0; HashMap<String, Integer> map = new HashMap<>(); while (input.hasNext()) { String s = input.next(); int num = input.nextInt(); if (map.containsKey(s)) { if (num > list[map.get(s)].num) { list[map.get(s)].num = num; list[map.get(s)].time = time; } } else { if (size < length) { list[size] = new Node(s, num, time); map.put(s, size); size++; } else { Comparator<Node> cmp = new MyComparator(); Arrays.sort(list, cmp); map.remove(list[0].str); System.out.println(list[0].str + " " + list[0].num); list[0] = new Node(s, num, time); map.put(s, 0); for (int i = 1; i < length; i++) { map.put(list[i].str, i); } } } time++; } } static class Node { String str; int num; int time; public Node(String str, int num, int time) { this.str = str; this.num = num; this.time = time; } } static class MyComparator implements Comparator<Node> { @Override public int compare(Node a, Node b) { return a.time < b.time ? -1 : 1; } } }
点赞 回复 分享
发布于 2019-09-08 18:04

相关推荐

04-17 18:32
门头沟学院 Java
野猪不是猪🐗:他跟你一个学校,你要是进来之后待遇比他好,他受得了?
点赞 评论 收藏
分享
评论
5
20
分享

创作者周榜

更多
牛客网
牛客企业服务