Skip to content

Instantly share code, notes, and snippets.

@z11i
z11i / DP_TSP.java
Last active November 11, 2015 03:02
memo = new int[V][1 << V];
T = new int[V][V]; // adjacency matrix
int min_dist_from_0 = DP_TSP(0, 1);
int DP_TSP(int u, int m) { // m is visited mask (bitmask)
if (m == ((1 << V) - 1)) // all 1, all vertices have been visited
return T[u][0]; // no choice, return to vertex 0
if (memo[u][m] != -1) // computed before
return memo[u][m];
void dijkstra() {
int[] dist = new int[n + 1];
boolean[] visited = new boolean[n + 1];
for (int source = 0; source < k + 1; source++) {
arrays.fill(dist, INFINITY);
arrays.fill(visited, false);
dist[list[source]] = 0;
for (int t = 0; t < n; t++) {
int min = INFINITY;
@z11i
z11i / coding-standards-for-cpp.md
Last active May 16, 2024 10:00
Coding Standards for C++ (Extract)
@z11i
z11i / UniqueArray.java
Created February 6, 2016 13:08
Remove duplicates in an array while maintaining the original order
import java.util.*;
public class UniqueArray {
public static int[] unique(int[] integers) {
return Arrays.stream(integers).distinct().toArray();
}
}
@z11i
z11i / unfollow.js
Created November 25, 2017 04:48
批量取关知乎关注问题。去 https://www.zhihu.com/question/following 页面执行脚本。会取消所有关注的问题。
function unfollow() {
window.scrollTo(0, document.body.scrollHeight);
setTimeout(function() {
console.log('unfollowed');
let list = document.querySelectorAll('.follow-link.zg-unfollow.meta-item');
list[0].click();
unfollow()
}, 1000)
}
unfollow()
@z11i
z11i / karabiner-command-escape.json
Last active December 10, 2022 10:20
Karabiner complex modification: Post command+backtick if command+escape is pressed. This is useful to those who use 60% compact keyboards on which backtick/grave keys are replaced with the escape key.
{
"title": "Emit Command+Backtick if Command and Escape are pressed",
"rules": [
{
"description": "Change escape to backtick if pressed with command",
"manipulators": [
{
"type": "basic",
"from": {
"key_code": "escape",
@z11i
z11i / strip_html_tags.go
Created June 4, 2020 08:30
Strip HTML tags in Golang
import (
"html"
"reflect"
"strings"
"github.com/microcosm-cc/bluemonday"
)
var p = bluemonday.UGCPolicy()
@z11i
z11i / replacer.js
Last active November 28, 2020 07:26
Replaces all regular expression patterns in a string with dynamic data from a map.
function makeReplacer(str) {
const regex = /\$\w+\.(\w+)/g;
let strFragments = []; // stores static strings, or a function to get value from map
let m; // regex match object
let idx; // index tracking the position of one match in `str`
while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
@z11i
z11i / upload.sh
Last active November 16, 2024 22:05
Use curl to upload a file in a multipart/form-data request, with custom content-type for the file (not the request)
filename='yourfilename'
filetype='text/csv'
token='my oauth token'
url='http://localhost/upload'
curl "$url" \
--form "data=@$filename;type=$filetype" \
--form "name=somename" \
-H "Authorization: Bearer $token"