forked from wisdompeak/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2a920e9
commit addd70d
Showing
1 changed file
with
10 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
### 432.All-O-one-Data-Structure | ||
|
||
此题和[146.LRU](https://github.com/wisdompeak/LeetCode/tree/master/Design/146.LRU-Cache)和[460.LFU](https://github.com/wisdompeak/LeetCode/tree/master/Design/460.LFU-Cache)的解法非常相似.这类问题的关键点就是,如果用o(1)的时间复杂度对val进行实时的排序.方法是用两个数据结构 | ||
```cpp | ||
list<int>ListNum; | ||
unordered_map<val,list<int>iterator>Num2Iter; | ||
``` | ||
其中ListNum是一个链表,存储着顺序的value.只要给出了迭代器的位置(可以理解为指针),那么在特定位置的插入和删除都是o(1)的操作.那么假设已知value,如何在这个链表里找到这个对应value的位置呢?就需要另一个Hash表Num2Iter来存储.总结来说,就是给出一个value,那么Num2Iter[value]给出了这个value在链表里的位置,可以进行删除或添加的操作. | ||
|
||
为什么ListNum可以一直保持是顺序的呢?对于任何一个key的操作,题意要求就是将其value增(或者减)一.假设我们在链表中定位了value(如前所说:通过Num2Iter),只需要在其后面相邻的地址添加一个value+1(或者在其前面相邻的地址添加一个vlaue-1)即可,所以这个链表始终可以保持顺序,而且指针的位移操作是o(1)的. |