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);
        }
    }
  • 相关阅读:
    Python 爬虫 解决escape问题
    python 爬虫 重复下载 二次请求
    iOS开发-消息通知机制(NSNotification和NSNotificationCenter)
    iOS开发-UITableView自定义Cell
    iOS开发-自定义UIAlterView(iOS 7)
    iOS开发-CocoaPods实战
    iOS开发-UICollectionView实现瀑布流
    iOS开发-UITabBarController详解
    iOS 开发-Certificate、App ID和Provisioning Profile之间的关系
    iOS开发-View中frame和bounds区别
  • 原文地址:https://www.cnblogs.com/cquer-xjtuer-lys/p/11404012.html
Copyright © 2011-2022 走看看