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;
    }
}
相关推荐
D e re k ZH17 分钟前
P5025 [SNOI2017] 炸弹
c++·算法
A boy CDEF girl25 分钟前
算法题专栏:优选算法
数据结构·算法·leetcode
没落之王32 分钟前
三易五行看宋朝的三界确立
大数据·人工智能·算法
坤坤子吖1 小时前
算法学习——单调队列
开发语言·c++·笔记·学习·算法
Js.Tx3 小时前
【具体数学 Concrete Mathematics】2.1.5 一般性的方法
算法·具体数学
秋天的怀念_David4 小时前
算法一千零一夜 · 作品相关:版权与致谢声明
程序人生·算法·学习方法
香蕉大香蕉(06敬畏市场)4 小时前
MySQL 单点详解(03)
java·数据库·mysql·面试·八股·大厂实习
揽星河..4 小时前
【C语言】数组
c语言·开发语言·算法
careathers4 小时前
【数据结构】基于比较的排序算法 - 选择排序
数据结构·算法·排序算法
听风若依5 小时前
C++模板初阶
c++·算法