什么数据结构为O(1)随机插入/缺失和O(1)随机访问? [英] What data structure for O(1) random insertion/deletion and O(1) random access?

查看:329
本文介绍了什么数据结构为O(1)随机插入/缺失和O(1)随机访问?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道用什么数据结构这个问题。我要结构有:

I don't know what data structure to use for this problem. I want the structure to have:


  • 恒定的时间插入或删除。

  • 将ID常量时间检索。

实际的系统是:

我有一堆具有唯一ID的每个对象。我的程序需要接收一个ID请求,并返回相关的对象。

I've got a bunch of objects each with a unique id. My program will need to receive requests for an id and return the relevant object.

每当收到我希望它的请求:搜索结构,看看它的存在。如果是,将其返回。如果不是,从盘到存储器加载(把它的结构,使得下一个被请求的时间不具有使用磁盘),然后返回。

Whenever it receives a request I want it to: search the structure to see if it's there. If it is, return it. If it isn't, load it from the disk into memory (put it in the structure so that next time it is requested it doesn't have to use the disk) and then return it.

我用C

下面是一个<一个href=\"http://stackoverflow.com/questions/1174852/what-is-the-best-data-structure-container-for-fast-element-insertion-deletion-b\">similar问题但我不知道它是多么重要。

Here's a similar question but I'm not sure how relevant it is.

推荐答案

一个哈希表可能是$ P $你的情况ptty很好的解决方案 - 即使它不​​是在O(1)时,有一个colision:这是一个非常有效的解决方案。

A Hash table might be a pretty good solution in your case -- even if it's not in O(1) when there's a colision : it's a quite efficient solution.

这篇关于什么数据结构为O(1)随机插入/缺失和O(1)随机访问?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆