This file contains hidden or 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
| public boolean add(E e) { | |
| ensureCapacityInternal(size + 1); // Increments modCount!! | |
| elementData[size++] = e; | |
| return true; | |
| } |
This file contains hidden or 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
| 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); | |
| } | |
| } |
This file contains hidden or 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
| 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); |
This file contains hidden or 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
| // 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; |
This file contains hidden or 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
| // 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; | |
| } |
This file contains hidden or 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
| class RunningChildren { | |
| int N; | |
| int[] memo; | |
| RunningChildren(int N) { | |
| this.N = N; | |
| this.memo = new int[N]; | |
| } | |
This file contains hidden or 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
| 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; |
This file contains hidden or 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
| 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; |
This file contains hidden or 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
| class StackWithMin { | |
| Node top; | |
| Stack minStack = new Stack(); | |
| } |
This file contains hidden or 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
| 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; | |
| } |