A data structure for n elements and O(1) operations
来源:互联网 发布:淘宝买星空连门卡 编辑:程序博客网 时间:2024/05/18 00:54
GFG上的一道数据结构的题:
http://geeksquiz.com/data-structure-n-elements-o1-operations/
设计一个数据结构插入删除和查找都是O(1),存储 0 - n-1
最初想法是hashtable类似,基于链表,但是查找不可能为O(1), 看了gfg上的讲解才豁然开朗。
Solution:
就是用一个数组实现,因为要存的数已知,0 到n-1,
数字 i 存在就把A[i] 设为true,mark
不存在就设为false
实现非常简单了
boolean A[];public void initialize(int n){A = new boolean[n];Arrays.fill(A, false);//or no need to initialize in java }//Insert an elementpublic void insert(int i){A[i] = true; }//Delete an elementpublic void delete(int i){A[i] = false;}public int deletelement(int i){if(A[i] == true){A[i] = false;return 0;}else{return -1; // failure}}//Find an elementpublic boolean find(int i){return A[i];}
0 0
- A data structure for n elements and O(1) operations
- leetcode_Design and implement a data structure for Least Recently Used (LRU) cache
- [Seminar Presentation] DCell: A Scalable and Fault-Tolerant Network Structure for Data Centers
- [Python for Data Analysis]Python Basic2--Data Structure and Sequences
- Overview of solrconfig.xml 之 Common XML data-structure and type elements
- algorithm and data structure
- algorithm and data structure
- Data Structure and Algorithm
- data elements and domain
- All O`one Data Structure
- 【Data Structure & Algorithm】求1+2+…+n
- android data structure And algorithm
- Algorithm and Data Structure Review
- [Data Structure and Algorithm]--Heap
- 【Data Structures】 5. Stack—A limited data structure and LIFO
- ECC in OPEN SSL 1(background knowledge and data structure)
- Geometric and Topological Data Structure Presentation in Open Cascade [1]
- Overview: (1)Data Structure: Interval Tree and Poj3264
- trying to use a recycled bitmap解决之道
- java解惑之字符之谜(谜题21)
- mysql连接查询之间的差异性
- JavaMail学习笔记(三)、使用SMTP协议发送电子邮件(全)
- 基础总结篇之一:Activity生命周期
- A data structure for n elements and O(1) operations
- Xcode Instruments之iprofiler
- QtCreator生成excel文件
- (转)各大OJ题目分类!!
- IP cam 与模拟cam
- Android Fragment 真正的完全解析(下)
- Android TextView中文字通过SpannableString来设置超链接、颜色、字体等属性
- (转AS3)BitmapData.draw比BitmapData.copyPixel能做得更多
- Java内存溢出详解