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;
    }
}
相关推荐
鹿角片ljp3 小时前
力扣226.翻转二叉树-递归
数据结构·算法·leetcode
TechNomad3 小时前
排序算法:归并排序算法
算法·排序算法
WBluuue4 小时前
数据结构和算法:Morris遍历
数据结构·c++·算法
scx201310044 小时前
20251117Manacher总结
算法·manacher
iAkuya4 小时前
(leetcode)力扣100 21搜索二维矩阵2(z型搜索)
linux·leetcode·矩阵
(●—●)橘子……4 小时前
记力扣42.接雨水 练习理解
笔记·学习·算法·leetcode·职场和发展
不想秃头的程序员4 小时前
JS继承方式详解
前端·面试
旺仔小拳头..5 小时前
数据结构(三)----树/二叉树/完全二叉树/线索二叉树/哈夫曼树/树、二叉树、森林之间的转换/前 中 后序遍历
算法
摇滚侠5 小时前
面试实战 问题三十五 Spring bean 的自动装配 介绍一下熟悉的几种设计模式 Java 四种线程池是哪些
java·spring·面试