forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add java LRU Cache, Min stack, Happy number, Palindrone linked list
- Loading branch information
Showing
4 changed files
with
219 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
class LRUCache { | ||
|
||
private Map<Integer,Node> cache; | ||
private int capacity; | ||
|
||
private Node left; | ||
private Node right; | ||
|
||
public LRUCache(int capacity) { | ||
this.capacity = capacity; | ||
cache = new HashMap<>(); | ||
|
||
//left = LRU , right = most recent | ||
this.left = new Node(0,0); | ||
this.right = new Node(0,0); | ||
this.left.next = this.right; | ||
this.right.prev = this.left; | ||
|
||
} | ||
|
||
public int get(int key) { | ||
|
||
if(cache.containsKey(key)){ | ||
remove(cache.get(key)); | ||
insert(cache.get(key)); | ||
return cache.get(key).val; | ||
|
||
} else{ | ||
return -1; | ||
} | ||
|
||
} | ||
|
||
public void put(int key, int value) { | ||
|
||
if(cache.containsKey(key)){ | ||
remove(cache.get(key)); | ||
} | ||
cache.put(key , new Node(key , value)); | ||
insert(cache.get(key)); | ||
|
||
if(cache.size() > capacity){ | ||
// remove from the list and delte the LRU from the hashmap | ||
Node lru = this.left.next; | ||
remove(lru); | ||
cache.remove(lru.key); | ||
|
||
} | ||
|
||
|
||
} | ||
|
||
// remove node from list | ||
public void remove(Node node) { | ||
|
||
Node prev = node.prev; | ||
Node next = node.next; | ||
|
||
prev.next = next; | ||
next.prev = prev; | ||
} | ||
|
||
// insert node at right | ||
public void insert(Node node) { | ||
|
||
Node prev = this.right.prev; | ||
Node next = this.right; | ||
|
||
|
||
prev.next = node; | ||
next.prev = node; | ||
|
||
node.next =next; | ||
node.prev =prev; | ||
|
||
|
||
|
||
} | ||
private class Node{ | ||
private int key; | ||
private int val; | ||
|
||
Node next; | ||
Node prev; | ||
|
||
public Node(int key , int val){ | ||
this.key = key; | ||
this.val = val; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
class MinStack { | ||
|
||
private Stack<Integer> stack; | ||
private Stack<Integer> minStack; | ||
|
||
public MinStack() { | ||
stack = new Stack<>(); | ||
minStack = new Stack<>(); | ||
|
||
} | ||
|
||
public void push(int val) { | ||
stack.push(val); | ||
|
||
// The min stack may be empty, so we need to check it | ||
val = Math.min(val, minStack.isEmpty() ? val : minStack.peek()); | ||
minStack.push(val); | ||
|
||
} | ||
|
||
public void pop() { | ||
stack.pop(); | ||
minStack.pop(); | ||
|
||
} | ||
|
||
public int top() { | ||
return stack.peek(); | ||
} | ||
|
||
public int getMin() { | ||
return minStack.peek(); | ||
|
||
} | ||
} | ||
|
||
/** | ||
* Your MinStack object will be instantiated and called as such: | ||
* MinStack obj = new MinStack(); | ||
* obj.push(val); | ||
* obj.pop(); | ||
* int param_3 = obj.top(); | ||
* int param_4 = obj.getMin(); | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
class Solution { | ||
public boolean isHappy(int n) { | ||
|
||
if (n == 1 || n == -1) { | ||
return true; | ||
} | ||
|
||
Set<Integer> visit = new HashSet<Integer>(); | ||
|
||
// compute square until getting duplicate value | ||
while (!visit.contains(n)) { | ||
visit.add(n); | ||
// using helper function to compute the sum of squares | ||
n = sumOfSquare(n); | ||
|
||
if (n == 1) | ||
return true; | ||
|
||
} | ||
|
||
return false; | ||
|
||
} | ||
|
||
public int sumOfSquare(int n) { | ||
|
||
int output = 0; | ||
|
||
while (n != 0) { | ||
int digit = n % 10; | ||
digit = digit * digit; | ||
output += digit; | ||
n = n / 10; | ||
|
||
} | ||
|
||
return output; | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
class Solution { | ||
public boolean isPalindrome(ListNode head) { | ||
|
||
ListNode fast = head; | ||
ListNode slow = head; | ||
|
||
while (fast != null && fast.next != null) { | ||
|
||
// fast pointer shifted twice | ||
fast = fast.next; | ||
fast = fast.next; | ||
|
||
// slow pointer shifted only once | ||
slow = slow.next; | ||
|
||
} | ||
|
||
// reverse second half | ||
ListNode prev = null; | ||
while (slow != null) { | ||
ListNode tmp = slow.next; | ||
slow.next = prev; | ||
prev = slow; | ||
slow = tmp; | ||
} | ||
|
||
ListNode left = head; | ||
ListNode right = prev; | ||
|
||
while (right != null) { | ||
if (left.val != right.val) { | ||
return false; | ||
} | ||
|
||
left = left.next; | ||
right = right.next; | ||
|
||
} | ||
|
||
return true; | ||
|
||
} | ||
} |