Skip to content

Instantly share code, notes, and snippets.

@BeyondSkyCoder
BeyondSkyCoder / RegularExpressionMatch.java
Last active September 22, 2015 18:14
LeetCode Regular Expression Match
public boolean isMatch(String s, String p) {
if (p.length() == 0) {
return s.length() == 0;
}
// case 1: when the second char of p is NOT '*', simple
if (p.length() == 1 || p.charAt(1) != '*') {
if (s.length() < 1) {
return false;
} else if ((p.charAt(0) != s.charAt(0)) && (p.charAt(0) != '.')) {
@BeyondSkyCoder
BeyondSkyCoder / AlienDictionary_graph.java
Created September 22, 2015 18:01
LeetCode Alien Dictionary
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Map;
import java.util.Queue;
import java.util.Set;
public class AlienDictionary_graph {
public String alienOrder(String[] words) {
if (words == null) return null;
@BeyondSkyCoder
BeyondSkyCoder / GasStation_greedy.java
Last active September 22, 2015 17:59
LeetCode Gas Station
public class GasStation_greedy {
public int canCompleteCircuit(int[] gas, int[] cost) {
if (gas == null || cost == null || gas.length == 0 || cost.length == 0 || gas.length != cost.length)
return -1;
int len = gas.length;
int delta = 0;
int total = 0;
int start_station = -1;
@BeyondSkyCoder
BeyondSkyCoder / Candy_greedy.java
Created September 22, 2015 04:39
LeetCode Candy
import java.util.Arrays;
public class Candy_Greedy {
// faster. two scans from left, then from right
public int candyTwoScanFast(int[] ratings) {
if (ratings == null || ratings.length == 0) return 0;
int len = ratings.length;
int tot = 0;
int[] candy = new int[len];