981. 基于時間的鍵值存儲

創建一個基于時間的鍵值存儲類 TimeMap,它支持下面兩個操作:

  1. set(string key, string value, int timestamp)

存儲鍵 key、值 value,以及給定的時間戳 timestamp。

  1. get(string key, int timestamp)

返回先前調用 set(key, value, timestamp_prev) 所存儲的值,其中 timestamp_prev <= timestamp。
如果有多個這樣的值,則返回對應最大的 timestamp_prev 的那個值。
如果沒有值,則返回空字符串("")。

解題思路

map

使用map記錄鍵值對,因為根據時間戳的不同,將會產生多個鍵值對,所以value需要用一個列表來保存,并且因為時間戳不是連續的,所以我們在對應的value上面還要注明時間戳

二分查找

在通過key,定位到對應的value列表以后,因為所有 TimeMap.set 操作中的時間戳 timestamps 都是嚴格遞增的。所以可以使用二分查找,找出滿足timestamp_prev <= timestamp的value

代碼

    class TimeMap {Map<String,List<String[]>> map;/** Initialize your data structure here. */public TimeMap() {map=new HashMap<>();}public void set(String key, String value, int timestamp) {if(!map.containsKey(key))map.put(key,new ArrayList<>());map.get(key).add(new String[]{String.valueOf(timestamp),value});}public String get(String key, int timestamp) {if(!map.containsKey(key))return "";List<String[]> list = map.get(key);int l=0,r=list.size()-1;if(timestamp<Integer.parseInt(list.get(0)[0]))return "";//[]while (l<=r){int mid=(r-l)/2+l;if(Integer.parseInt(list.get(mid)[0])>timestamp){r=mid-1;}else l=mid+1;}return list.get(r)[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);*/

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/390533.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/390533.shtml
英文地址,請注明出處:http://en.pswp.cn/news/390533.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

前端開發-DOM

文檔對象模型&#xff08;Document Object Model&#xff0c;DOM&#xff09;是一種用于HTML和XML文檔的編程接口。它給文檔提供了一種結構化的表示方法&#xff0c;可以改變文檔的內容和呈現方式。我們最為關心的是&#xff0c;DOM把網頁和腳本以及其他的編程語言聯系了起來。…

css 繪制三角形_解釋CSS形狀:如何使用純CSS繪制圓,三角形等

css 繪制三角形Before we start. If you want more free content but in video format. Dont miss out on my Youtube channel where I publish weekly videos on FrontEnd coding. https://www.youtube.com/user/Weibenfalk----------Are you new to web development and CSS?…

密碼學基本概念(一)

區塊鏈兄弟社區&#xff0c;區塊鏈技術專業問答先行者&#xff0c;中國區塊鏈技術愛好者聚集地 作者&#xff1a;于中陽 來源&#xff1a;區塊鏈兄弟 原文鏈接&#xff1a;http://www.blockchainbrother.com/article/72 著權歸作者所有。商業轉載請聯系作者獲得授權&#xff0c…

JAVA-初步認識-第十三章-多線程(驗證同步函數的鎖)

一. 至于同步函數用的是哪個鎖&#xff0c;我們可以驗證一下&#xff0c;借助原先賣票的例子 對于程序中的num&#xff0c;從100改為400&#xff0c;DOS的結果顯示的始終都是0線程&#xff0c;票號最小都是1。 票號是沒有問題的&#xff0c;因為同步了。 有人針對只出現0線程&a…

追求卓越追求完美規范學習_追求新的黃金比例

追求卓越追求完美規范學習The golden ratio is originally a mathematical term. But art, architecture, and design are inconceivable without this math. Everyone aspires to golden proportions as beautiful and unattainable perfection. By visualizing data, we chal…

leetcode 275. H 指數 II

給定一位研究者論文被引用次數的數組&#xff08;被引用次數是非負整數&#xff09;&#xff0c;數組已經按照 升序排列 。編寫一個方法&#xff0c;計算出研究者的 h 指數。 h 指數的定義: “h 代表“高引用次數”&#xff08;high citations&#xff09;&#xff0c;一名科研…

Node js開發中的那些旮旮角角 第一部

#前戲 上一周是我到現公司來最忙碌的&#xff08;最有意思的&#xff09;一周了&#xff0c;為什么這么說呢&#xff1f;因為項目中需要提供服務端對用戶病人信息的一個匯總并以email的形式分享信息的接口&#xff0c;在幾天的時間里調研處理一套實施方案。我們服務端是Node.js…

文件2. 文件重命名

servlet對本機已存在的文件進行重命名。 .jsp界面 1 <form action"<%basePath %>fileAction" method"get" >2 <table>3 <tr>4 <td>輸入文件路徑</td>5 <td&…

js字符串slice_JavaScript子字符串示例-JS中的Slice,Substr和Substring方法

js字符串sliceIn daily programming, we often need to work with strings. Fortunately, there are many built-in methods in JavaScript that help us while working with arrays, strings and other data types. We can use these methods for various operations like sea…

leetcode 218. 天際線問題

城市的天際線是從遠處觀看該城市中所有建筑物形成的輪廓的外部輪廓。給你所有建筑物的位置和高度&#xff0c;請返回由這些建筑物形成的 天際線 。 每個建筑物的幾何信息由數組 buildings 表示&#xff0c;其中三元組 buildings[i] [lefti, righti, heighti] 表示&#xff1a…

[Android Pro] 終極組件化框架項目方案詳解

cp from : https://blog.csdn.net/pochenpiji159/article/details/78660844 前言 本文所講的組件化案例是基于自己開源的組件化框架項目github上地址github.com/HelloChenJi…其中即時通訊(Chat)模塊是單獨的項目github上地址github.com/HelloChenJi… 1.什么是組件化&#xff…

如何寫一個vue指令directive

舉個例子 &#xff1a;clickoutside.js const clickoutsideContext clickoutsideContext;export default {/*param el 指令所綁定的元素param binding {Object} param vnode vue編譯生成的虛擬節點*/bind (el, binding, vnode) {const documentHandler function(e) {console.…

安裝angular cli_Angular 9適用于初學者—如何使用Angular CLI安裝第一個應用程序

安裝angular cliAngular is one of the most popular JavaScript frameworks created and developed by Google. In the last couple of years, ReactJS has gained a lot of interest and has become the most popular modern JS library in the industry. But this doesn’t …

leetcode 1818. 絕對差值和

給你兩個正整數數組 nums1 和 nums2 &#xff0c;數組的長度都是 n 。 數組 nums1 和 nums2 的 絕對差值和 定義為所有 |nums1[i] - nums2[i]|&#xff08;0 < i < n&#xff09;的 總和&#xff08;下標從 0 開始&#xff09;。 你可以選用 nums1 中的 任意一個 元素來…

【轉載】keil5中加入STM32F10X_HD,USE_STDPERIPH_DRIVER的原因

初學STM32&#xff0c;在RealView MDK 環境中使用STM32固件庫建立工程時&#xff0c;初學者可能會遇到編譯不通過的問題。出現如下警告或錯誤提示&#xff1a; warning: #223-D: function "assert_param" declared implicitly;assert_param(IS_GPIO_ALL_PERIPH(GPIOx…

下崗職工_下崗后我如何獲得多位軟件工程師的面試

下崗職工“Opportunities to find our deeper powers come when life seems most challenging.” -Joseph Campbell“當生活似乎最具挑戰性時&#xff0c;就有機會找到我們更深層的力量。” 約瑟夫坎貝爾 I was recently laid off for the first time in my life. I realized t…

1846. 減小和重新排列數組后的最大元素

給你一個正整數數組 arr 。請你對 arr 執行一些操作&#xff08;也可以不進行任何操作&#xff09;&#xff0c;使得數組滿足以下條件&#xff1a; arr 中 第一個 元素必須為 1 。任意相鄰兩個元素的差的絕對值 小于等于 1 &#xff0c;也就是說&#xff0c;對于任意的 1 <…

bashdb常用命令

一、列出代碼和查詢代碼類&#xff1a; l 列出當前行以下的10行- 列出正在執行的代碼行的前面10行. 回到正在執行的代碼行w 列出正在執行的代碼行前后的代碼/pat/ 向后搜索pat&#xff1f;pat&#xff1f;向前搜索pat二、Debug控制類&#xff1a; h 幫助help 命令 得到…

podcast播客資源_為什么播客是我的新維基百科-完美的非正式學習資源

podcast播客資源In this article, I’ll explain why podcasts replaced a lot of my Wikipedia usage for informal learning. I’ll also talk about how I listen to 5 hours of podcasts every day.在本文中&#xff0c;我將解釋為什么播客代替了我的許多Wikipedia用于非正…