【发布时间】:2018-03-27 10:43:35
【问题描述】:
我将如何实现一个在恒定时间内支持以下内容的数据结构。我在求职面试中遇到了这个问题,以下是我的解决方案。请检查我的方法,或者建议一种更好的替代方法(如果有的话)。
////////////////////////////////////////////////////////////////
// Implement a container that have the following methods:
// Java:
// public class Container<E>
// {
// // data memebers
// ...
// public Container();
// // constructor
// // Complexity: Constant
//
// public int size();
// // return the total number of elements in the container
// // Complexity: Constant
//
// public E get(int index);
// // get an element from the container by its index
// // Complexity: Constant
//
// public void set(int index, E element);
// // set an element in the container by its index. index >= 0 && index < size()
// // Complexity: Constant
//
// public void add_front (E element);
// // add a new element to the front of the container i.e. its index is 0
// // Complexity: Constant (amortized time)
//
// public void remove_front ();
// // remove the element at the front of the container
// // Complexity: Constant
//
// public void add_back (E element);
// // add a new element to the back of the container i.e. its index is size()
// // Complexity: Constant (amortized time)
//
// public void remove_back ();
// // remove the element at the back of the container
// // Complexity: Constant
// }
//
// Examples:
// at beginning => []
// add_front(0) => [0]
// add_front(-1) => [-1, 0]
// add_back(1) => [-1, 0, 1]
// add_back(2) => [-1, 0, 1, 2]
// get(0) => -1
// get(3) => 2
// set(3, 8) => [-1, 0, 1, 8]
// remove_front() => [0, 1, 8]
// remove_back() => [0, 1]
////////////////////////////////////////////////////////////////
我的方法 使用 Hashtable 存储值。 哈希表存储 = new Hashtable(); 有两个变量front和back,分别表示数据结构存储范围的开始和结束。
- add_front(E 元素)
low--; storage.put(low, element);
- add_back(E 元素)
高++; storage.put(high, element);
- public void remove_front();
低++;
- public void remove_back ();
高--;
- public E get(int index);
索引 = 索引低;返回 storage.get(index);
- public void set(int index, E element);
索引 = 索引低; storage.put(index, element);
- public int size();
返回高-低+1;
【问题讨论】:
-
如果你已经有一个可行的解决方案,你应该把它移到Code Review。
-
似乎更像是Code Review 的问题,而不是 Stack Overflow。请注意,哈希表并不是真正的
O(1)(尽管实际上它们工作得很好,并且它们的平均情况确实是O(1),但在最坏的情况下它们是O(n))。 -
哈希表并不意味着元素按照插入的顺序进行排序,那么如何使
add_front和add_back保持最新?我认为您需要一个哈希表和一个链表,一些查询/操作由其中一个处理,一些由另一个处理,并且每次操作它们时都需要保持同步(并且同步需要花费 O( 1)). -
@shmosel 移动了此代码审查 codereview.stackexchange.com/questions/177997/…
-
你的方法几乎行得通,但是你从哈希表得到的 O(1) expected 时间与 O(1) amoritized 时间不同i> 要求的时间。但是,即使它完全正确,它也不会是一个非常有效的实现。您想要的东西在 Java 中称为 ArrayDeque。您可能需要查看源代码。
标签: java algorithm data-structures big-o hashtable