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

Commitcbfb739

Browse files
committed
Add java LRU Cache, Min stack, Happy number, Palindrone linked list
1 parentb817e34 commitcbfb739

File tree

4 files changed

+219
-0
lines changed

4 files changed

+219
-0
lines changed

‎java/146-LRU-Cache.java‎

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
classLRUCache {
2+
3+
privateMap<Integer,Node>cache;
4+
privateintcapacity;
5+
6+
privateNodeleft;
7+
privateNoderight;
8+
9+
publicLRUCache(intcapacity) {
10+
this.capacity =capacity;
11+
cache =newHashMap<>();
12+
13+
//left = LRU , right = most recent
14+
this.left =newNode(0,0);
15+
this.right =newNode(0,0);
16+
this.left.next =this.right;
17+
this.right.prev =this.left;
18+
19+
}
20+
21+
publicintget(intkey) {
22+
23+
if(cache.containsKey(key)){
24+
remove(cache.get(key));
25+
insert(cache.get(key));
26+
returncache.get(key).val;
27+
28+
}else{
29+
return -1;
30+
}
31+
32+
}
33+
34+
publicvoidput(intkey,intvalue) {
35+
36+
if(cache.containsKey(key)){
37+
remove(cache.get(key));
38+
}
39+
cache.put(key ,newNode(key ,value));
40+
insert(cache.get(key));
41+
42+
if(cache.size() >capacity){
43+
// remove from the list and delte the LRU from the hashmap
44+
Nodelru =this.left.next;
45+
remove(lru);
46+
cache.remove(lru.key);
47+
48+
}
49+
50+
51+
}
52+
53+
// remove node from list
54+
publicvoidremove(Nodenode) {
55+
56+
Nodeprev =node.prev;
57+
Nodenext =node.next;
58+
59+
prev.next =next;
60+
next.prev =prev;
61+
}
62+
63+
// insert node at right
64+
publicvoidinsert(Nodenode) {
65+
66+
Nodeprev =this.right.prev;
67+
Nodenext =this.right;
68+
69+
70+
prev.next =node;
71+
next.prev =node;
72+
73+
node.next =next;
74+
node.prev =prev;
75+
76+
77+
78+
}
79+
privateclassNode{
80+
privateintkey;
81+
privateintval;
82+
83+
Nodenext;
84+
Nodeprev;
85+
86+
publicNode(intkey ,intval){
87+
this.key =key;
88+
this.val =val;
89+
}
90+
}
91+
}

‎java/155-Min-Stack.java‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
classMinStack {
2+
3+
privateStack<Integer>stack;
4+
privateStack<Integer>minStack;
5+
6+
publicMinStack() {
7+
stack =newStack<>();
8+
minStack =newStack<>();
9+
10+
}
11+
12+
publicvoidpush(intval) {
13+
stack.push(val);
14+
15+
// The min stack may be empty, so we need to check it
16+
val =Math.min(val,minStack.isEmpty() ?val :minStack.peek());
17+
minStack.push(val);
18+
19+
}
20+
21+
publicvoidpop() {
22+
stack.pop();
23+
minStack.pop();
24+
25+
}
26+
27+
publicinttop() {
28+
returnstack.peek();
29+
}
30+
31+
publicintgetMin() {
32+
returnminStack.peek();
33+
34+
}
35+
}
36+
37+
/**
38+
* Your MinStack object will be instantiated and called as such:
39+
* MinStack obj = new MinStack();
40+
* obj.push(val);
41+
* obj.pop();
42+
* int param_3 = obj.top();
43+
* int param_4 = obj.getMin();
44+
*/

‎java/202-Happy-Number.java‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
classSolution {
2+
publicbooleanisHappy(intn) {
3+
4+
if (n ==1 ||n == -1) {
5+
returntrue;
6+
}
7+
8+
Set<Integer>visit =newHashSet<Integer>();
9+
10+
// compute square until getting duplicate value
11+
while (!visit.contains(n)) {
12+
visit.add(n);
13+
// using helper function to compute the sum of squares
14+
n =sumOfSquare(n);
15+
16+
if (n ==1)
17+
returntrue;
18+
19+
}
20+
21+
returnfalse;
22+
23+
}
24+
25+
publicintsumOfSquare(intn) {
26+
27+
intoutput =0;
28+
29+
while (n !=0) {
30+
intdigit =n %10;
31+
digit =digit *digit;
32+
output +=digit;
33+
n =n /10;
34+
35+
}
36+
37+
returnoutput;
38+
39+
}
40+
41+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
classSolution {
2+
publicbooleanisPalindrome(ListNodehead) {
3+
4+
ListNodefast =head;
5+
ListNodeslow =head;
6+
7+
while (fast !=null &&fast.next !=null) {
8+
9+
// fast pointer shifted twice
10+
fast =fast.next;
11+
fast =fast.next;
12+
13+
// slow pointer shifted only once
14+
slow =slow.next;
15+
16+
}
17+
18+
// reverse second half
19+
ListNodeprev =null;
20+
while (slow !=null) {
21+
ListNodetmp =slow.next;
22+
slow.next =prev;
23+
prev =slow;
24+
slow =tmp;
25+
}
26+
27+
ListNodeleft =head;
28+
ListNoderight =prev;
29+
30+
while (right !=null) {
31+
if (left.val !=right.val) {
32+
returnfalse;
33+
}
34+
35+
left =left.next;
36+
right =right.next;
37+
38+
}
39+
40+
returntrue;
41+
42+
}
43+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp