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静态内部类 :
它不依赖实例 ,只依赖类本身 。
这完美符合"命名空间"的定义:它是一个分类标签,而不是一个对象依赖。
静态内部类 让一个类可以"住"在另一个类的名字下面,从而利用外部类的名字作为前缀(命名空间),既避免了全球范围内的类名冲突,又清晰地表达了两者之间的逻辑从属关
-
4.2Map.Entry内部接口:- 它是
Map的静态嵌套接口。 - 因为它被定义为
static(无论是显式还是隐式),所以它可以独立存在,不需要Map的实现类对象(如HashMap对象)就能使用。
- 它是
4.3
如果在内部类(包括静态内部类)前面不加任何访问修饰符 (即不写 public、private 或 protected),它会拥有 包级私有(Package-Private / Default) 的访问权限。
上午11:00-12:00 每日一题 1h
中间跟同学讨论了下CAS、AQS,HashMap;面试相关的;网课 1.5h
下午 3-5点 面试加写测评 2h
晚上9点- 11:30 写了翻转k节点、LRU缓存 2.5h
7h