数据结构问题-逆时间顺序
我在一次面试中被问到一个与数据结构相关的问题。
问题:获取数据流,并且应该以相反的时间顺序显示。 没有重复项。
1. Which data structure to use?
2. What would be your solution approach?
Example
Data Output
(first set of data)
A B -> A B
(new streamed data i.e. D E arrives)
D E -> D E A B
(new streamed data i.e. A F arrives)
A F -> A F D E B
有人可以分享一些知识吗?
我的做法:
数据结构:数组
算法
- 创建一个空数组
- 将传入的数据插入列表顶部
- 当新数据到达时,进行线性搜索。
- 如果数据已存在,则将其删除并将新数据插入到列表顶部。
- 重复
I was asked a question in an interview related to the data structure.
Problem: Getting stream of data and it should be shown in the reverse chronological order.
No duplicates.
1. Which data structure to use?
2. What would be your solution approach?
Example
Data Output
(first set of data)
A B -> A B
(new streamed data i.e. D E arrives)
D E -> D E A B
(new streamed data i.e. A F arrives)
A F -> A F D E B
Can someone please share some knowledge?
My approach:
Data structure: Array
Algorithm
- Create an empty array
- Insert the incoming data into the top of the list
- When new data arrives, do a linear search.
- If data is already present then remove it and insert the new data on top of the list.
- Repeat
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我认为最好的解决方案是指向链表节点的指针的哈希映射,我将解释:
哈希映射用于在 O(1) 时间内搜索,
在哈希映射中,键将是data,values 将是指向链表中带有 key 值的节点的指针。
使用哈希映射,可以查找当前链表中是否存在某个元素(在 O(1) 时间内),并在 O(1) 时间内更改元素的位置(使用指向该元素的指针的优点)链表中的节点)
总的来说,新数据的更新将是 O(1) 时间,而数据结构是 O(n) 空间。
如果答案中的某些内容不满意,请务必发表评论:)
I think the best soluttion would be an Hash-map of pointer to linked-list Nodes, I'll explain:
The hash-map is used for searching in O(1) time,
In the hash-map, the keys will be the data, and the values will be the pointer to the node in the linked list with the value of the key.
With the hash-map, it's possible to find if an element is present in the current linked list (in O(1) time), and changing the position of an element in O(1) time (the positive of using a pointer to a node in a linked list)
Over all, the Update of the new data will be O(1) time and the data structures are O(n) space.
If something in the answer is unsatisfying, be sure to leave a comment :)
对于订单逆转,经典数据结构为a stack 最后)。
将新条目推到堆栈的顶部。最后,堆栈条目以相反的顺序弹出。
为了防止重复项,可以使用 hashset 。每当新的条目到达时,都会计算其哈希签名。如果哈希集合中存在哈希签名,则该条目将被封锁为重复。否则,将哈希注册为集合的新成员,并将条目推到堆栈。
与线性搜索相比,标签更快。但这需要额外的记忆。
For order reversal, the classical data structure is a stack (FILO = first in, last out).
New entries are pushed on top of the stack. In the end, the stack entries are popped off in reverse order.
To prevent duplicates, one could use a hashset. Whenever a new entry arrives, its hash signature is computed. If the hash signature is present in the hash set, the entry is blocked as duplicate. Otherwise, the hash is registered as new member of the set and the entry is pushed to the stack.
Compared to linear search, the hashset is faster. But it requires extra memory.
我认为一个简单的解决方案是使用hashmap使用双重链接列表。
o(1)
如果已经存在元素,请检查列表中的存在o(1)
从旧位置删除由于您有next
和prev
在双重链接列表节点中的指针)和o(1)
o(1)
在列表中插入o(n)
遍历以使所有元素按顺序获取I think an easy solution for this would be to use a doubly linked list with a hashmap.
O(1)
check for an element's presence in the listO(1)
removal from the old position (no need to traverse the entire list since you havenext
andprev
pointers in doubly linked list nodes) andO(1)
insertion at the list headO(1)
insertion at the list headO(N)
traversal to get all elements in order当显示没有重复的输出时,我将使用数组的数组来存储数据流和哈希集结构。
通过推送和弹出来维持逆时间顺序。并且重复项会被集合内检查跳过。考虑到哈希集包含测试的复杂度为 O(1),复杂度与传入元素的数量成线性关系。
这是我的想法,但也许这太天真了。欢迎评论。
I would use an array of arrays to store the data stream, and hashset structure when showing the output with no duplicate.
Reverse-chronological order is maintained by push and pop. And duplicates are skipped by the in-set check. Given that hashset inclusion test is O(1), the complexity is linear in the number of incoming elements.
This is what I thought, but maybe this is too naive. Comments are welcome.
这可以通过哈希在
O(n)
空间和O(1)
更新时间内解决,其中键是数据,值是表示数据的元组之前的数据和之后的数据;以及存储第一个数据的变量。This can be solved in
O(n)
space andO(1)
update-time with a hash where the key is the datum, and the value is a tuple representing the datum before and the datum after; and a variable that stores the first datum.