Why time limits exceed ??


  • 0

    //命中的清零,其他各行加1,替换的时候选择最大的;

    struct value_count
    {
        int value;
        int count;
    };
    typedef struct value_count Value_Count;
    public:
       LRUCache(int capacity) {
               LRU_Capacity = capacity;
               current_LRU_Numbers = 0;
        }
    
    int get(int key) {
        map<int,Value_Count>::iterator iter_Get = cache.find(key);
        if(iter_Get!=cache.end())
        {
            inc();
            iter_Get->second.count = 0;
            return iter_Get->second.value;
        }
        return -1;
    }
    
    void set(int key, int value) {
        map<int,Value_Count>::iterator iter_Set = cache.find(key);
        if(iter_Set!=cache.end())
            return;
        if(current_LRU_Numbers==LRU_Capacity)
        {//去掉一个LRU cache
            map<int,Value_Count>::iterator iter1 = cache.begin();
            map<int,Value_Count>::iterator iter2 = iter1;
            iter2++;
            for(;iter2!=cache.end();iter2++)
            {
                if((iter2->second.count)>(iter1->second.count))
                    iter1 = iter2;
            }
            cache.erase(iter1);
            current_LRU_Numbers--;
        }
        Value_Count p;
        p.value = value;
        p.count = 0;
        inc();
        cache.insert(make_pair(key,p));
        current_LRU_Numbers++;
    }
    
    void inc()
    {
        map<int,Value_Count>::iterator iter_temp;
        for(iter_temp = cache.begin();iter_temp != cache.end();iter_temp++)
        {
            iter_temp->second.count++;
        }
    }
    private:
           int LRU_Capacity;
           int current_LRU_Numbers;
          map<int,Value_Count> cache;

  • 0
    K

    yup i got same error

    I m using linkedlist implementation in java. still getting above error ? how to fix it ?

    import java.util.LinkedList;
    import java.util.List;

    public class LRUCache {

    LinkedList<Integer> keys;
    

    LinkedList<Integer> values;
    int capacity;

        public LRUCache(int capacity) {
            keys = new LinkedList<Integer>();
    

    values = new LinkedList<Integer>();
    this.capacity = capacity;
    }

        public int get(int key) {
              
    if(keys.contains(key))
    {
      int index = keys.indexOf(key);
      
      return(values.get(index));
      
    }
    else
      return -1;
      
        }
        
        public void set(int key, int value) {
             if(keys.contains(key))
    {
      int index = keys.indexOf(key);
      values.remove(index);keys.remove(index);
      
      keys.addLast(key);values.addLast(value);
    }
    else
    {
      if(keys.size()==capacity)
      {
        keys.removeFirst();values.removeFirst();
        keys.addLast(key);values.addLast(value);
      }else
      {
        keys.addLast(key);values.addLast(value);
      }
    }
        }
    

    }


Log in to reply
 

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