Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions Java/Problem1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import java.util.Arrays;

class Solution {
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
int[] mergeArray = new int[nums1.length + nums2.length];
double median = 0.0;

System.arraycopy(nums1, 0, mergeArray, 0, nums1.length);
System.arraycopy(nums2, 0, mergeArray, nums1.length, nums2.length);

Arrays.sort(mergeArray);
if (mergeArray.length % 2 == 0) {
int mid = mergeArray.length / 2;
median = (mergeArray[mid - 1] + mergeArray[mid]) / 2.0;
} else {
median = mergeArray[mergeArray.length / 2];
}
return median;

}
}
78 changes: 78 additions & 0 deletions Java/Problem2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import java.util.HashMap;
import java.util.Map;

class LRUCache {

private static class Node {
int key;
int value;
Node prev;
Node next;

}

private Map<Integer, Node> cache;
private Node start;
private Node end;
private int size;

public LRUCache(int capacity) {
this.size = capacity;
cache = new HashMap<>();
start = new Node();
end = new Node();
this.start.next = this.end;
this.end.prev = this.start;
}

public int get(int key) {
if (!cache.containsKey(key)) {
return -1;
}

Node node = cache.get(key);
deleteNode(node);
addFront(node);
return node.value;

}

public void put(int key, int value) {
if (cache.containsKey(key)) {
Node node = cache.get(key);
node.value = value;
deleteNode(node);
addFront(node);
} else {
Node newnode = new Node();
newnode.key = key;
newnode.value = value;
cache.put(key, newnode);
addFront(newnode);
if (cache.size() > size) {
Node lru = end.prev;
deleteNode(lru);
cache.remove(lru.key);
}
}
}

private void deleteNode(Node node) {
node.prev.next = node.next;
node.next.prev = node.prev;
}

private void addFront(Node node) {
node.prev = this.start;
node.next = this.start.next;
this.start.next.prev = node;
this.start.next = 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);
*/
26 changes: 26 additions & 0 deletions Java/Problem3.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import java.util.HashMap;
import java.util.Map;

class Solution {
public int lengthOfLongestSubstring(String s) {
int actualmax = 0;
int start = 0;
Map<Character, Integer> characterMaps = new HashMap<>();

for (int end = 0; end < s.length(); end++) {
char current = s.charAt(end);

if (characterMaps.containsKey(current)) {
int newindex = characterMaps.get(current);
if (newindex >= start) {
start = newindex + 1;
}
}
characterMaps.put(current, end);

actualmax = Math.max(actualmax, end - start + 1);
}

return actualmax;
}
}
11 changes: 11 additions & 0 deletions Java/Problem4.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

class Solution {
public boolean isMatch(String s, String p) {
Pattern pattern = Pattern.compile("^" + p + "$");
Matcher matcher = pattern.matcher(s);
return matcher.matches();

}
}