day42(12.23)——leetcode面试经典150

86. 分隔链表

86. 分隔链表

咱也是成功发现leetcode的bug了哈哈哈

题目:

题解:

java 复制代码
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode partition(ListNode head, int x) {
        //小于x的链表
        ListNode xy  = new ListNode();
        //大于等于x的链表
        ListNode dy = new ListNode();
        //当前辅助小于x的链表
        ListNode curXy = xy;
        //当前辅助大于等于的链表
        ListNode curDy = dy;
        //当前辅助遍历head的结点
        ListNode cur = head;
        while(cur != null) {
            if(cur.val < x) {
                curXy.next = cur;
                curXy = curXy.next;
            }
            else {
                curDy.next = cur;
                curDy = curDy.next;
            }
            cur = cur.next;
        }
        curDy.next = null;
        curXy.next = dy.next;
        return xy.next;
    }
}

146. LRU 缓存

146. LRU缓存

真没想到java官方还有这样的方法,牛皮

题目:

题解:

java 复制代码
import java.util.LinkedHashMap;
import java.util.Map;

class LRUCache extends LinkedHashMap<Integer, Integer> {
    private final int capacity;

    public LRUCache(int capacity) {
        // true 表示按访问顺序排序(LRU 关键!)
        super(capacity, 0.75f, true);
        this.capacity = capacity;
    }

    public int get(int key) {
        return super.getOrDefault(key, -1);
    }

    public void put(int key, int value) {
        super.put(key, value);
    }

    // 当 size() > capacity 时,自动移除最老的 entry
    @Override
    protected boolean removeEldestEntry(Map.Entry<Integer, Integer> eldest) {
        return size() > capacity;
    }
}
相关推荐
vibecoding日记5 小时前
双非如何快速入职字节等大厂大模型?真实案例分析:推理优化和投机解码
算法·求职·大模型工程师
yszaygr21387 小时前
Verilog参数化游程编码RLE模块
算法
望易7 小时前
刚设计的大模型架构-双域耦合认知框架
算法·架构
用户852495071847 小时前
解密 JavaScript 中的 this:谁才是真正的调用者?
javascript·面试
Heo7 小时前
Vite进阶用法详解
前端·javascript·面试
洛卡卡了7 小时前
Claude Code rules 要怎么用,团队协作时如何统一代码规范呢?
面试·agent·claude
不好听61311 小时前
JavaScript 的 this 到底指向谁?
javascript·面试
烬羽11 小时前
面试官:聊聊 LocalStorage 和 this 指向?看这篇就够了
面试·程序员