Need help in debugging my solution!


  • 0
    M

    Can someone please check my solution and help me find out whats wrong in my code? I am getting correct output on a particular test-case but different when i am running it here. I am unable to figure out the problem.

    class LRUCache {

        private static int capacity =0;
        private static HashMap<Integer, Integer> record = new HashMap<Integer, Integer>();
        private static  ArrayList<Integer>recordList = new ArrayList<Integer>(); 
         private static HashMap<Integer, Integer> recordIndex = new HashMap<Integer, Integer>();
        public LRUCache(int capacity) {
            this.capacity = capacity;
        }
        public static void setCurrent(int key){
      
             int index = (recordList.indexOf(key)>-1)? recordList.indexOf(key) : recordList.size(); 
                recordList.remove(index);
                recordList.add(recordList.size(), key);
        }
        public static int get(int key) {
    
            if(record.containsKey(key)){
            	 int index =  recordList.indexOf(key); 
    	         recordList.remove(index);
    	         recordList.add(recordList.size(), key);
    
                return record.get(key);
            }
            
            return -1;
        }
        
        public static void put(int key, int value) {
    
            if(record.containsKey(key)){
            	recordList.remove(recordList.indexOf(key));
            	recordList.add(recordList.size(),key);
            	
            }
               
            else
            {
                if(recordList.size() == capacity)
                {  
                	int newkey = recordList.get(0);
                    record.remove(newkey);
                    recordList.remove(0);
                }
                recordList.add(recordList.size(),key);
            	 record.put(key, value);
    
            }
        }
    

    }


  • 0
    M

    @mb821 can you paste your code in here?


  • 0
    M

    @mastrangelo I have copied the code. Thanks


Log in to reply
 

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