————2020.1.15————

Posted zzl1209

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了————2020.1.15————相关的知识,希望对你有一定的参考价值。

 

# 学习记录 || Coding_Record #


# LRU

比较常见的页面置换算法。*leetcode 146

# 个人简易实现(Java)

 

 1 import java.util.HashMap;
 2 
 3 
 4 class LRUCache {
 5     
 6     public static class Node{
 7         public Object val;
 8         public Object key;
 9         public Node next;
10         public Node last;
11         
12         public Node(Object key,Object val) {
13             this.val = val;
14             this.key = key;
15         }
16     }
17     
18     public static class DoubleList{
19         private Node head;
20         private Node tail;
21         public int size;
22         
23         public DoubleList() {
24             this.size = 0;
25             this.head = new Node(0,0);
26             this.tail = new Node(0,0);
27             head.next = tail;
28             head.last = null;
29             tail.last = head;
30             tail.next = null;
31         }
32         
33         public void addToHead(Node node) {
34             Node tmp = head.next;
35             head.next = node;
36             tmp.last = node;
37             node.last = head;
38             node.next = tmp;
39             size++;
40         }
41         
42         public Node removeLast() {
43             Node tmp = tail.last;
44             tmp.last.next = tail;
45             tail.last = tmp.last;
46             size--;
47             return tmp;
48         }
49         
50         public void moveToHead(Node node) {
51             node.last.next = node.next;
52             node.next.last = node.last;
53             size--;
54             addToHead(node);
55         }
56         
57     }
58     private int capacity;
59     
60     private DoubleList list = new DoubleList();
61     
62     private HashMap<Object,Node> map = new HashMap<>();
63     
64     public LRUCache(int capacity) {
65         this.capacity = capacity;
66     }
67     
68     public void put(Object key,Object value) {
69         Node node = map.get(key);
70         if(node!=null) {
71             node.val = value;
72             list.moveToHead(node);
73         }
74         else {
75             Node newNode = new Node(key,value);
76             if(list.size==capacity) {
77                 Node delnode = list.removeLast();
78                 map.remove(delnode.key);
79             }
80             list.addToHead(newNode);
81             map.put(key,newNode);
82         }
83     }
84     
85     public Object get(Object key) {
86         Node node = map.get(key);
87         if(node==null) return -1;
88         else {
89             list.moveToHead(node);
90             return node.val;
91         }
92     }
93 }

 

# SkipList

基于链表的随机化数据结构。 *leetcode 1206

 # 参考题解的简易实现(Java)

  1 import java.util.ArrayList;
  2 import java.util.List;
  3 
  4 
  5 public class SkipList {
  6     
  7     public class SkipListNode{
  8         
  9         public Integer value;
 10         public SkipListNode right;
 11         public SkipListNode left;
 12         public SkipListNode down;
 13         
 14         public SkipListNode(Integer value) {
 15             this.value = value;
 16             this.down = null;
 17             this.right = null;
 18             this.left = null;
 19         }
 20     }
 21     
 22     private List<SkipListNode> heads ;
 23     
 24     public SkipList() {
 25         SkipListNode head_of_0 = new SkipListNode( Integer.MIN_VALUE );
 26         heads = new ArrayList<>();
 27         heads.add(head_of_0);
 28     }
 29     
 30     public boolean search(Integer target) {
 31         int level = heads.size()-1;
 32         SkipListNode curNode = heads.get(level);
 33         while(level != -1) {
 34             boolean jump = false;
 35             while(curNode.right != null) {
 36                 curNode = curNode.right;
 37                 if(curNode.value == target) {
 38                     return true;
 39                 }
 40                 else if(curNode.value > target) {
 41                     if(level == 0) return false;
 42                     else {
 43                         level--;
 44                         curNode = curNode.left;
 45                         jump = true;
 46                         break;
 47                     }
 48                 }
 49             }
 50             if(jump==true) continue;
 51             level--;
 52             curNode = curNode.down;
 53         }
 54         return false;
 55     }
 56     
 57     private void del(SkipListNode node){
 58         node.left.right = node.right;
 59         if(node.right != null ) {
 60             node.right.left = node.left;
 61         }
 62     }
 63     
 64     private boolean throwCoin(){
 65         return Math.random()<0.5f;
 66     }
 67     
 68     public SkipListNode[] searchNodes(Integer target) {
 69         int level = heads.size()-1;
 70         SkipListNode[] jumps = new SkipListNode[heads.size()];
 71         SkipListNode curNode = heads.get(level);
 72         while(level!=-1){
 73             boolean jump = false;
 74             while(curNode.right!= null){
 75                 curNode = curNode.right;
 76                 if(curNode.value == target) {
 77                     while(level!=-1){
 78                         jumps[level] = curNode;
 79                         curNode = curNode.down;
 80                         level --;
 81                     }
 82                     return jumps;
 83                 }
 84                 else if(curNode.value>target){
 85                     jumps[level] = curNode.left;
 86                     level --;
 87                     curNode = curNode.left.down;
 88                     jump = true;
 89                     break;
 90                 }
 91             }
 92             if(jump)
 93                 continue;
 94             jumps[level] = curNode;
 95             level --;
 96             curNode = curNode.down;
 97         }
 98         return jumps;
 99     }
100     
101     public void erase(Integer target) {
102         SkipListNode[] jmpt = searchNodes(target);
103         for (SkipListNode n:jmpt)
104             if(n.value == target){
105                 del(n);
106             }
107     }
108     
109     public void add(Integer num) {
110         SkipListNode curNode = new SkipListNode(num);
111         SkipListNode[] curNodes = searchNodes(num);
112         SkipListNode leftNode = curNodes[0];
113         curNode.right = leftNode.right;
114         curNode.left = leftNode;
115         if(leftNode.right!=null)
116             leftNode.right.left=curNode;
117         leftNode.right = curNode;
118         //Add Index
119         int curIndex = 1;
120         while(throwCoin()){
121             SkipListNode tmp;
122             if(curNodes.length <= curIndex){
123                 tmp= new SkipListNode(curIndex-1);
124                 heads.add(tmp);
125                 tmp.down = heads.get(curIndex-1);
126             }
127             else
128                 tmp = curNodes[curIndex];
129             SkipListNode curtmp = new SkipListNode(num);
130             curtmp.down = curNode;
131             curtmp.right = tmp.right;
132             curtmp.left = tmp;
133             if(tmp.right!=null)
134                 tmp.right.left = curtmp;
135             tmp.right = curtmp;
136             curNode = curtmp;
137             curIndex++;
138         }
139     }
140     public static void main(String[] args) {
141         SkipList sl = new SkipList();
142         sl.add(1);
143         sl.add(2);
144         System.out.println(sl.search(3));
145     }
146 }

 

 


# Edit : 2020.1.15 

以上是关于————2020.1.15————的主要内容,如果未能解决你的问题,请参考以下文章

谷歌浏览器调试jsp 引入代码片段,如何调试代码片段中的js

————2020.1.15————

片段和活动之间的核心区别是啥?哪些代码可以写成片段?

VSCode自定义代码片段——.vue文件的模板

VSCode自定义代码片段6——CSS选择器

VSCode自定义代码片段——声明函数