java Solution - use single ArrayList


  • 0
    L

    Use a single ArrayList rather than HashMap. I guess contains is not strictly O(1).

    ''''

    private List<Integer> list;
    /** Initialize your data structure here. */
    public RandomizedSet() {
       // set = new HashSet<Integer>();
        list = new ArrayList<Integer>();
        
        
    }
    
    /** Inserts a value to the set. Returns true if the set did not already contain the specified element. */
    public boolean insert(int val) {
        if(!list.contains(val)){
            return list.add(val);
        }
        else{
            return false;
        }
        
    }
    
    /** Removes a value from the set. Returns true if the set contained the specified element. */
    public boolean remove(int val) {
       return list.remove(Integer.valueOf(val));
        
        
    }
    
    /** Get a random element from the set. */
    public int getRandom() {
        // if(list.size() < 1){
        //     return -1;
        // }
        int min = 0; 
        int max = list.size();
        int num =  ThreadLocalRandom.current().nextInt(min, max);
        return list.get(num);
        
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.