今際の国の呵呵君
Monday, October 23, 2017
Insert Delete GetRandom O(1) - Duplicates allowed
这道题和
Insert Delete GetRandom O(1)
是类似的,区别在于允许重复的数。我们的思路是类似的,只不过hashmap存的不是数字和对应的index了,而是数数字和hashset,hashset里存了所有的index。删除的时候我们只需要从hashset里随便取一个出来和末尾的数交换即可,代码如下:
No comments:
Post a Comment
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment