navicat怎么添加check约束
271
2022-08-27
[leetcode] 705. Design HashSet
Description
Design a HashSet without using any built-in hash table libraries.
To be specific, your design should include these functions:
add(value): Insert a value into the HashSet. contains(value) : Return whether the value exists in the HashSet or not. remove(value): Remove a value in the HashSet. If the value does not exist in the HashSet, do nothing.
Example:
MyHashSet hashSet = new MyHashSet();hashSet.add(1); hashSet.add(2); hashSet.contains(1); // returns truehashSet.contains(3); // returns false (not found)hashSet.add(2); hashSet.contains(2); // returns truehashSet.remove(2); hashSet.contains(2); // returns false (already removed)
Note:
All values will be in the range of [0, 1000000].The number of operations will be in the range of [1, 10000].Please do not use the built-in HashSet library.
分析
题目的意思是:设计一个hashSet的实现,这道题还是很简单,我开始用list实现了一下,虽然能ac,但是效率比较低下,尤其是判断是否存在的时候;后面发现直接申请10000大小的list存bool值就行了,这样判断存在的时候的复杂度就是O(1),解法很巧妙
代码
class MyHashSet: def __init__(self): """ Initialize your data structure here. """ self.arr=[False] * 100000 def add(self, key: int) -> None: self.arr[key]=True def remove(self, key: int) -> None: self.arr[key]=False def contains(self, key: int) -> bool: """ Returns true if this set contains the specified element """ return self.arr[key] # Your MyHashSet object will be instantiated and called as such:# obj = MyHashSet()# obj.add(key)# obj.remove(key)# param_3 = obj.contains(key)
参考文献
705. Design HashSet
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~