Skip to content

Instantly share code, notes, and snippets.

View weidagang's full-sized avatar

Dagang Wei weidagang

  • Bay Area, California
View GitHub Profile
/*
http://oj.leetcode.com/problems/trapping-rain-water/
Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.
For example,
Given [0,1,0,2,1,0,1,3,2,1,2,1], return 6.
The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped. Thanks Marcos for contributing this image!
*/
/*
http://oj.leetcode.com/problems/binary-tree-level-order-traversal-ii/
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).
For example:
Given binary tree {3,9,20,#,#,15,7},
return its bottom-up level order traversal as:
/*
http://oj.leetcode.com/problems/interleaving-string/
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2.
For example,
Given:
s1 = "aabcc",
s2 = "dbbca",
@weidagang
weidagang / kth-smallest-in-binary-search-tree.cpp
Last active August 29, 2015 13:56
Kth Smallest Number in Binary Search Tree
/*
Find the kth smallest number in the binary search tree.
*/
#include <cstdio>
#include <stack>
#include <queue>
struct TreeNode {
int val;
/*
Given a binary tree, return the inorder traversal of its nodes' values.
For example:
Given binary tree {1,#,2,3},
1
\
2
/
3
#include <cstdio>
#include <vector>
#include <algorithm>
class Solution {
public:
int threeSumClosest(std::vector<int> &num, int target) {
int closest = num[0] + num[1] + num[2];
std::sort(num.begin(), num.end());
@weidagang
weidagang / bash_aliases.sh
Last active August 29, 2015 14:01
Useful bash aliases
# Bash command aliases and utils
# code format
function astyle-java {
astyle --mode=java --style=linux -s4 $@
}
function astyle-c {
astyle --mode=c --style=linux -s4 $@
}
@weidagang
weidagang / Boolean.scala
Last active August 29, 2015 14:03
Make True and False as subtypes of Boolean in Scala (Note that the "object" keyword is the Scala way to declare a singleton class)
abstract class Boolean {
def == (other: Boolean): Boolean;
def != (other: Boolean): Boolean;
def && (other: Boolean): Boolean;
def || (other: Boolean): Boolean;
def unary_! (): Boolean;
}
object True extends Boolean {
def == (other: Boolean) = other;
@weidagang
weidagang / timespan.pl
Created July 1, 2014 02:46
Perl: timespan between 2 keywords
#!/usr/bin/env perl
use File::Basename
$nargs = @ARGV;
sub usage() {
my $prog = basename($0);
print "Usage: $prog <start-text> <end-text>\n";
}
@weidagang
weidagang / ListLength.scala
Created July 1, 2014 08:18
HackerRank: List Length
package list.length
object Main {
def f(arr: List[Int]): Int = arr match {
case x :: xs => 1 + f(xs)
case _ => 0
}
def main(args: Array[String]) {
val len = f(List(1, 2, 3));