Skip to content

Instantly share code, notes, and snippets.

View keitin's full-sized avatar

Keita Matsushita keitin

View GitHub Profile
public boolean add(E e) {
ensureCapacityInternal(size + 1); // Increments modCount!!
elementData[size++] = e;
return true;
}
public ArrayList(int initialCapacity) {
if (initialCapacity > 0) {
this.elementData = new Object[initialCapacity];
} else if (initialCapacity == 0) {
this.elementData = EMPTY_ELEMENTDATA;
} else {
throw new IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
}
}
@keitin
keitin / .java
Last active January 22, 2017 09:50
void sortByAnagram(String[] array) {
HashMap<String, LinkedList<String>> hash = new HashMap<String, LinkedList<String>>();
/* Group words by anagram */
for (String str : array) {
String key = sortChars(str);
if (!hash.containsKey(key)) {
hash.put(str, new LinkedList<String>());
}
LinkedList<String> list = hash.get(key);
// While Loop Approach
int magicIndex(int[] arr) {
int left = 0;
int right = arr.length - 1;
while (left <= right) {
int mid = (left + right) / 2;
if (mid == arr[mid]) {
return mid;
} else if (mid > arr[mid]) {
left = mid + 1;
// In Follow Up Case: the values are not distinct
int magicIndexF(int[] arr, int left, int right) {
if (left > right || left < 0 || right >= arr.length) {
return -1;
}
int mid = (left + right) / 2;
int midValue = arr[mid];
if (mid == midValue) {
return mid;
}
class RunningChildren {
int N;
int[] memo;
RunningChildren(int N) {
this.N = N;
this.memo = new int[N];
}
class Graph {
Node[] nodes;
int[][] adj;
int size;
boolean search(Graph g, Node start, Node end) {
Queue<Node> q = new LinkedList<Node>();
for (Node node : g.getNodes()) {
node.visited = false;
class StackWithMin {
Node top;
Stack minStack = new Stack();
void push(int item) throws Exception {
if (isEmpty()) {
top = new Node(item);
} else {
Node node = new Node(item);
node.next = top;
class StackWithMin {
Node top;
Stack minStack = new Stack();
}
public boolean deleteNode(Node node) {
if (node == null || node.next == null) {
return false;
}
Node next = node.next;
node.data = next.data;
node.next = next.next;
return true;
}