leetcode brush 15 title

Today's topic is LeetCode brush 146 title, the title is required to implement a LRU (least recently used) cache mechanism, and support access to data and write data put get 
  get data get (key): If the key exists in the cache, then get corresponding key value, otherwise -1
  write data put (key, value): If a key does not exist, then the data is written. When the cache size reaches the upper limit of the time,
should be removed in between writing new data value the least recently used data, thus leaving room for new data value
of this solution is very rich topic, you can use the array can also use the list. I used here is an array of solution, specifically code is as follows:
import java.time.temporal.ValueRange;
import java.util.HashMap;
import java.util.Map;


public class LRUCache_146_middle {
    private int capacity;
    private int count;
    private int[] KEY;
    private int [] VALUE;
    public LRUCache_146_middle(int capacity){
        this.capacity=capacity;
        this.count=0;//记录当前存储的数据个数
        this.KEY=new int[capacity];
        this.VALUE=new int[capacity];
    }
    public int get(int key){
        if (count==0){
            return -1;
        }
        int result=-1;
        for (int i = 0; i <count ; i++) {
            if (KEY[i]==key){
                result=VALUE[i];
                VALUE=move(VALUE,count,i);
                KEY=move(KEY,count,i);
                break;
            }
        }
        System.out.println(result);
        return result;
    }
    public void put(int key,int value){
        if (containskey(KEY,count,key)[0]==1){
            int index=containskey(KEY,count,key)[1];
            VALUE[index]=value;
            VALUE=move(VALUE,count,index);
            KEY=move(KEY,count,index);
        }else if (count==capacity){
            //保存的数据满了
            for (int i = 0; i <count-1 ; i++) {
                KEY[i]=KEY[i+1];
                VALUE[i]=VALUE[i+1];
            }
            KEY[count-1]=key;
            VALUE[count-1]=value;
        } else {
            VALUE[count]=value;
            KEY[count]=key;
            count++;
        }
    }
    public static int[] containskey(int[] KEY,int count,int key){
        int[] result=new int[2];
        for (int i = 0; i <count ; i++) {
            if (KEY[i]==key){
                result[0]=1;
                result[1]=i;
                break;
            }
        }
        return result;
    }
    public static int[] move(int [] nums,int count,int i){
        int temp=nums[i];
        for (int j = i; j <count-1 ; j++) {
            nums[j]=nums[j+1];
        }
        nums[count-1]=temp;
        return nums;
    }

    public static void main(String[] args) {
        LRUCache_146_middle lru=new LRUCache_146_middle(2);
        lru.put(2,1);
        lru.put(2,2);
        lru.get(2);
        lru.put(1,1);
        lru.put(4,1);
        lru.get(2);
    }
}

 

Guess you like

Origin www.cnblogs.com/cquer-xjtuer-lys/p/11404012.html