Skip to content

Instantly share code, notes, and snippets.

View wayetan's full-sized avatar

Wei Tan wayetan

  • Microsoft
  • United States
View GitHub Profile
@wayetan
wayetan / SurroundedRegions.java
Last active October 21, 2015 09:45
Surrounded Regions
/**
* 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
@wayetan
wayetan / WordBreak.java
Last active March 27, 2017 18:17
Word Break
/**
* 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)
@wayetan
wayetan / HashMapPut.java
Created February 14, 2014 09:07
HashMap java Implementation Get Function
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;
}
}
@wayetan
wayetan / HashMapAddEntry.java
Created February 14, 2014 08:59
HashMap java Implementation addEntry Function
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);
}
@wayetan
wayetan / HashMapPut.java
Created February 14, 2014 08:50
HashMap Java Implementation Put Function
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;
@wayetan
wayetan / reorderlist.java
Last active August 29, 2015 13:56
Reorder List
/**
* 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) {
@wayetan
wayetan / MaxPointsonaLine.java
Created February 4, 2014 08:38
Max Points on a Line
/**
* 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; }
@wayetan
wayetan / ReverseaString.java
Created January 18, 2014 10:22
Reverse a String
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(" ");
}
}
@wayetan
wayetan / UniqueChars.java
Created January 18, 2014 09:45
Unique Chars
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;
@wayetan
wayetan / Dijkstra.java
Created January 7, 2014 09:55
Dijkstra
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