成人无码视频,亚洲精品久久久久av无码,午夜精品久久久久久毛片,亚洲 中文字幕 日韩 无码

資訊專欄INFORMATION COLUMN

leetcode380. Insert Delete GetRandom O(1)

phoenixsky / 1280人閱讀

摘要:題目要求設(shè)計一個數(shù)據(jù)結(jié)構(gòu),使得能夠在的時間復(fù)雜度中插入數(shù)字,刪除數(shù)字,以及隨機獲取一個數(shù)字。因此,使用來查詢時不可避免的。如何實現(xiàn)的隨機查詢這個其實就是強調(diào)一點,我們需要維持原有的插入順序,從而保證各個元素等概率被隨機。

題目要求
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è)計一個數(shù)據(jù)結(jié)構(gòu),使得能夠在O(1)的時間復(fù)雜度中插入數(shù)字,刪除數(shù)字,以及隨機獲取一個數(shù)字。要求所有的數(shù)字都能夠被等概率的隨機出來。

思路和代碼

其實有幾個思路入手:

如何實現(xiàn)O(1)的插入

這里數(shù)字的插入還需要能夠去重,即需要首先判斷該數(shù)字是否已經(jīng)存在,已經(jīng)存在的話就不執(zhí)行任何插入操作。如果底層是一個一般的數(shù)組,我們知道查詢的時間復(fù)雜度為O(n),明顯不滿足題目的意思。一個有序的數(shù)組能夠?qū)⒉樵兊臅r間復(fù)雜度下降到O(lgn),但是這依然不滿足條件1,而且也無法做到所有的元素被等概率的查詢出來,因為每插入一個元素都將改動之前元素的位置。而唯一能夠做到O(1)時間查詢的只有一個數(shù)據(jù)結(jié)構(gòu),即hash。因此,使用hash來查詢時不可避免的。

如何實現(xiàn)O(1)的刪除

這個其實是一個很經(jīng)典的問題了,只要能夠利用hash在O(1)的時間內(nèi)找到這個數(shù)字的位置,就有兩種方法來實現(xiàn)O(1)的刪除,一個是利用偽刪除,即每一個位置都對應(yīng)一個狀態(tài)為,將狀態(tài)位社會為已經(jīng)刪除即可,還有一種就更有意思,就是將被刪除位替換為數(shù)組最后一位的值,然后只需要刪除最后一位就行。這種刪除就無需將刪除位右側(cè)的元素全部左移造成O(n)的時間復(fù)雜度。這里我們采用的是第二種方法。

如何實現(xiàn)O(1)的隨機查詢

這個其實就是強調(diào)一點,我們需要維持原有的插入順序,從而保證各個元素等概率被隨機。

綜上所述,我們底層需要兩種數(shù)據(jù)結(jié)構(gòu),一個hashmap來支持O(1)的查詢,以及一個list來支持隨機數(shù)的獲取。代碼實現(xiàn)如下:

public class InsertDeleteGetRandom_380 {
    private List list;
    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)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://hztianpu.com/yun/73273.html

相關(guān)文章

  • Insert Delete GetRandom O(1) & Duplicates allo

    摘要:思路可以實現(xiàn)時間復(fù)雜度的和,但是要求也是,只用是不可以的。但是在里面查找的時間復(fù)雜度依然是,可以想到用來記錄對應(yīng)的,這樣查找的時間也是常數(shù)。用可以保持順序,但是的時間復(fù)雜度是。 380. Insert Delete GetRandom O(1) Design a data structure that supports all following operations in aver...

    2shou 評論0 收藏0
  • leetcode381. Insert Delete GetRandom O(1) - Duplic

    摘要:題目要求設(shè)計一個數(shù)據(jù)結(jié)構(gòu),支持能夠在的時間內(nèi)完成對數(shù)字的插入,刪除和獲取隨機數(shù)的操作,允許插入重復(fù)的數(shù)字,同時要求每個數(shù)字被隨機獲取的概率和該數(shù)字當前在數(shù)據(jù)結(jié)構(gòu)中的個數(shù)成正比。網(wǎng)上有一些實現(xiàn)采用來解決,這是不合理的。此時的代碼如下 題目要求 Design a data structure that supports all following operations in average...

    h9911 評論0 收藏0
  • Design Phone Directory

    摘要:題目鏈接直接用一個,結(jié)果了看了加了個,不過感覺沒什么必要加,反正保存的都一樣,只是的時間大于,用可以保證。看了題目條件是可以隨便返回一個值,但是不讓這么做。很無語啊如果這道題要求要求的是的,那就和一樣了。 Design Phone Directory 題目鏈接:https://leetcode.com/problems... 直接用一個set,結(jié)果tle了= = public clas...

    NicolasHe 評論0 收藏0
  • 哈希函數(shù)與哈希表

    摘要:哈希函數(shù)與哈希表一哈希函數(shù)哈希函數(shù)性質(zhì)輸入域是無窮的輸出域有窮的當輸入?yún)?shù)固定的情況下,返回值一定一樣當輸入不一樣,可能得到一樣的值。 哈希函數(shù)與哈希表 一、哈希函數(shù) 1.1 哈希函數(shù)性質(zhì): input輸入域是無窮的 output輸出域有窮的 當輸入?yún)?shù)固定的情況下,返回值一定一樣 當輸入不一樣,可能得到一樣的值。(必然會出現(xiàn),因為輸入域很大,輸出域很小),產(chǎn)生哈希碰撞 均勻分布的特...

    Rainie 評論0 收藏0
  • LeetCode[72] Edit Distance

    摘要:復(fù)雜度思路考慮用二維來表示變換的情況。如果兩個字符串中的字符相等,那么如果兩個字符串中的字符不相等,那么考慮不同的情況表示的是,從字符串到的位置轉(zhuǎn)換到字符串到的位置,所需要的最少步數(shù)。 LeetCode[72] Edit Distance Given two words word1 and word2, find the minimum number of steps require...

    call_me_R 評論0 收藏0

發(fā)表評論

0條評論

閱讀需要支付1元查看
<