摘要:題目要求設(shè)計(jì)一個(gè)數(shù)據(jù)結(jié)構(gòu),使得能夠在的時(shí)間復(fù)雜度中插入數(shù)字,刪除數(shù)字,以及隨機(jī)獲取一個(gè)數(shù)字。因此,使用來查詢時(shí)不可避免的。如何實(shí)現(xiàn)的隨機(jī)查詢這個(gè)其實(shí)就是強(qiáng)調(diào)一點(diǎn),我們需要維持原有的插入順序,從而保證各個(gè)元素等概率被隨機(jī)。
題目要求
Design a data structure that supports all following operations in average O(1) time. insert(val): Inserts an item val to the set if not already present. remove(val): Removes an item val from the set if present. getRandom: Returns a random element from current set of elements. Each element must have the same probability of being returned. Example: // Init an empty set. RandomizedSet randomSet = new RandomizedSet(); // Inserts 1 to the set. Returns true as 1 was inserted successfully. randomSet.insert(1); // Returns false as 2 does not exist in the set. randomSet.remove(2); // Inserts 2 to the set, returns true. Set now contains [1,2]. randomSet.insert(2); // getRandom should return either 1 or 2 randomly. randomSet.getRandom(); // Removes 1 from the set, returns true. Set now contains [2]. randomSet.remove(1); // 2 was already in the set, so return false. randomSet.insert(2); // Since 2 is the only number in the set, getRandom always return 2. randomSet.getRandom();
設(shè)計(jì)一個(gè)數(shù)據(jù)結(jié)構(gòu),使得能夠在O(1)的時(shí)間復(fù)雜度中插入數(shù)字,刪除數(shù)字,以及隨機(jī)獲取一個(gè)數(shù)字。要求所有的數(shù)字都能夠被等概率的隨機(jī)出來。
思路和代碼其實(shí)有幾個(gè)思路入手:
如何實(shí)現(xiàn)O(1)的插入這里數(shù)字的插入還需要能夠去重,即需要首先判斷該數(shù)字是否已經(jīng)存在,已經(jīng)存在的話就不執(zhí)行任何插入操作。如果底層是一個(gè)一般的數(shù)組,我們知道查詢的時(shí)間復(fù)雜度為O(n),明顯不滿足題目的意思。一個(gè)有序的數(shù)組能夠?qū)⒉樵兊臅r(shí)間復(fù)雜度下降到O(lgn),但是這依然不滿足條件1,而且也無法做到所有的元素被等概率的查詢出來,因?yàn)槊坎迦胍粋€(gè)元素都將改動(dòng)之前元素的位置。而唯一能夠做到O(1)時(shí)間查詢的只有一個(gè)數(shù)據(jù)結(jié)構(gòu),即hash。因此,使用hash來查詢時(shí)不可避免的。
如何實(shí)現(xiàn)O(1)的刪除這個(gè)其實(shí)是一個(gè)很經(jīng)典的問題了,只要能夠利用hash在O(1)的時(shí)間內(nèi)找到這個(gè)數(shù)字的位置,就有兩種方法來實(shí)現(xiàn)O(1)的刪除,一個(gè)是利用偽刪除,即每一個(gè)位置都對(duì)應(yīng)一個(gè)狀態(tài)為,將狀態(tài)位社會(huì)為已經(jīng)刪除即可,還有一種就更有意思,就是將被刪除位替換為數(shù)組最后一位的值,然后只需要?jiǎng)h除最后一位就行。這種刪除就無需將刪除位右側(cè)的元素全部左移造成O(n)的時(shí)間復(fù)雜度。這里我們采用的是第二種方法。
如何實(shí)現(xiàn)O(1)的隨機(jī)查詢這個(gè)其實(shí)就是強(qiáng)調(diào)一點(diǎn),我們需要維持原有的插入順序,從而保證各個(gè)元素等概率被隨機(jī)。
綜上所述,我們底層需要兩種數(shù)據(jù)結(jié)構(gòu),一個(gè)hashmap來支持O(1)的查詢,以及一個(gè)list來支持隨機(jī)數(shù)的獲取。代碼實(shí)現(xiàn)如下:
public class InsertDeleteGetRandom_380 { private Listlist; private Map hash; public InsertDeleteGetRandom_380() { list = new ArrayList (); hash = new HashMap (); } /** Inserts a value to the set. Returns true if the set did not already contain the specified element. */ public boolean insert(int val) { if(hash.containsKey(val)) { return false; } list.add(val); hash.put(val, list.size()-1); return true; } /** Removes a value from the set. Returns true if the set contained the specified element. */ public boolean remove(int val) { if(!hash.containsKey(val)){ return false; } int position = hash.get(val); if(position != list.size()-1) { int last = list.get(list.size()-1); list.set(position, last); hash.put(last, position); } list.remove(list.size()-1); hash.remove(val); return true; } /** Get a random element from the set. */ public int getRandom() { int position = (int)Math.floor((Math.random() * list.size())); return list.get(position); } }
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/73273.html
摘要:思路可以實(shí)現(xiàn)時(shí)間復(fù)雜度的和,但是要求也是,只用是不可以的。但是在里面查找的時(shí)間復(fù)雜度依然是,可以想到用來記錄對(duì)應(yīng)的,這樣查找的時(shí)間也是常數(shù)。用可以保持順序,但是的時(shí)間復(fù)雜度是。 380. Insert Delete GetRandom O(1) Design a data structure that supports all following operations in aver...
摘要:題目要求設(shè)計(jì)一個(gè)數(shù)據(jù)結(jié)構(gòu),支持能夠在的時(shí)間內(nèi)完成對(duì)數(shù)字的插入,刪除和獲取隨機(jī)數(shù)的操作,允許插入重復(fù)的數(shù)字,同時(shí)要求每個(gè)數(shù)字被隨機(jī)獲取的概率和該數(shù)字當(dāng)前在數(shù)據(jù)結(jié)構(gòu)中的個(gè)數(shù)成正比。網(wǎng)上有一些實(shí)現(xiàn)采用來解決,這是不合理的。此時(shí)的代碼如下 題目要求 Design a data structure that supports all following operations in average...
摘要:題目鏈接直接用一個(gè),結(jié)果了看了加了個(gè),不過感覺沒什么必要加,反正保存的都一樣,只是的時(shí)間大于,用可以保證??戳祟}目條件是可以隨便返回一個(gè)值,但是不讓這么做。很無語啊如果這道題要求要求的是的,那就和一樣了。 Design Phone Directory 題目鏈接:https://leetcode.com/problems... 直接用一個(gè)set,結(jié)果tle了= = public clas...
摘要:哈希函數(shù)與哈希表一哈希函數(shù)哈希函數(shù)性質(zhì)輸入域是無窮的輸出域有窮的當(dāng)輸入?yún)?shù)固定的情況下,返回值一定一樣當(dāng)輸入不一樣,可能得到一樣的值。 哈希函數(shù)與哈希表 一、哈希函數(shù) 1.1 哈希函數(shù)性質(zhì): input輸入域是無窮的 output輸出域有窮的 當(dāng)輸入?yún)?shù)固定的情況下,返回值一定一樣 當(dāng)輸入不一樣,可能得到一樣的值。(必然會(huì)出現(xiàn),因?yàn)檩斎胗蚝艽?,輸出域很?,產(chǎn)生哈希碰撞 均勻分布的特...
摘要:復(fù)雜度思路考慮用二維來表示變換的情況。如果兩個(gè)字符串中的字符相等,那么如果兩個(gè)字符串中的字符不相等,那么考慮不同的情況表示的是,從字符串到的位置轉(zhuǎn)換到字符串到的位置,所需要的最少步數(shù)。 LeetCode[72] Edit Distance Given two words word1 and word2, find the minimum number of steps require...
閱讀 3472·2023-04-25 19:39
閱讀 3823·2021-11-18 13:12
閱讀 3645·2021-09-22 15:45
閱讀 2443·2021-09-22 15:32
閱讀 746·2021-09-04 16:40
閱讀 3741·2019-08-30 14:11
閱讀 1896·2019-08-30 13:46
閱讀 1578·2019-08-29 15:43