zoukankan      html  css  js  c++  java
  • leetcode-mid-design-380. Insert Delete GetRandom O(1)

    mycode

    import random
    class RandomizedSet(object):
    
        def __init__(self):
            """
            Initialize your data structure here.
            """
            self.s = []
    
    
        def insert(self, val):
            """
            Inserts a value to the set. Returns true if the set did not already contain the specified element.
            :type val: int
            :rtype: bool
            """
            if val in self.s:
                
                return False
            else:
                self.s.append(val)
                return True
            
    
        def remove(self, val):
            """
            Removes a value from the set. Returns true if the set contained the specified element.
            :type val: int
            :rtype: bool
            """
            if val in self.s:
                self.s.remove(val)
                return True
            else:
                return False
            
    
        def getRandom(self):
            """
            Get a random element from the set.
            :rtype: int
            """
            return self.s[random.randint(0,len(self.s)-1)]
            
    
    
    # Your RandomizedSet object will be instantiated and called as such:
    # obj = RandomizedSet()
    # param_1 = obj.insert(val)
    # param_2 = obj.remove(val)
    # param_3 = obj.getRandom()

    参考:

    emmmm。。。我直接调用的???我的天。。。

    思路:用dic来查找,找到后去insert和删除

    import random
    
    class RandomizedSet(object):
    
        def __init__(self):
            self.data = []
            self.pos = {}
    
        def insert(self, val):
            if val in self.pos:
                return False
            
            self.data.append(val)
            self.pos[val] = len(self.data) - 1
            
            return True
    
        def remove(self, val):
            """
            Removes a value from the set. Returns true if the set contained the specified element.
            :type val: int
            :rtype: bool
            """
            if val not in self.pos:
                return False
            
            last = self.data[-1]
            elt = self.pos[val]
            self.data[elt] = last
            self.pos[last] = elt
            self.data.pop()
            del self.pos[val]
            
            return True
    
        def getRandom(self):
            return random.choice(self.data)
            
    
    
    # Your RandomizedSet object will be instantiated and called as such:
    # obj = RandomizedSet()
    # param_1 = obj.insert(val)
    # param_2 = obj.remove(val)
    # param_3 = obj.getRandom()
  • 相关阅读:
    集合类--容器
    《学习之道》第十一章理解
    文件操作引出流(一)Stream和File.Create(path)
    整理文件操作(五)
    整理文件操作(四)Image.FromFile(path)
    整理文件操作(三)File.Exists(path)和new FileInfo(path).Exists
    整理文件操作(二)File和FileInfo
    整理文件操作(一)逻辑流程
    《学习之道》第十一章先理解再去记忆
    《学习之道》第十一章再次强调激发感官
  • 原文地址:https://www.cnblogs.com/rosyYY/p/10980323.html
Copyright © 2011-2022 走看看