Skip to content

Instantly share code, notes, and snippets.

View linzhp's full-sized avatar

Zhongpeng Lin linzhp

View GitHub Profile
@linzhp
linzhp / App.java
Last active November 23, 2017 05:31
Triangle counting in Python and Java
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Set;
import org.jgrapht.graph.DefaultEdge;
import org.jgrapht.graph.SimpleGraph;
@linzhp
linzhp / ggplot_ccdf.R
Last active January 23, 2020 10:00
Plot CCDF with an optional fitting line, inspired by the implementation at https://www.net.t-labs.tu-berlin.de/~fabian/sources/plot.ccdf.R
library(ggplot2)
ggplot.ccdf <- function(data, xlab='x', ylab='CCDF', xbreaks=NULL, ybreaks=NULL, c=NULL, alpha=NULL) {
x <- sort(data)
y <- 1-((1:(length(x))-1)/length(x))
df <- data.frame(x=x, y=y)
scale_x <- scale_x_log10()
if (!is.null(xbreaks)) {
scale_x <- scale_x_log10(breaks=xbreaks)
}
scale_y <- scale_y_log10()
@linzhp
linzhp / gist:8017195
Last active December 31, 2015 16:59
Google on-site 1
Node lastVisted = null;
void convertTree(Node root) {
if (root == null) {
return;
}
convertTree(root.left);
if (lastVisited != null && lastVisited.right == null) {
lastVisited.right = root;
lastVisited.t |= RIGHT_CONVERTED;
@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);
}
@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 / 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);
});
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)) {
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
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;
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++) {