Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit9c4db3d

Browse files
refactor 146
1 parent3c20ac8 commit9c4db3d

File tree

2 files changed

+22
-21
lines changed

2 files changed

+22
-21
lines changed

‎README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,7 @@ Your ideas/fixes/algorithms are more than welcome!
470470
|149|[Max Points on a Line](https://leetcode.com/problems/max-points-on-a-line/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_149.java)| O(?)|O(?)| Hard|
471471
|148|[Sort List](https://leetcode.com/problems/sort-list/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_148.java) O(nlogn)|O(h) | Medium| Linked List, Sort
472472
|147|[Insertion Sort List](https://leetcode.com/problems/insertion-sort-list/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_147.java) O(n^2)|O(1) | Medium| Linked List
473-
|146|[LRU Cache](https://leetcode.com/problems/lru-cache/)|[Solution](../master/leetcode-algorithms/src/main/java/com/fishercoder/solutions/_146.java)| amortized O(1)| O(n) | Hard| Linked List
473+
|146|[LRU Cache](https://leetcode.com/problems/lru-cache/)|[Solution](../master/leetcode-algorithms/src/main/java/com/fishercoder/solutions/_146.java)| amortized O(1)| O(k) | Hard|DoublyLinked List, LinkedHashMap
474474
|145|[Binary Tree Postorder Traversal](https://leetcode.com/problems/binary-tree-postorder-traversal/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_145.java)| O(n)|O(h) | Hard| Binary Tree
475475
|144|[Binary Tree Preorder Traversal](https://leetcode.com/problems/binary-tree-preorder-traversal/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_144.java)| O(n)|O(h) | Medium| Binary Tree
476476
|143|[Reorder List](https://leetcode.com/problems/reorder-list/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_143.java)| O(n)|O(1)| Medium|

‎src/main/java/com/fishercoder/solutions/_146.java

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ Could you do both operations in O(1) time complexity?
3333

3434
publicclass_146 {
3535

36-
publicclassLinkedHashMapSolution {
36+
publicclassSolution1 {
3737
/**
3838
* The shortest implementation is to use LinkedHashMap:
3939
* specify a size of the linkedHashMap;
@@ -47,7 +47,7 @@ public class LinkedHashMapSolution {
4747
privateMap<Integer,Integer>cache;
4848
privatefinalintmax;
4949

50-
publicLinkedHashMapSolution(intcapacity) {
50+
publicSolution1(intcapacity) {
5151
max =capacity;
5252
cache =newLinkedHashMap<Integer,Integer>(capacity,1.0f,true) {
5353
publicbooleanremoveEldestEntry(Map.Entryeldest) {
@@ -65,13 +65,14 @@ public void set(int key, int value) {
6565
}
6666
}
6767

68-
publicclassDoublyLinkedListPlusHashMapSolution {
68+
publicclassSolution2 {
69+
/**The more verbose solution is to write a doubly linked list plus a map.*/
6970
privateclassNode {
7071
intkey;
7172
intvalue;
7273

73-
DoublyLinkedListPlusHashMapSolution.Nodeprev;
74-
DoublyLinkedListPlusHashMapSolution.Nodenext;
74+
Solution2.Nodeprev;
75+
Solution2.Nodenext;
7576

7677
Node(intk,intv) {
7778
this.key =k;
@@ -86,24 +87,24 @@ private class Node {
8687

8788
privateintcapacity;
8889
privateintcount;
89-
privateDoublyLinkedListPlusHashMapSolution.Nodehead;
90-
privateDoublyLinkedListPlusHashMapSolution.Nodetail;
91-
privateMap<Integer,DoublyLinkedListPlusHashMapSolution.Node>map;
90+
privateSolution2.Nodehead;
91+
privateSolution2.Nodetail;
92+
privateMap<Integer,Solution2.Node>map;
9293
// ATTN: the value should be Node type! This is the whole point of having a class called Node!
9394

94-
publicDoublyLinkedListPlusHashMapSolution(intcapacity) {
95+
publicSolution2(intcapacity) {
9596
this.capacity =capacity;
9697
this.count =0;// we need a count to keep track of the number of elements in the cache so
9798
// that we know when to evict the LRU one from the cache
9899
this.map =newHashMap();
99-
head =newDoublyLinkedListPlusHashMapSolution.Node();
100-
tail =newDoublyLinkedListPlusHashMapSolution.Node();
100+
head =newSolution2.Node();
101+
tail =newSolution2.Node();
101102
head.next =tail;
102103
tail.prev =head;
103104
}
104105

105106
publicintget(intkey) {
106-
DoublyLinkedListPlusHashMapSolution.Nodenode =map.get(key);
107+
Solution2.Nodenode =map.get(key);
107108
// HashMap allows value to be null, this is superior than HashTable!
108109
if (node ==null) {
109110
return -1;
@@ -122,9 +123,9 @@ public int get(int key) {
122123
}
123124

124125
publicvoidset(intkey,intvalue) {
125-
DoublyLinkedListPlusHashMapSolution.Nodenode =map.get(key);
126+
Solution2.Nodenode =map.get(key);
126127
if (node ==null) {
127-
node =newDoublyLinkedListPlusHashMapSolution.Node(key,value);
128+
node =newSolution2.Node(key,value);
128129
map.put(key,node);
129130
add(node);
130131
count++;
@@ -133,7 +134,7 @@ public void set(int key, int value) {
133134
/** ATTN: It's tail.prev, not tail, because tail is always an invalid node, it
134135
doesn't contain anything, it's always the tail.prev that is the last node in the
135136
cache*/
136-
DoublyLinkedListPlusHashMapSolution.NodetoDelete =tail.prev;
137+
Solution2.NodetoDelete =tail.prev;
137138
map.remove(toDelete.key);
138139
remove(toDelete);
139140
count--;
@@ -145,16 +146,16 @@ public void set(int key, int value) {
145146
}
146147
}
147148

148-
privatevoidremove(DoublyLinkedListPlusHashMapSolution.Nodenode) {
149-
DoublyLinkedListPlusHashMapSolution.Nodenext =node.next;
150-
DoublyLinkedListPlusHashMapSolution.Nodeprev =node.prev;
149+
privatevoidremove(Solution2.Nodenode) {
150+
Solution2.Nodenext =node.next;
151+
Solution2.Nodeprev =node.prev;
151152
prev.next =next;
152153
next.prev =prev;
153154
}
154155

155-
privatevoidadd(DoublyLinkedListPlusHashMapSolution.Nodenode) {
156+
privatevoidadd(Solution2.Nodenode) {
156157
// ATTN: we'll always add the node into the first position: head.next!!!!
157-
DoublyLinkedListPlusHashMapSolution.Nodenext =head.next;
158+
Solution2.Nodenext =head.next;
158159
head.next =node;
159160
node.next =next;
160161
node.prev =head;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp