Skip to content

Instantly share code, notes, and snippets.

View linzhp's full-sized avatar

Zhongpeng Lin linzhp

View GitHub Profile
def max a, b
diff = a - b
[b, a][(diff / diff.abs + 1) / 2]
end
@linzhp
linzhp / gist:3718306
Created September 13, 2012 22:48
A jasmine spec showing that events may not be handled in the order they are triggered
describe('A jquery element', function() {
it('should invoke handlers in the order of events triggered from it', function() {
var tokens = [];
var elem = document.createElement('div');
function e1Handler1(payload) {
tokens.push('e1Handler1');
$(elem).trigger('Event2');
}
function e1Handler2(payload) {tokens.push('e1Handler2');}
function e2Handler1(payload) {tokens.push('e2Handler1');}
public class Solution {
/*
Special cases:
* A.length == 0 && B.length == 0
* A.length != 0 && B.length == 0
* A.length == 1 && B.length == 1
[2], [1,3,4]
*/
public double findMedianSortedArrays(int A[], int B[]) {
public class Solution {
public int lengthOfLongestSubstring(String s) {
// Start typing your Java solution below
// DO NOT write main() function
String longest = "";
for(int i = 0; i < s.length(); i++) {
HashSet<Character> set = new HashSet<Character>();
set.add(s.charAt(i));
int j;
for(j = i + 1; j < s.length(); j++) {
public class Solution {
public int lengthOfLongestSubstring(String s) {
// Start typing your Java solution below
// DO NOT write main() function
String longest = "";
int i = 0;
while(i < s.length()) {
HashMap<Character, Integer> map = new HashMap<Character, Integer>();
map.put(s.charAt(i), i);
int j;
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
public class Solution {
public String longestPalindrome(String s) {
// Start typing your Java solution below
// DO NOT write main() function
String longest = "";
for(int i = 0; i < s.length(); i++) {
int j;
// pivot at a char
for(j = 1; i - j >=0 && i + j < s.length(); j++) {
if(s.charAt(i - j) != s.charAt(i + j)) {
@linzhp
linzhp / Jakefile
Created September 17, 2013 17:43
Code for communicating with RA
var exec = require('child_process').exec;
var mongoose = require('mongoose');
desc('Runs the mocha unit tests');
task('test', function(params) {
var proc = exec('mocha --recursive --colors');
proc.stdout.pipe(process.stdout, { end: false });
proc.stderr.pipe(process.stderr, { end: false });
proc.on('exit', process.exit);
});
@linzhp
linzhp / gist:7035991
Created October 18, 2013 03:17
脸书面试代码整理
"""
array anagramBuckets(array strings)
Write a function to group an array of strings by anagrams.
Input: An array of strings (possibly with duplicates)
Output: An array of arrays. Each array contains a set of strings from the input array that are anagrams of each other. The output should not contain any duplicates.
Example:
Input: ('abc', 'bac', 'xyz', 'xyz')
@linzhp
linzhp / gist:7140939
Created October 24, 2013 16:55
Palindrome Partitioning II Given a string s, partition s such that every substring of the partition is a palindrome. Return the minimum cuts needed for a palindrome partitioning of s. For example, given s = "aab", Return 1 since the palindrome partitioning ["aa","b"] could be produced using 1 cut.
public class Solution {
private HashMap<String, Boolean> palindromeCache;
private HashMap<String, Integer> resultCache;
public int minCut(String s) {
// Note: The Solution object is instantiated only once and is reused by each test case.
palindromeCache = new HashMap<String, Boolean>();
resultCache = new HashMap<String, Integer>();
return minCutRecursive(s);
}