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
type SomeStruct struct { | |
AnotherStructToEmbeddFieldsFrom | |
ThisOneIsVisible string | |
notThisOneThough string | |
} |
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
func main() { | |
a := 4 | |
defer fmt.Println(a) | |
defer fmt.Println(3) | |
fmt.Println(1) | |
fmt.Println(2) | |
a = 5 | |
// this prints 1, 2, 3, 4 | |
} |
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
int mod = 1000000007; | |
int p = 97; //prime | |
long hash = 0, power = 1; | |
for(char c: str) { | |
long add = ((long)c * power); | |
hash += add; | |
hash %= mod; | |
power = power * p % mod; | |
} | |
// in mod space x/p is the same as x*pinv |
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 boolean canConstructSum(int[] a, int sum) { | |
int n = a.length; | |
boolean[] dp = new boolean[sum+1]; | |
dp[0] = true; | |
for(int i = 0; i < n; ++i) { | |
for(int s = sum - a[i]; s >= 0; --s) { | |
dp[s+a[i]] |= dp[s]; | |
} | |
} | |
return dp[sum]; |
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 RunnableWithSyncExample implements Runnable { | |
private Object someSharedObject; | |
public SomethingRunnableWithSync(Object sharedObject) { | |
someSharedObject = sharedObject; | |
} | |
public void run() { | |
synchronized(someSharedObject) { | |
// do something with someSharedObject | |
// and then optionally wait or notify/notifyAll | |
someSharedObject.wait(); |
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 BoundedBlockingQueue { | |
private Deque<Integer> queue; | |
private Semaphore enqueue; | |
private Semaphore dequeue; | |
public BoundedBlockingQueue(int capacity) { | |
queue = new ConcurrentLinkedDeque<>(); | |
enqueue = new Semaphore(capacity); | |
dequeue = new Semaphore(0); | |
} |
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
boolean[] visited = new boolean[N+1]; | |
// [distance, toNode] | |
PriorityQueue<int[]> pq = new PriorityQueue<>((a, b) -> (a[0] - b[0])); | |
pq.add(new int[]{0, startNode}); | |
while(!pq.isEmpty()) { | |
int[] current = pq.poll(); | |
int node = current[1]; | |
int dist = current[0]; | |
// if reached target node, return dist, or if task is to cover entire graph continue | |
if(!visited[node]) { |
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 RangeSumSegmentTree { | |
private int n = 0; | |
private int[] tree; | |
public RangeSumSegmentTree(int[] a) { | |
n = a.length; | |
tree = new int[2*n]; | |
for(int i = 0; i < n; ++i) { | |
update(i, a[i]); | |
} | |
} |
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
// recursive | |
public List<Integer> inorderTraversal(TreeNode root) { | |
List<Integer> ans = new ArrayList(); | |
if(root == null) return ans; | |
ans.addAll(inorderTraversal(root.left)); | |
ans.add(root.val); | |
ans.addAll(inorderTraversal(root.right)); | |
return ans; | |
} |
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
class DisjointSetUnion { | |
int[] parent; | |
public DisjointSetUnion(int n) { | |
parent = new int[n]; | |
for(int i = 0; i < n; ++i) { | |
parent[i] = i; | |
} | |
} | |
public int find(int x) { | |
if(parent[x] != x) { |
NewerOlder