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
| /** | |
| * Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. | |
| * A region is captured by flipping all 'O's into 'X's in that surrounded region . | |
| * For example, | |
| * X X X X | |
| X O O X | |
| X X O X | |
| X O X X | |
| * After running your function, the board should be: | |
| * X X X X |
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
| /** | |
| * Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words. | |
| * For example, given | |
| * s = "leetcode", | |
| * dict = ["leet", "code"]. | |
| * Return true because "leetcode" can be segmented as "leet code". | |
| */ | |
| public class Solution { | |
| public boolean wordBreak(String s, List<String> dict) { | |
| if(s == null || dict == null) |
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 V get(Object key){ | |
| if(key == null) | |
| return getForNullKey(); | |
| int hash = hash(key.hashCode()); | |
| for(Entry<K, V> e = table[indexFor(hash, table.length)]; e != null; e = e.next) { | |
| Object k; | |
| if(e.hash == hash && ((k = e.key) == key || key.equals(k))) { | |
| return e.value; | |
| } | |
| } |
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 addEntry(int hash, K key, V value, int bucketIndex){ | |
| Entry<K, V> e = table[bucketIndex]; | |
| table[bucketIndex] = new Entry<K, V>(hash, key, value, e); | |
| if(size++ >= threshold) | |
| resize(2 * table.length); | |
| } |
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 V put(K key, V value){ | |
| if(key == null) | |
| return putForNullKey(value); | |
| int hash = hash(key.hashCode()); | |
| int i = indexFor(hash, table.length); | |
| for(Entry<K, V> e = table[i]; e != null; e = e.next){ | |
| Object k; | |
| // Collision | |
| if(e.hash == hash && K == e.key || key.equals(k)){ | |
| V oldValue = e.value; |
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
| /** | |
| * Given a singly linked list L: L0→L1→…→Ln-1→Ln, | |
| * reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… | |
| * You must do this in-place without altering the nodes' values. | |
| * For example, | |
| * Given {1,2,3,4}, reorder it to {1,4,2,3}. | |
| */ | |
| public class Solution { | |
| public void reorderList(ListNode head) { |
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
| /** | |
| * Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. | |
| */ | |
| /** | |
| * Definition for a point. | |
| * class Point { | |
| * int x; | |
| * int y; | |
| * Point() { x = 0; y = 0; } | |
| * Point(int a, int b) { x = a; y = b; } |
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 class Q2_reverseString { | |
| // Method 1: built in StringBuffer function | |
| public static void reverseString(String str) { | |
| for (String part : str.split(" ")) { | |
| System.out.print(new StringBuffer(part).reverse().toString()); | |
| System.out.print(" "); | |
| } | |
| } |
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 class Solution{ | |
| // With additional data structures / space | |
| public boolean isUniqueChars(String str){ | |
| if(str.length() > 256) | |
| return false; | |
| boolean[] char_set = new boolean[256]; | |
| for(int i = 0; i < str.length(); i++){ | |
| int val = str.charAt(i); | |
| if(char_set[val]) | |
| return 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
| public class Dijkstra { | |
| // s stands for the start point | |
| private Graph graph; | |
| //priority queue stores all of the nodes, reachable from the start node | |
| //The queue is sorted by the node.distance | |
| private GraphNodePriorityQueue priorityQ = new GraphNodePriorityQueue(); | |
| private Hashtable <GraphNode,Integer> distance = new Hashtable<GraphNode, Integer>(); | |
| //1. needs to get the list of all nodes in the graph | |
| //2. need to initialize distance vector to infinity |