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;
    }
}
相关推荐
j_jiajia20 小时前
(一)人工智能算法之监督学习——KNN
人工智能·学习·算法
源代码•宸20 小时前
Golang语法进阶(协程池、反射)
开发语言·经验分享·后端·算法·golang·反射·协程池
Jasmine_llq1 天前
《CF280C Game on Tree》
数据结构·算法·邻接表·深度优先搜索(dfs)·树的遍历 + 线性累加统计
小棠师姐1 天前
支持向量机(SVM)入门:超平面与核函数的通俗解释
算法·python机器学习·支持向量机svm·超平面可视化·核函数应用
im_AMBER1 天前
Leetcode 102 反转链表
数据结构·c++·学习·算法·leetcode·链表
今儿敲了吗1 天前
01|多项式输出
c++·笔记·算法
Xの哲學1 天前
深入剖析Linux文件系统数据结构实现机制
linux·运维·网络·数据结构·算法
AlenTech1 天前
200. 岛屿数量 - 力扣(LeetCode)
算法·leetcode·职场和发展
37手游后端团队1 天前
gorm回读机制溯源
后端·面试·github
C雨后彩虹1 天前
竖直四子棋
java·数据结构·算法·华为·面试