力扣打卡(15):上学的时间过的真快,找不到时间刷题.。。。

10.12lc

开学的这两天 是真的瞎忙。。。 刷题明显没时间了 可恶吖~

29. 两数相除 - 力扣(LeetCode) (leetcode-cn.com)

:思路 除法的本质: 就是减法 : 这题要使用位运算~

class Solution {
    public int divide(int dividend, int divisor) {
        //超时
    if(dividend  == 1<< 31 && divisor == -1  )  return (1<<31)-1;
        
    int sing = (dividend > 0) ^ (divisor >0) ? -1: 1;
    long a=Math.abs((long)dividend);
    long b =Math.abs((long)divisor);
    int res =0;
    int i=0;
    while( a-b >=0){
        long temp =b;
        int m=1;
        //要使用的倍数的 相减 为了减少时间复杂度
        while(temp << 1 <= a){
            temp <<=1;
            m<<= 1;
        }   
        a=a-temp;
        res+=m;
       
          
    }    
      return res * sing;
        }
}

146. LRU 缓存机制 - 力扣(LeetCode) (leetcode-cn.com)

大厂常客面试:

:我刚开始思路就是 使用 LinkedHashMap:

class LRUCache {
        int capacity;
        LinkedHashMap<Integer,Integer> cache;
    public LRUCache(int capacity) {
       this.capacity = capacity;
       cache = new LinkedHashMap<Integer,Integer>(capacity,0.75f,true){
//开启 读取顺序的更新
        @Override
        protected boolean removeEldestEntry(Map.Entry eldest){
            return cache.size() > capacity;
        }
       };
    }
    
    public int get(int key) {
            return cache.getOrDefault(key,-1);
    }
    
    public void put(int key, int value) {
            cache.put(key,value);
    }
}

/**
 * Your LRUCache object will be instantiated and called as such:
 * LRUCache obj = new LRUCache(capacity);
 * int param_1 = obj.get(key);
 * obj.put(key,value);
 */

题解:给出的 自己去 “假手动实现”LRU: 问社么这么说呢? 说白了 还是

LinkedHashMap。。。。。 只是自己去手写了的链表。。。 不过这个解题的思想很值得思考 答案自己去官网看吧

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值