This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.lang.Exception; | |
| import java.util.List; | |
| import java.util.ArrayList; | |
| class Parser { | |
| static public class ParseException extends Exception { | |
| } | |
| static public class Edge { | |
| public Edge(char src, char dst, int weight) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Suppose you have a simple language with three possible tokens: + - p. The runtime of your language keeps a counter. | |
| // The command + increments the counter, - decrements, and p prints. | |
| type Runtime struct { | |
| Counter int | |
| } | |
| // An interpreter. | |
| func interprete(r *Runtime, program string) { | |
| for _, r := range program { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fn c { clear; tmux clear } | |
| fn ls { e:ls -G $@ } | |
| if (eq $E:ELVISH_PATH "") { | |
| E:ELVISH_PATH = 1 | |
| paths = [~/Library/Python/*/bin ~/.cargo/bin ~/on/*[nomatch-ok]/bin $@paths] | |
| } | |
| edit:binding[insert][Down] = $&nop | |
| edit:binding[insert][Alt-b] = $edit:&move-dot-left-word |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class Solution { | |
| public double myPow(double x, int n) { | |
| if (n < 0 && -n < 0) { | |
| n /= 2; | |
| x *= x; | |
| } | |
| if (n < 0) { | |
| n = -n; | |
| x = 1 / x; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Definition for singly-linked list. | |
| * public class ListNode { | |
| * int val; | |
| * ListNode next; | |
| * ListNode(int x) { val = x; } | |
| * } | |
| */ | |
| public class Solution { | |
| public ListNode mergeKLists(ListNode[] lists) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| template <int i> | |
| void f(int& num, string& roman, const char *s) { | |
| while (num >= i) { | |
| roman += s; | |
| num -= i; | |
| } | |
| } | |
| class Solution { | |
| public: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| memo = {0: 0, 1: 1} | |
| def fib(n): | |
| if n in memo: | |
| return memo[n] | |
| result = fib(n-1) + fib(n-2) | |
| memo[n] = result | |
| return result | |
| # Or, a slightly cleaner way to write it: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| memo = {} | |
| def isCharMatch(s, p): | |
| return p in ('.', s) | |
| def isMatch(s, p): | |
| if (s, p) not in memo: | |
| memo[(s, p)] = isMatchInner(s, p) | |
| return memo[(s, p)] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def isCharMatch(s, p): | |
| return p in ('.', s) | |
| def isMatch(s, p): | |
| if p == '': | |
| return s == '' | |
| elif len(p) == 1: | |
| return len(s) == 1 and isCharMatch(s, p) | |
| if p[-1] != '*': |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class Solution { | |
| private Map<String, Map<String, boolean>> memo = new HashMap<String, HashMap<String, boolean>>(); | |
| public boolean hasMemo(String s, String p) { | |
| return memo.containsKey(s) && memo.get(s).containsKey(p); | |
| } | |
| public boolean getMemo(String s, String p) { | |
| return memo.get(s).get(p); | |
| } |