380. Insert Delete GetRandom O(1)


經過昨天的消沉
image

今天我振作了
image

設計個數據結構,添加,刪除,隨機獲取都是O(1).
怎么會有這么牛逼的數據結構,所以肯定相應的要耗費空間。

添加和獲取耗時O(1)是Array的特性,或者說是Map/Table的特性,思考下php的array就明白其實是index的mapping了。
Random要求O(1)那就是需要知道數據結構的大小,並且保證儲存的元素是相鄰的。

其實就是一個table/map,KEY是添加的元素,value是他儲存在array中的位置;
然后一個array和上面的table/map對應;
再一個變量size記錄總共有多少個元素,便於random.

添加,直接添加到SIZE的位置,MAP里記錄,SIZE++
RANDOM,通過SIZE隨便RANDOM一個數,直接從ARRAY里直接獲取。
刪除,為了保證所有元素在ARRAY中是相鄰的,像LIST那樣。用ARRAY模擬就是刪除之后,后面所有的都前移,但是要求O(1),可以把最后一個元素和它換一下。換的時候相應的改變MAP/TABLE里的信息,刪除map里本來最后一個KEY(因為我們換到前面了),最后SIZE--,使得array[size]指向的位置雖然不為空,但是是標記為刪除的元素,就是剛才換過來的,而RANDOM不會影響。

感覺和學習哦啊以前做過的用JAVA實現PHP ARRAY的作業有點像,只不過那個要自己寫hash function

為了圖省事不resize array,用了arrayList,但是意思是那個意思。。

public class RandomizedSet {


    Map<Integer,Integer> map;
    List<Integer> list;
    int size;

    /** Initialize your data structure here. */
    public RandomizedSet() 
    {
        map = new HashMap<Integer,Integer>();
        list = new ArrayList<Integer>();
        this.size = 0;
    }
    
    /** Inserts a value to the set. Returns true if the set did not already contain the specified element. */
    public boolean insert(int val) 
    {
        if(map.containsKey(val)) return false;
        else
        {
            list.add(size,val);
            map.put(val,size++);
            return true;
        }
    }
    

    
    /** Removes a value from the set. Returns true if the set contained the specified element. */
    public boolean remove(int val) 
    {
        if(!map.containsKey(val)) return false;
        else if(size == 0) map.remove(val);
        else
        {
            int tailKey = list.get(size-1);
            map.put(tailKey,map.get(val));
            list.set(map.get(val),tailKey);
            size--;
            map.remove(val);
              
        }
        return true; 
    }
    
    /** Get a random element from the set. */
    public int getRandom() 
    {
        Random rdm = new Random();
        return list.get(rdm.nextInt(size));
    }
}


二刷。

用個Map,KEY是存的元素,VAL是元素存在arraylist里的位置。

刪除是把arraylist里最后一個有效元素和刪除的元素調換,同時修改map里被最后一個有效元素(key)的相應位置(value)。。

public class RandomizedSet {
    
    List<Integer> list;
    int num;
    Map<Integer, Integer> map;
    
    /** Initialize your data structure here. */
    public RandomizedSet() {
        list = new ArrayList<>();
        num = 0;
        map = 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 (map.containsKey(val)) {
            return false;
        } else {
            list.add(num, val);
            map.put(val, num++);
            return true;
        }
    }
    
    /** Removes a value from the set. Returns true if the set contained the specified element. */
    public boolean remove(int val) {
        if (!map.containsKey(val)) {
            return false;
        } else if (num == 0) {
            map.remove(val);
            return true;
        } else {
            int removedIndex = map.get(val);
            int backElement = list.get(num - 1);
            map.put(backElement, removedIndex);
            list.set(removedIndex, backElement);
            num--;
            map.remove(val);
            return true;
        }
            
    }
    
    /** Get a random element from the set. */
    public int getRandom() {
        Random rdm = new Random();
        return list.get(rdm.nextInt(num));
    }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM