【学习日记】

1.idea关闭代码补全,或关闭plugins

2.合并有序链表

java 复制代码
import  java.util.*;
import  java.lang.*;

public class test1_MergeSortedList {

    static  class ListNode{

        int val;
        ListNode next;
        ListNode(){};
        ListNode(int val){
            this.val = val;
        };
        ListNode(int val,ListNode next){
            this.val = val;
            this.next = next;

        }
    }
    public static  ListNode mergerList(ListNode[] lists){
        if(lists == null || lists.length == 0){
            return null;
        }
        PriorityQueue<ListNode> minHeap = new PriorityQueue<>((a,b) -> Integer.compare(a.val,b.val));

        for(ListNode list : lists){
            if(list !=null){
                minHeap.offer(list);
            }
        }

        ListNode dummy = new ListNode(0);
        ListNode current = dummy;

        while(!minHeap.isEmpty()){
            ListNode smallest = minHeap.poll();
            current.next =smallest;
            current = current.next;

            if(smallest.next!=null){
                minHeap.offer(smallest.next);
            }
        }
        return dummy.next;

    }



    public static void main(String[] args) {
        ListNode l1= new ListNode(1,new ListNode(4,new ListNode(5)));
        ListNode l2 = new ListNode(1, new ListNode(3, new ListNode(4)));
        ListNode l3 = new ListNode(2, new ListNode(6));

        ListNode[] lists = {l1,l2,l3};
        ListNode result = mergerList(lists);
        while(result !=null){
            System.out.println(result.val);
        result= result.next;
    }

    }


}

1.jdk8后Lambda 表达式简化了匿名内部类;

2.Comparator 是一个函数式接口(只有一个抽象方法 compare)

3.Integer.compare 是 JDK 提供的安全工具方法,它只进行逻辑判断,不进行减法运算,因此完全避免了整数溢出问题。

用 Lambda 写法是为了短,用 Integer.compare 是为了稳(防溢出)

3.k个一组翻转

花了53min

java 复制代码
class ListNode{
    int val;
    ListNode next;
    ListNode(){}
    ListNode(int val){
        this.val = val;
    }
    ListNode(int val, ListNode next){
        this.next = next;
        this.val = val;
    }
}
public class test2_reverseKNodeList {
    public static ListNode mergeKNode(ListNode node, int k){
        if(node == null || k ==1){
            return node;
        }
        ListNode dummy = new ListNode(0);
        dummy.next = node;
        ListNode curr = node;
        ListNode preGroupTail = dummy;
        ListNode currGroupHead = curr;
        while(curr!= null){
            ListNode currGroupTail = getkNode(curr, k);
            if(currGroupTail == null) {
                break;
            }
            ListNode nextGroupHead = currGroupTail.next;
            ListNode[] a = reverseNode(currGroupHead,currGroupTail);


            currGroupHead = a[0];
            currGroupTail = a[1];
            //连接
            preGroupTail.next = currGroupHead;
            currGroupTail.next = nextGroupHead;
            //更新指针
            preGroupTail = currGroupTail;
            currGroupHead = nextGroupHead;
            curr = currGroupHead;

        }
        return  dummy.next;
    }


    public static ListNode getkNode(ListNode node, int k){
        while(k!=1 && node != null){
            node = node.next;
            k--;
        }
        return  node;
    }
    public static ListNode[] reverseNode(ListNode l1, ListNode l2){
        //翻转链表的方法 a-> b-> c -> d ->Tail
        //        返回 d-> c -> b -> a
        ListNode dummy = l1;
        ListNode prev = null;
        ListNode Tail = l2.next;
        while(l1!= Tail){

            ListNode next = l1.next;
            //链接
            l1.next =prev;
            //更新指针
            prev = l1;
            l1 = next;
        }

        return new ListNode[]{l2,dummy};

    }

    public static void main(String[] args) {
        ListNode a = new ListNode(0,new ListNode(1,new ListNode(2,new ListNode(3,new ListNode(4)))));
        ListNode b = mergeKNode(a,2);
        while(b !=null){
            System.out.println(b.val);
            b = b.next;

        }

    }
}

4.LRU缓存

写了50min

java 复制代码
class LRUCache {
    class Node{

        int key;
        int value;
        //成员变量
        Node prev;
        Node next;
        //构造方法
        Node(int key,int value){
            this.key = key;
            this.value = value;
        }
    }
    private final int capacity;
    private final Map<Integer,Node> cache;
    private Node head;
    private Node tail;

    public LRUCache(int capacity) {

        this.capacity = capacity;

        head = new Node(0,0);
        tail = new Node(0,0);
        cache = new HashMap<Integer,Node>();
        head.next = tail;
        tail.prev = head;
        
    }
    
    public int get(int key) {
        Node node_get = cache.get(key);
        if(node_get== null){
            return -1;
        }else{
            moveToHead(node_get);
            return node_get.value;
            
        }
    }
    
    public void put(int key, int value) {
        Node node_get = cache.get(key);
        if(node_get == null){
            Node c = new Node(key,value);
            addToHead(c);
            cache.put(key,c);
            if(cache.size()> capacity){
                Node b = removeTailNode();
                cache.remove(b.key);
            }

        }else{
            node_get.value = value;
            moveToHead(node_get);
        }
        
    }

    private void moveToHead(Node node){
        removeNode(node);
        addToHead(node);
    }
    private void addToHead(Node node){
        node.prev = head;
        node.next = head.next;

        head.next.prev = node;
        head.next = node; 

    }
    private void removeNode(Node node){
        node.prev.next = node.next;
        node.next.prev = node.prev; 
    }
    private Node removeTailNode(){
        Node a = tail.prev;
        removeNode(a);
        return a;
    }
    
}

/**
 * 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);
 */

静态内部类

4.1静态内部类

不依赖实例 ,只依赖类本身

这完美符合"命名空间"的定义:它是一个分类标签,而不是一个对象依赖。

静态内部类 让一个类可以"住"在另一个类的名字下面,从而利用外部类的名字作为前缀(命名空间),既避免了全球范围内的类名冲突,又清晰地表达了两者之间的逻辑从属关

  1. 4.2Map.Entry 内部接口

    • 它是 Map静态嵌套接口
    • 因为它被定义为 static(无论是显式还是隐式),所以它可以独立存在,不需要 Map 的实现类对象(如 HashMap 对象)就能使用。

4.3

如果在内部类(包括静态内部类)前面不加任何访问修饰符 (即不写 publicprivateprotected),它会拥有 包级私有(Package-Private / Default) 的访问权限。

上午11:00-12:00 每日一题 1h

中间跟同学讨论了下CAS、AQS,HashMap;面试相关的;网课 1.5h

下午 3-5点 面试加写测评 2h

晚上9点- 11:30 写了翻转k节点、LRU缓存 2.5h

7h

相关推荐
人间打气筒(Ada)5 小时前
go实战案例:如何通过 Service Meh 实现熔断和限流
java·开发语言·golang·web·istio·service mesh·熔断限流
不写八个5 小时前
PHP教程004:php链接mysql数据库
数据库·mysql·php
Dylan~~~6 小时前
深度解析Cassandra:分布式数据库的王者之路
数据库·分布式
桦06 小时前
[C++复习]:STL
开发语言·c++
主宰者6 小时前
C# CommunityToolkit.Mvvm全局事件
java·前端·c#
计算机学姐6 小时前
基于SpringBoot的咖啡店管理系统【个性化推荐+数据可视化统计+配送信息】
java·vue.js·spring boot·后端·mysql·信息可视化·tomcat
荒川之神6 小时前
Oracle HR 模式递归函数练习(基于 employees 表)
数据库·oracle
前端小咸鱼一条6 小时前
16.迭代器 和 生成器
开发语言·前端·javascript
My的梦想已实现6 小时前
关于JAVA Springboot集成支付后打包JAR之后报安全错误的处理
java·spring boot·jar
小陈工6 小时前
2026年3月31日技术资讯洞察:AI智能体安全、异步编程突破与Python运行时演进
开发语言·jvm·数据库·人工智能·python·安全·oracle