-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f7a9bc0
commit d4bf288
Showing
8 changed files
with
334 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,25 @@ | ||
class Solution { | ||
public ListNode addTwoNumbers(ListNode l1, ListNode l2) { | ||
ListNode sumNode = new ListNode(0); | ||
ListNode curr = sumNode; | ||
int carry = 0, sum; | ||
|
||
while (l1 != null || l2 != null || carry > 0) { | ||
sum = carry; | ||
if (l1 != null) { | ||
sum += l1.val; | ||
l1 = l1.next; | ||
} | ||
if (l2 != null) { | ||
sum += l2.val; | ||
l2 = l2.next; | ||
} | ||
|
||
carry = sum / 10; | ||
curr.next = new ListNode(sum % 10); | ||
curr = curr.next; | ||
} | ||
|
||
return sumNode.next; | ||
} | ||
} |
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,37 @@ | ||
import java.util.HashMap; | ||
|
||
class Node { | ||
int val; | ||
Node next; | ||
Node random; | ||
|
||
public Node(int val) { | ||
this.val = val; | ||
this.next = null; | ||
this.random = null; | ||
} | ||
} | ||
|
||
class Solution { | ||
public Node copyRandomList(Node head) { | ||
if (head == null) | ||
return null; | ||
|
||
HashMap<Node, Node> map = new HashMap<>(); | ||
Node curr = head; | ||
|
||
while (curr != null) { | ||
map.put(curr, new Node(curr.val)); | ||
curr = curr.next; | ||
} | ||
|
||
curr = head; | ||
while (curr != null) { | ||
map.get(curr).next = map.get(curr.next); | ||
map.get(curr).random = map.get(curr.random); | ||
curr = curr.next; | ||
} | ||
|
||
return map.get(head); | ||
} | ||
} |
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,24 @@ | ||
class Solution { | ||
public int findDuplicate(int[] nums) { | ||
int slow = nums[0]; | ||
int fast = nums[0]; | ||
|
||
// Find the intersection point of the two runners. | ||
while (true) { | ||
slow = nums[slow]; | ||
fast = nums[nums[fast]]; | ||
if (slow == fast) | ||
break; | ||
} | ||
|
||
// Find the "entrance" to the cycle. | ||
int slow2 = nums[0]; | ||
while (slow2 != slow) { | ||
slow = nums[slow]; | ||
slow2 = nums[slow2]; | ||
} | ||
|
||
return slow; | ||
|
||
} | ||
} |
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,17 @@ | ||
class Solution { | ||
public boolean hasCycle(ListNode head) { | ||
ListNode slow = head; | ||
ListNode fast = head; | ||
|
||
while (fast != null && fast.next != null) { | ||
slow = slow.next; | ||
fast = fast.next.next; | ||
|
||
if (slow == fast) | ||
return true; | ||
} | ||
|
||
return false; | ||
|
||
} | ||
} |
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,84 @@ | ||
class LRUCache { | ||
|
||
class Node { | ||
int key; | ||
int value; | ||
Node prev; | ||
Node next; | ||
|
||
Node(int key, int value) { | ||
this.key = key; | ||
this.value = value; | ||
} | ||
} | ||
|
||
Node[] cache; | ||
int capacity, size; | ||
Node head, tail; | ||
|
||
public LRUCache(int capacity) { | ||
this.capacity = capacity; | ||
this.size = 0; | ||
this.cache = new Node[10001]; | ||
|
||
head = new Node(0, 0); | ||
tail = new Node(0, 0); | ||
|
||
head.next = tail; | ||
tail.prev = head; | ||
head.prev = null; | ||
tail.next = null; | ||
} | ||
|
||
void deleteNode(Node node) { | ||
node.prev.next = node.next; | ||
node.next.prev = node.prev; | ||
} | ||
|
||
void addNode(Node node) { | ||
node.next = head.next; | ||
node.next.prev = node; | ||
node.prev = head; | ||
head.next = node; | ||
} | ||
|
||
public int get(int key) { | ||
if (cache[key] != null) { | ||
Node node = cache[key]; | ||
int val = node.value; | ||
deleteNode(node); | ||
addNode(node); | ||
return val; | ||
} else { | ||
return -1; | ||
} | ||
} | ||
|
||
public void put(int key, int value) { | ||
if (cache[key] != null) { | ||
Node node = cache[key]; | ||
node.value = value; | ||
deleteNode(node); | ||
addNode(node); | ||
} else { | ||
Node node = new Node(key, value); | ||
cache[key] = node; | ||
|
||
if (size < capacity) { | ||
size++; | ||
addNode(node); | ||
} else { | ||
cache[tail.prev.key] = null; | ||
deleteNode(tail.prev); | ||
addNode(node); | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* 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); | ||
*/ |
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,58 @@ | ||
import java.util.PriorityQueue; | ||
|
||
class Solution { | ||
public ListNode mergeKLists(ListNode[] lists) { | ||
if (lists == null || lists.length == 0) | ||
return null; | ||
PriorityQueue<ListNode> queue = new PriorityQueue<>(lists.length, (a, b) -> a.val - b.val); | ||
|
||
ListNode dummy = new ListNode(0); | ||
ListNode tail = dummy; | ||
|
||
for (ListNode node : lists) { | ||
if (node != null) | ||
queue.add(node); | ||
} | ||
|
||
while (!queue.isEmpty()) { | ||
tail.next = queue.poll(); | ||
tail = tail.next; | ||
|
||
if (tail.next != null) | ||
queue.add(tail.next); | ||
} | ||
|
||
return dummy.next; | ||
} | ||
} | ||
|
||
class Solution2 { | ||
public ListNode mergeKLists(ListNode[] lists) { | ||
if (lists == null || lists.length == 0) | ||
return null; | ||
return merge(lists, 0, lists.length - 1); | ||
} | ||
|
||
ListNode merge(ListNode[] lists, int start, int end) { | ||
if (start == end) | ||
return lists[start]; | ||
int mid = (start + end) / 2; | ||
ListNode left = merge(lists, start, mid); | ||
ListNode right = merge(lists, mid + 1, end); | ||
return mergeTwoLists(left, right); | ||
} | ||
|
||
ListNode mergeTwoLists(ListNode l1, ListNode l2) { | ||
if (l1 == null) | ||
return l2; | ||
if (l2 == null) | ||
return l1; | ||
if (l1.val < l2.val) { | ||
l1.next = mergeTwoLists(l1.next, l2); | ||
return l1; | ||
} else { | ||
l2.next = mergeTwoLists(l1, l2.next); | ||
return l2; | ||
} | ||
} | ||
} |
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,57 @@ | ||
import java.util.Arrays; | ||
import java.util.PriorityQueue; | ||
|
||
class Solution { | ||
public String[] findRelativeRanks(int[] score) { | ||
Integer[] sort = new Integer[score.length]; | ||
for (int i = 0; i < score.length; i++) { | ||
sort[i] = i; | ||
} | ||
|
||
Arrays.sort(sort, (a, b) -> (score[b] - score[a])); | ||
|
||
String[] result = new String[score.length]; | ||
|
||
for (int i = 0; i < score.length; i++) { | ||
if (i == 0) { | ||
result[sort[i]] = "Gold Medal"; | ||
} else if (i == 1) { | ||
result[sort[i]] = "Silver Medal"; | ||
} else if (i == 2) { | ||
result[sort[i]] = "Bronze Medal"; | ||
} else { | ||
result[sort[i]] = String.valueOf(i + 1); | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
} | ||
|
||
class Solution2 { | ||
// Using priority queue | ||
public String[] findRelativeRanks(int[] score) { | ||
int n = score.length; | ||
String[] result = new String[n]; | ||
PriorityQueue<Integer> pq = new PriorityQueue<>(n, (a, b) -> score[b] - score[a]); | ||
|
||
for (int i = 0; i < n; i++) { | ||
pq.add(i); | ||
} | ||
|
||
for (int i = 0; i < n; i++) { | ||
int index = pq.poll(); | ||
if (i == 0) { | ||
result[index] = "Gold Medal"; | ||
} else if (i == 1) { | ||
result[index] = "Silver Medal"; | ||
} else if (i == 2) { | ||
result[index] = "Bronze Medal"; | ||
} else { | ||
result[index] = String.valueOf(i + 1); | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
} |
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,32 @@ | ||
|
||
class Solution { | ||
public ListNode reverseKGroup(ListNode head, int k) { | ||
ListNode dummy = new ListNode(0); | ||
dummy.next = head; | ||
ListNode prev = dummy; | ||
ListNode curr = head; | ||
int count = 0; | ||
while (curr != null) { | ||
count++; | ||
if (count % k == 0) { | ||
prev = reverse(prev, curr.next); | ||
curr = prev.next; | ||
} else { | ||
curr = curr.next; | ||
} | ||
} | ||
return dummy.next; | ||
} | ||
|
||
private ListNode reverse(ListNode prev, ListNode next) { | ||
ListNode last = prev.next; | ||
ListNode curr = last.next; | ||
while (curr != next) { | ||
last.next = curr.next; | ||
curr.next = prev.next; | ||
prev.next = curr; | ||
curr = last.next; | ||
} | ||
return last; | ||
} | ||
} |