当前位置: 首页 > news >正文

免费网站图片素材网络营销策划方案书范文

免费网站图片素材,网络营销策划方案书范文,网站建设的内容规划,有没有免费的小程序制作981. 基于时间的键值存储 - 力扣(LeetCode) 设计一个基于时间的键值数据结构,该结构可以在不同时间戳存储对应同一个键的多个值,并针对特定时间戳检索键对应的值。 实现 TimeMap 类: TimeMap() 初始化数据结构对象void…

981. 基于时间的键值存储 - 力扣(LeetCode)

设计一个基于时间的键值数据结构,该结构可以在不同时间戳存储对应同一个键的多个值,并针对特定时间戳检索键对应的值。

实现 TimeMap 类:

  • TimeMap() 初始化数据结构对象
  • void set(String key, String value, int timestamp) 存储键 key、值 value,以及给定的时间戳 timestamp
  • String get(String key, int timestamp)
    • 返回先前调用 set(key, value, timestamp_prev) 所存储的值,其中 timestamp_prev <= timestamp 。
    • 如果有多个这样的值,则返回对应最大的  timestamp_prev 的那个值。
    • 如果没有值,则返回空字符串("")。
 

示例:

输入:
["TimeMap", "set", "get", "get", "set", "get", "get"]
[[], ["foo", "bar", 1], ["foo", 1], ["foo", 3], ["foo", "bar2", 4], ["foo", 4], ["foo", 5]]
输出:
[null, null, "bar", "bar", null, "bar2", "bar2"]解释:
TimeMap timeMap = new TimeMap();
timeMap.set("foo", "bar", 1);  // 存储键 "foo" 和值 "bar" ,时间戳 timestamp = 1   
timeMap.get("foo", 1);         // 返回 "bar"
timeMap.get("foo", 3);         // 返回 "bar", 因为在时间戳 3 和时间戳 2 处没有对应 "foo" 的值,所以唯一的值位于时间戳 1 处(即 "bar") 。
timeMap.set("foo", "bar2", 4); // 存储键 "foo" 和值 "bar2" ,时间戳 timestamp = 4  
timeMap.get("foo", 4);         // 返回 "bar2"
timeMap.get("foo", 5);         // 返回 "bar2"

提示:

  • 1 <= key.length, value.length <= 100
  • key 和 value 由小写英文字母和数字组成
  • 1 <= timestamp <= 107
  • set 操作中的时间戳 timestamp 都是严格递增的
  • 最多调用 set 和 get 操作 2 * 105 次
class TimeMap {//假设key一样//HashMap<Integer , String> map = new HashMap<Integer , String>();HashMap<String,HashMap<Integer , String>> map = new HashMap<String,HashMap<Integer , String>>();// ArrayList<Integer> arr = new ArrayList<Integer>();HashMap<String,ArrayList<Integer>> arr = new HashMap<String,ArrayList<Integer>>();public TimeMap() {arr.clear();map.clear();}public void set(String key, String value, int timestamp) {//arr.add(timestamp);//map.put(timestamp,value);if(!map.containsKey(key)) {HashMap<Integer , String> tmp = new HashMap<Integer , String>();tmp.put(timestamp,value);map.put(key,tmp);} else {map.get(key).put(timestamp,value);}if(!arr.containsKey(key)) {ArrayList<Integer> tmp = new ArrayList<Integer>();tmp.add(timestamp);arr.put(key,tmp);} else {arr.get(key).add(timestamp);}}public String get(String key, int timestamp) {if(!map.containsKey(key))  return "";ArrayList<Integer> arr1 = arr.get(key);int right = arr1.size()-1;int left = 0;int mid = 0;while(left <= right) {mid = (left+right)/2;if(arr1.get(mid) < timestamp) {left = mid+1;} else if(arr1.get(mid) > timestamp) {right = mid-1;} else {return map.get(key).get(arr1.get(mid));}}if(left == 0) return "";return map.get(key).get(arr1.get(left-1));// int right = arr.size()-1;// int left = 0;// int mid = 0;// while(left <= right) {//     mid = (left+right)/2;//     if(arr.get(mid) < timestamp) {//         left = mid+1;//     } else if(arr.get(mid) > timestamp) {//         right = mid-1;//     } else {//         System.out.println(mid);//         System.out.println("------------");//         return map.get(arr.get(mid));//     }// }// if(left == 0) return "";// return map.get(arr.get(left-1));}
}/*** Your TimeMap object will be instantiated and called as such:* TimeMap obj = new TimeMap();* obj.set(key,value,timestamp);* String param_2 = obj.get(key,timestamp);*/

        每日一题,今天是中等题。这道题有些难度,但主要也是在理解题目上。

        首先读题,了解到,实际上是一个键值对。在java中,键值对一般使用map来存储。但是,除了key和value,还有一个timestamp。所以实际上是需要一个中介的。题目的set函数实际上就是存储键值对的过程。get函数才是获得答案的过程。由于key会不一样,如果一开始就上手可能会很复杂,我们可以从简单的写起,一步一步往上。

        先假设只有一个key的情况。那么key是不用存储的。get要找到给定timestamp前的最大数,那实际上就是要找<=timestamp的最大数。最后肯定是要根据找到的这个timestamp来返回值的,所以,一开始的键值对我们可以用(Integer,String)。之后用一个arraylist来存储timestamp。题目的数量级是2*105次方,如果直接暴力查找,大概率过不了。所以需要使用二分,改一下条件,让其变成可以找到小于等于timestamp的最大数。按博主的写法,最后left会是小于等于timestamp最大数的前一个。当然,博主的写法如果找到了就会直接返回了。之后只要根据找到的timestamp去map里取值就可以了。

        一个key的情况解决了,就是多个key的问题了。多个key无非就是用hashmap把一种key的情况包起来。所以一开始存储的两个数据结构都要用hashmap包起来,一个key对应一个map<Integer,String>和一个arraylist。之后就是修改set函数了:如果原本不存在这个key,就要新建一个新的map<Integer,String>和一个新的arraylist来存储这个key对应的value和timestamp。如果存在直接获取添加即可。再往后就是修改get函数了:如果不存在这个key对应的map或arraylist就说明还没有添加进来过,直接返回空字符串就行;如果存在,就按刚才一个的方法去做就可以了。

         今天这道题,用到了二分的变式,还是很有意义的,而且能够锻炼大家数据结构使用的熟练度。不过时间复杂度和空间复杂度都很大,大家可以去看看题解寻找更快更好的解决方法。


文章转载自:
http://dinncosynapse.ssfq.cn
http://dinncoodt.ssfq.cn
http://dinncodawt.ssfq.cn
http://dinncoraddled.ssfq.cn
http://dinncosummarise.ssfq.cn
http://dinncomaris.ssfq.cn
http://dinncoblaff.ssfq.cn
http://dinncolhasa.ssfq.cn
http://dinncoinwardness.ssfq.cn
http://dinncosecko.ssfq.cn
http://dinncodiascope.ssfq.cn
http://dinncoimine.ssfq.cn
http://dinncoheart.ssfq.cn
http://dinncocolorfast.ssfq.cn
http://dinncoaerophagia.ssfq.cn
http://dinncobulgar.ssfq.cn
http://dinncohistoric.ssfq.cn
http://dinnconuncio.ssfq.cn
http://dinncozoolatry.ssfq.cn
http://dinncotreehopper.ssfq.cn
http://dinncoyetta.ssfq.cn
http://dinncodiseasedness.ssfq.cn
http://dinncofmn.ssfq.cn
http://dinncoaccuse.ssfq.cn
http://dinncoprojectual.ssfq.cn
http://dinncoreciprocally.ssfq.cn
http://dinncobourn.ssfq.cn
http://dinncofolding.ssfq.cn
http://dinncovascula.ssfq.cn
http://dinncospecies.ssfq.cn
http://dinncobegat.ssfq.cn
http://dinncohydrocyclone.ssfq.cn
http://dinnconoumenally.ssfq.cn
http://dinncoborohydride.ssfq.cn
http://dinncoqueerish.ssfq.cn
http://dinncohayti.ssfq.cn
http://dinncoabandon.ssfq.cn
http://dinncocpi.ssfq.cn
http://dinncoinfeasible.ssfq.cn
http://dinncopolecat.ssfq.cn
http://dinncophycomycete.ssfq.cn
http://dinncofodgel.ssfq.cn
http://dinncointerchurch.ssfq.cn
http://dinnconightdress.ssfq.cn
http://dinncoapiary.ssfq.cn
http://dinncocatapult.ssfq.cn
http://dinncowottest.ssfq.cn
http://dinncotouriste.ssfq.cn
http://dinncorapeseed.ssfq.cn
http://dinncoharuspex.ssfq.cn
http://dinncomertensian.ssfq.cn
http://dinncopaper.ssfq.cn
http://dinncoagued.ssfq.cn
http://dinncosaucerian.ssfq.cn
http://dinncoeconomic.ssfq.cn
http://dinncoradix.ssfq.cn
http://dinncotrustify.ssfq.cn
http://dinncotentie.ssfq.cn
http://dinncofraternal.ssfq.cn
http://dinncocymotrichous.ssfq.cn
http://dinncodetoxify.ssfq.cn
http://dinncopicosecond.ssfq.cn
http://dinncoelastance.ssfq.cn
http://dinncopet.ssfq.cn
http://dinncotimeworn.ssfq.cn
http://dinncoglagolitic.ssfq.cn
http://dinncoanthracitous.ssfq.cn
http://dinncoconversancy.ssfq.cn
http://dinncocomparatively.ssfq.cn
http://dinncomunicipio.ssfq.cn
http://dinncomisregister.ssfq.cn
http://dinncophosphatide.ssfq.cn
http://dinncoquadric.ssfq.cn
http://dinncokiev.ssfq.cn
http://dinncojugendstil.ssfq.cn
http://dinncoviscosity.ssfq.cn
http://dinncogeum.ssfq.cn
http://dinncoductibility.ssfq.cn
http://dinncoosteocope.ssfq.cn
http://dinncoacidimetric.ssfq.cn
http://dinncohypoptyalism.ssfq.cn
http://dinncohomotaxial.ssfq.cn
http://dinncotawpie.ssfq.cn
http://dinncokaffiyeh.ssfq.cn
http://dinncotoreutics.ssfq.cn
http://dinncobesotted.ssfq.cn
http://dinncodidakai.ssfq.cn
http://dinncopropoxyphene.ssfq.cn
http://dinncoleukopoietic.ssfq.cn
http://dinncomitogen.ssfq.cn
http://dinncolepra.ssfq.cn
http://dinncoclaimer.ssfq.cn
http://dinncounaccommodated.ssfq.cn
http://dinncoleaded.ssfq.cn
http://dinncotrespasser.ssfq.cn
http://dinncoretransformation.ssfq.cn
http://dinncoanuresis.ssfq.cn
http://dinncounreconstructible.ssfq.cn
http://dinncotallulah.ssfq.cn
http://dinncoumbrella.ssfq.cn
http://www.dinnco.com/news/150514.html

相关文章:

  • 网站建设功能定位怎么写培训体系
  • 政府网站建设招标要求nba最新比赛直播
  • 外贸建设网站公司市场营销一般在哪上班
  • 建网站 备案百度问答平台
  • 做软件跟做网站哪个难sem竞价是什么
  • 陕西网站建设哪家好济南网站seo
  • 跨境电商单页网站的详情页怎么做的2024年新冠疫情最新消息今天
  • 代理平台有哪些北京seo培训
  • 自己做的网站跳转到购彩大厅品牌推广策略有哪些
  • 山西省建设信息网站产品怎么做推广和宣传
  • wordpress多个域名成都seo招聘
  • 花生棒做网站网络产品及其推广方法
  • 二类电商用网站怎么做H5页面提高搜索引擎排名
  • 网站建设有什么用爱战网关键词
  • 企业网站建设亮点百度快照在哪里
  • 网站改版具体建议重庆seo全面优化
  • 电子商务网站建设与管理百度搜索智能精选
  • 自己建设个小网站要什么游戏推广平台代理
  • 怎么自己优化网站如何创建个人网站免费
  • 推广型网站建设地址爱链接网如何使用
  • 如何做外贸网站优化推广seo查询工具有哪些
  • 李志自己做网站微信公众号推广方法有哪些
  • 做网站的公司那家好。seo技术外包 乐云践新专家
  • 南通专业网站制作廊坊seo推广
  • 阿里云备案网站负责人百度怎么发布自己的广告
  • 网站建设 印花税合肥网站建设
  • 做动态网站有哪些平台搜索引擎是软件还是网站
  • 深圳网站的设计公司营销培训讲师
  • 平湖网站制作河南网络推广公司
  • 网站建设的经验山东服务好的seo公司