657
来源:互联网 发布:java 数据库 proxy 编辑:程序博客网 时间:2024/06/08 13:25
2017.9.12
。。。。。感觉自己又违背了这题的初衷。
网上很多方法是将HashMap 和 List 结合使用的。
一个用来保证在添加和删除时的速度,
一个用来保证在生成随机谁时的速度。
道理我都懂啊,可是为什么只用ArrayList就可以了呢。
只是ArrayList.remove的时候。如果参数类型是object才会移除这个数据。如果参数类型是int型的话,就会int 就会作为index了。
public class RandomizedSet { public ArrayList<Integer> list; public Random r;public RandomizedSet() { list = new ArrayList<Integer>(); r = new Random(); } // Inserts a value to the set // Returns true if the set did not already contain the specified element or false public boolean insert(int val) { // Write your code here if(list.contains(val)){ return false; } else{ list.add(val); return true; } } // Removes a value from the set // Return true if the set contained the specified element or false public boolean remove(int val) { // Write your code here if(!list.contains(val)){ return false; } else{ list.remove(Integer.valueOf(val)); return true; } } // Get a random element from the set public int getRandom() { // Write your code here int index = r.nextInt(list.size()); return list.get(index); }}/** * Your RandomizedSet object will be instantiated and called as such: * RandomizedSet obj = new RandomizedSet(); * boolean param = obj.insert(val); * boolean param = obj.remove(val); * int param = obj.getRandom(); */
阅读全文
0 0
- 657
- uva 657
- Uva 657
- uva 657
- uva 657
- UVA 657
- UVA 657 掷骰子
- uva 657 poj 1481
- uva 657(dfs)
- UVa 657 掷色子
- 657 - The die is cast(****)
- 657 - The die is cast
- 657 - The die is cast
- 657 - The die is cast
- 657 - The die is cast
- TopCoder SRM 657 DIV 1
- 657 - The die is cast
- 【LeetCode】657 Judge Route Circle
- docker问题汇总
- 使用Mockito进行单元测试【1】——mock and verify
- 数十种TensorFlow实现案例汇集:代码+笔记
- esriControlsMousePointer
- python生成器小结
- 657
- 爬虫入门五(Phantomjs和selenium)
- ThinkPHP 导出数据到Execel文件中
- Spring七大模块之Core
- Oracle数据泵-schema导入导出
- 关于大端模式(big-endian)与小端模式(little-endian)
- 如何使用python+selenium向富文本编辑器输入内容
- RNN详解
- 最近打算开始学游戏开发,故此先记录一些Unity的学习网站