380. Insert Delete GetRandom O(1)

来源:互联网 发布:新手怎样开淘宝店 编辑:程序博客网 时间:2024/06/06 02:51

Design a data structure that supports all following operations in average O(1) time.

  1. insert(val): Inserts an item val to the set if not already present.
  2. remove(val): Removes an item val from the set if present.
  3. getRandom: Returns a random element from current set of elements. Each element must have the same probability of being returned. 
  4. // 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();
  5. 这道题刚开始做的时候大意了,没有太明白题目意思,常用数据结构中基本都能完成常量时间内添加元素功能,但是在常量时间内删除元素功能例如数组就不能完成,因此需要诸如Map、Set这类可以在常量时间内定位到元素的数据结构,另外一个要求是需要返回随机元素,这时就需要我们的元素有一定顺序,因此需要类似于数组之类的数据结构,最后选用HashMap和ArrayList完成该数据结构的创建,代码如下:
  6. ArrayList<Integer> arrayList = null;    HashMap<Integer,Integer> hashMap = null;        /** Initialize your data structure here. */    public RandomizedSet() {        arrayList = new ArrayList<Integer>();        hashMap = new HashMap<Integer, Integer>();    }        /** Inserts a value to the set. Returns true if the set did not already contain the specified element. */    public boolean insert(int val) {        boolean result = hashMap.containsKey(val);        if(result){            return false;        }else{            hashMap.put(val, arrayList.size());            arrayList.add(val);            return true;        }    }        /** Removes a value from the set. Returns true if the set contained the specified element. */    public boolean remove(int val) {        boolean result = hashMap.containsKey(val);        int pos = 0;        if(result){            pos = hashMap.get(val);            arrayList.set(pos, arrayList.get(arrayList.size() - 1));            hashMap.put(arrayList.get(pos), pos);        }else{            return false;        }        hashMap.remove(val);        arrayList.remove(arrayList.size() - 1);        return true;    }        /** Get a random element from the set. */    public int getRandom() {        // Random rand = new Random();        // return arrayList.get(rand.nextInt(arrayList.size()));        int num = (int)(Math.random() * arrayList.size());        return arrayList.get(num);    }
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 b2驾驶证扣11分怎么办 驾照一共扣30分怎么办 驾照扣了100分怎么办 驾照扣40多分怎么办 驾照被扣69分怎么办 驾照被扣200多分怎么办 b2驾照扣了分怎么办 驾驶证在外地被冒用怎么办 身份证被冒用办驾照怎么办 外国人在中国境内死了怎么办 台风来临时航船应该怎么办 越南攻占太平岛台湾怎么办 厨房下水pvc管三通漏水怎么办 pvc下水管道接口漏水怎么办 马航机场转机行李怎么办 习惯了光脚开车怎么办 遇到暴恐分子时该怎么办视频 中国人在菲律宾失踪了怎么办 高跟凉鞋前面磨脚怎么办 小孩子偷邻居家的钱怎么办 碰见美国人说叙利亚诈骗团伙怎么办 日服的火焰纹章闪退怎么办 冒险岛没潜能的怎么办 冒险岛法师运气不够怎么办 cad文件打开老是闪退白屏怎么办 上古卷轴5坏档怎么办 冒险岛2 65级后怎么办 第二次起诉离婚对方不同意怎么办 起诉离婚对方不同意离婚怎么办 我12爸妈离婚该怎么办 苹果迅雷下载的链接打不开怎么办 苹果上装的迅雷打不开怎么办 mp4进水水干了后打不开怎么办 迅雷闪退ios10.3怎么办 用辣椒辣到眼睛怎么办 辣椒辣到眼皮了怎么办 眼被辣椒辣了怎么办 老公欠银行的钱怎么办 离婚老公欠的钱怎么办 欠了几十万债怎么办 执行死刑时已怀孕怎么办