Why answer different between local machine and OJ?


  • 0
    T

    struct Node
    {
    int nKey;
    int nValue;
    int pre;
    int next;
    Node()
    {
    pre = -1;
    next = -1;
    }
    };

    Node l[10000];
    int nTop = 0;
    int nHead = -1;
    int nTail = -1;

    class LRUCache{
    public:
    LRUCache(int capacity) {
    maxsize = capacity;
    use.clear();
    }

    int get(int key) {
    	return GetValue(key);
    }
    
    void set(int key, int value) {
    	if (use.find(key) == use.end() && use.size() >= maxsize )
    	{
    		RemoveLast();
    	}
    	RefresKey(key,value);
    }
    int GetValue(int key)
    {
    	if (use.find(key) != use.end())
    	{
    		int nIndex = use[key];
    		int nValue = l[nIndex].nValue;
    		RefresKey(nIndex,nValue);
    		return nValue;
    	}
    	else
    	{
    		return -1;
    	}
    }
    
    void RemoveLast()
    {
    	int nIndex = nHead;
    	int key = l[nHead].nKey;
    	if (use.find(key)!=use.end())
    	{
    		use.erase(use.find(key));
    	}
    	nHead = l[nHead].next;
    	l[nHead].pre = -1; 
    }
    
    void RefresKey(int key,int value)
    {
    	if(use.find(key)!=use.end())
    	{
    		int nIndex = use[key];
    		int npre = l[nIndex].pre;
    		int nnext = l[nIndex].next;
    
    		l[nIndex].nValue = value;
    		l[nIndex].nKey = key;
    
    		if (nIndex == nTail) 
    		{
    			return;
    		}
    		if (nHead == nIndex)
    		{
    			nHead = nnext;
    		}  
    
    		if (npre != -1)
    		{
    			l[npre].next = nnext;
    		}
    		if (nnext != -1)
    		{
    			l[nnext].pre = npre;
    		}
    
    		l[nTail].next = nIndex;
    		l[nIndex].pre = nTail;
    		nTail = nIndex;
    	}
    	else
    	{
    		Node &newnode = l[nTop++];
    		newnode.pre = -1;
    		newnode.next = -1;
    		newnode.nValue = value;
    		newnode.nKey = key;
    		if (nHead == -1) 
    		{
    			nHead = nTop - 1;
    		}
    		if (nTail == -1 )
    		{
    			nTail = nTop - 1;
    		}
    		else
    		{
    			l[nTail].next = nTop-1;
    			newnode.pre = nTail;
    			nTail = nTop - 1;
    		}
    		use[key] = nTop - 1;
    	}
    	return ;
    }
    
    map<int,int> use;
    int maxsize;
    

    };


Log in to reply
 

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