Skip to content

Instantly share code, notes, and snippets.

View adohe-zz's full-sized avatar

TonyAdo adohe-zz

View GitHub Profile
@adohe-zz
adohe-zz / EqHash
Created July 17, 2014 02:38
equals() and hashCode() contract in Java
package com.westudio.java;
import java.util.HashMap;
import java.util.Map;
class Apple {
public String color;
public Apple(String color) {
@adohe-zz
adohe-zz / Reflect
Created July 16, 2014 05:57
Java reflect experience
package com.westudio.java;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Modifier;
/**
* Reflect experience
*/
class ReflectClass {
@adohe-zz
adohe-zz / Compare
Created July 15, 2014 03:25
The difference between comparable and comparator....
package com.westudio.java;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
class HDTV implements Comparable<HDTV> {
private int size;
@adohe-zz
adohe-zz / CheckExists
Created July 14, 2014 02:39
How to check if an array (unsorted) contains a certain value?
package com.westudio.java;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
/**
* Actually, if you really need to check if a value is contained in some array/collection efficiently,
* a sorted list or tree can do it in O(log(n)) or hashset can do it in O(1).
*/
@adohe-zz
adohe-zz / LeetCodeFive
Created July 13, 2014 05:13
Find the median or k-th element of two sorted arrays
package com.ado.java;
public class LeetCodeFive {
private static double findMedianOfSortedArray(int[] a, int[] b) {
int m = a.length;
int n = b.length;
if ((m + n) % 2 == 0) {
// even
@adohe-zz
adohe-zz / NoVisibility
Created July 12, 2014 12:41
Memory visibility when lack of synchronization
package com.ado.java;
public class NoVisibility {
private static int number;
private static boolean ready;
private static class ReaderThread extends Thread {
@Override
public void run() {
@adohe-zz
adohe-zz / proto
Created July 10, 2014 05:27
JavaScript prototype chain thought
function getProperty (obj, prop) {
if (obj.hasOwnProperty(prop)) {
return obj[prop];
}
if (obj.__proto__ !== null) {
return getProperty(obj.__proto__, prop);
}
return undefined;
@adohe-zz
adohe-zz / leet
Last active August 29, 2015 14:03
LeetCode four
package com.westudio.java;
import java.util.*;
public class LeetCodeFour {
private static int ladderLength(String start, String end, Set<String> dict) {
if (start == null || end == null ||
start.equals(end) || start.length() != end.length()) {
return -1;
@adohe-zz
adohe-zz / list
Last active August 29, 2015 14:03
A list implementation in C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef int elementType;
typedef struct Node {
elementType d;
Node* next;
} Node;
@adohe-zz
adohe-zz / whatever
Created July 5, 2014 05:31
Node Startup Progress
The work flow: Node_main.cc: wmain(win), main(*nix)->Node.cc: node::Start->Load->ExecuteString->node.js: startup->Module.js: Module.runMain()->Module._load->Module.prototype.load->Module._extensions['.js']->NativeModule.require('fs').readFileSync