标签:sta record int TreeMap 次数 RecentCounter new 请求
这是我的废物写法
class RecentCounter { Deque<Integer> sta; public RecentCounter() { this.sta = new LinkedList<>(); } public int ping(int t) { int res = 1; Deque<Integer> test = new LinkedList<>(); while(!sta.isEmpty() && sta.peekLast() >= t-3000){ test.addLast(sta.pollLast()); res++; } while(!test.isEmpty()){ sta.addLast(test.pollLast()); } sta.addLast(t); return res; } } /** * Your RecentCounter object will be instantiated and called as such: * RecentCounter obj = new RecentCounter(); * int param_1 = obj.ping(t); */
这是应用TreeMap
java中的TreeMap是一类有序集合,能够快速返回一个特定大小的值,例如下面用到的ceilingKey,就是返回TreeMap中第一个大于等于t-3000的值,类似的函数还有floor等,用起来很方便
class RecentCounter { TreeMap<Integer, Integer> tm; List<Integer> record; public RecentCounter() { tm = new TreeMap<>(); record = new ArrayList<>(); } public int ping(int t) { tm.put(t, record.size()); record.add(t); Integer target = tm.ceilingKey(t - 3000); return record.size() - tm.get(target); } }
之后更新一下TreeMap的一些用法吧。
标签:sta,record,int,TreeMap,次数,RecentCounter,new,请求 来源: https://www.cnblogs.com/Dkfeng/p/15202851.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。