what is wrong?


  • 0
    D

    There's a huge input to it but I can't figure out from the code what is wrong. any hints?

    public class RandomizedSet {
        List<Integer> ints;
        Map<Integer, Integer> mapping;
        int counter;
        java.util.Random rnd;
        /** Initialize your data structure here. */
        public RandomizedSet() {
        	mapping = new HashMap<>();
        	ints = new ArrayList<>();
            counter = -1;
        }
        
        /** Inserts a value to the set. Returns true if the set did not already contain the specified element. */
        public boolean insert(int val) {
            if(!mapping.containsKey(val)){
            	mapping.put(val, ints.size());
            	ints.add(val);
            	return true;
            }else{
            	return false;
            }
        }
        
        /** Removes a value from the set. Returns true if the set contained the specified element. */
        public boolean remove(int val) {
            if(mapping.containsKey(val)){
            	int index = mapping.remove(val);
            	
            	
            	if(index < ints.size() -1 )
            		Collections.swap(ints, index, ints.size() - 1);
            	mapping.remove(ints);
            	ints.remove(ints.size() -1);
            	return true;
            }else{
            	return false;
            }
        }
        
        /** Get a random element from the set. */
        public int getRandom() {
        	rnd = new java.util.Random();
        	int index = rnd.nextInt(ints.size());        
            return ints.get(index);
        }
        
        public int search(int x){
        	return ints.get(mapping.get(x));
        }
        
        public static void main(String[] args) {
    		RandomizedSet set = new RandomizedSet();
    		set.insert(1);
    		set.remove(2);
    		set.insert(2);
    		set.getRandom();
    		set.remove(1);
    		set.insert(2);
    		set.getRandom();
    		
    		
    		set = new RandomizedSet();
    		set.remove(0);
    		set.remove(0);
    		set.insert(0);
    		set.getRandom();
    		set.remove(0);
    		set.insert(0);
    	}
        
    }
    

  • 1

    @dixitk13164 Your remove() misses something. It should look like:

        public boolean remove(int val) {
            if(mapping.containsKey(val)) {
            	int index = mapping.get(val);
            	
            	if(index < ints.size() -1 ) {
            		mapping.put(ints.get(ints.size() - 1), index); //remember to update the index
            		Collections.swap(ints, index, ints.size() - 1);
            	}
            	mapping.remove(val); //remove val not ints
            	ints.remove(ints.size() -1);
            	return true;
            } else {
            	return false;
            }
        }
    

  • 0
    D

    that did the trick. Thanks for pointing out the update of index! I think I should not code when in half sleep or something :-P
    thanks again.


Log in to reply
 

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