zoukankan      html  css  js  c++  java
  • leetcode刷题15

    今天刷的题是LeetCode第146题,题目要求是实现一个LRU(最近最少使用)缓存机制,并支持获取数据get和写入数据put
      获取数据get(key):如果key存在与缓存中,则获取key对应的值,否则返回-1
      写入数据put(key,value):如果key不存在,则写入数据。当缓存容量达到上限的时候,
    应该在写入新数据之间删除最近最少使用的数据值,从而为新数据值留出空间
    这个题的解法很丰富,可以用数组也可以用链表。我这里采用的是数组的解法,具体地代码如下:
    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);
        }
    }
  • 相关阅读:
    史上最全常用正则表达式大全
    JAVA中不要用e.printStackTrace()
    JAVA中不要用e.printStackTrace()
    为什么尽量不用e.printStackTrace
    java返回集合为null还是空集合以及空集合的三种写法
    IntelliJ IDEA使用技巧——常用快捷键Mac篇
    [php] 使用laravel-modules模块化开发
    [php] 解决laravel: production.ERROR: No application encryption key has been specified
    [gitlab] 解决:remote: Ask a project Owner or Maintainer to create a default branch:
    [composer] composer 安装配置laravel 8
  • 原文地址:https://www.cnblogs.com/cquer-xjtuer-lys/p/11404012.html
Copyright © 2011-2022 走看看