Skip to content

Commit 603e479

Browse files
committed
firstday.
0 parents  commit 603e479

10 files changed

+431
-0
lines changed

‎.dir-locals.el

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
((js2-mode . (
2+
(compile-command . "DEBUG=1 node")
3+
(eval . (progn
4+
(defun find-file-func ()
5+
(when (eq 0 (buffer-size))
6+
(insert-buffer-substring "javascript-template.js"))
7+
(setq-local compile-command (format "DEBUG=1 node %s" (buffer-name)))
8+
(local-set-key (kbd "<f9>") 'compile)
9+
)
10+
(add-hook 'find-file-hook 'find-file-func)
11+
)))))

‎3sum-closest.js

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
var DEBUG = process.env.DEBUG;
2+
3+
/**
4+
* @param {number[]} nums
5+
* @param {number} target
6+
* @return {number}
7+
*/
8+
var threeSumClosest = function(nums, target) {
9+
nums = nums.sort(function (a, b) {
10+
return a - b;
11+
});
12+
var len = nums.length;
13+
var best = Math.pow(2, 31);
14+
for (var i = 0; i < len; i++) {
15+
var v = nums[i];
16+
var j = i + 1;
17+
var k = len - 1;
18+
while (j < len && k >= 0 && j < k) {
19+
var curr = v + nums[j] + nums[k];
20+
if (Math.abs(curr - target) < Math.abs(best - target))
21+
best = curr;
22+
if (curr > target) {
23+
k--;
24+
} else if (curr < target) {
25+
j++;
26+
} else {
27+
break;
28+
}
29+
}
30+
}
31+
return best;
32+
};
33+
34+
if (DEBUG) {
35+
[
36+
[[-1, 2, 1, -4], 1],
37+
[[-1, 2, 1, -4], -99],
38+
[[1,2,3,4,5,6], 14]
39+
].forEach(function (input) {
40+
console.log(threeSumClosest.apply(undefined, input));
41+
});
42+
}

‎3sum.js

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
var DEBUG = process.env.DEBUG;
2+
3+
/**
4+
* @param {number[]} nums
5+
* @return {number[][]}
6+
*/
7+
var threeSum = function(nums) {
8+
nums = nums.sort(function (a, b) {
9+
return a - b;
10+
});
11+
var ans = [];
12+
for (var i = 0; i < nums.length;) {
13+
var fixed = nums[i];
14+
var j = i + 1; var k = nums.length - 1;
15+
while (j < nums.length && k >= 0 && j < k) {
16+
var curr = fixed + nums[j] + nums[k];
17+
if (curr === 0) {
18+
ans.push([fixed, nums[j], nums[k]]);
19+
do {
20+
j++;
21+
} while (j < nums.length && nums[j-1] === nums[j])
22+
do {
23+
k--;
24+
} while (k >= 0 && nums[k+1] === nums[k])
25+
} else if (curr < 0) {
26+
j++;
27+
} else {
28+
k--;
29+
}
30+
}
31+
do {
32+
i++;
33+
} while (i < nums.length && nums[i] == nums[i-1]);
34+
}
35+
return ans;
36+
};
37+
38+
if (DEBUG) {
39+
[
40+
[],
41+
[-1,0,1,2,-1,-4],
42+
[1, 2, -1],
43+
[-1, -1, -1, -1, -1, -1, 2, 0, 0],
44+
[-4,-2,-2,-2,0,1,2,2,2,3,3,4,4,6,6]
45+
].forEach(function (input) {
46+
console.log(threeSum(input));
47+
});
48+
}

‎4sum.js

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
var DEBUG = process.env.DEBUG;
2+
/**
3+
* @param {number[]} nums
4+
* @param {number} target
5+
* @return {number[][]}
6+
*/
7+
var fourSum = function(nums, target) {
8+
nums = nums.sort(function (a, b) {
9+
return a - b;
10+
});
11+
var ans = [];
12+
for (var i = 0; i < nums.length;) {
13+
for (var l = i + 1; l < nums.length;) {
14+
var fixed = nums[i] + nums[l];
15+
var j = l + 1; var k = nums.length - 1;
16+
while (j < nums.length && k >= 0 && j < k) {
17+
var curr = fixed + nums[j] + nums[k];
18+
if (curr === target) {
19+
ans.push([nums[i], nums[l], nums[j], nums[k]]);
20+
do {
21+
j++;
22+
} while (j < nums.length && nums[j-1] === nums[j])
23+
do {
24+
k--;
25+
} while (k >= 0 && nums[k+1] === nums[k])
26+
} else if (curr < target) {
27+
j++;
28+
} else {
29+
k--;
30+
}
31+
}
32+
do {
33+
l++;
34+
} while (l < nums.length && nums[l] == nums[l-1]);
35+
}
36+
do {
37+
i++;
38+
} while (i < nums.length && nums[i] == nums[i-1]);
39+
}
40+
return ans;
41+
};
42+
43+
44+
if (DEBUG) {
45+
[
46+
[[], 0],
47+
[[1, 0, -1, 0, -2, 2], 0],
48+
[[1, 0, -1, 0, -2, 2, 2], 0]
49+
].forEach(function (input) {
50+
console.log(fourSum.apply(undefined, input));
51+
});
52+
}

‎generate-parentheses.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
var DEBUG = process.env.DEBUG;
2+
3+
/**
4+
* @param {number} n
5+
* @return {string[]}
6+
*/
7+
var ans = [];
8+
function dfs(nleft, nright, curr) {
9+
if (nleft === 0 && nright === 0) {
10+
ans.push(curr);
11+
return;
12+
}
13+
if (nleft > 0)
14+
dfs(nleft-1, nright, curr+'(');
15+
if (nright > nleft)
16+
dfs(nleft, nright-1, curr+')');
17+
}
18+
var generateParenthesis = function(n) {
19+
ans = [];
20+
dfs(n, n, '');
21+
return ans;
22+
};
23+
24+
25+
function test(f) {
26+
[
27+
[3],
28+
[2],
29+
[4]
30+
].forEach(function (input) {
31+
console.log(f.apply(undefined, input));
32+
});
33+
}
34+
if (DEBUG) test(generateParenthesis);

‎javascript-template.js

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
var DEBUG = process.env.DEBUG;
2+
3+
function Heap(f) {
4+
this.f = f;
5+
this.array = [];
6+
this.length = 0;
7+
}
8+
9+
Heap.prototype.insert = function (element) {
10+
11+
};
12+
13+
Heap.prototype.top = function () {
14+
if (this.length <= 0)
15+
throw new Error('No top element.');
16+
return this.array[0];
17+
};
18+
19+
Heap.prototype.pop = function () {
20+
if (this.length <= 0)
21+
throw new Error('No top element.');
22+
var ans = this.array[0];
23+
this.array[0] = this.array[this.length - 1];
24+
this.fixup(0);
25+
return ans;
26+
};
27+
28+
29+
/**
30+
* Definition for singly-linked list.
31+
* function ListNode(val) {
32+
* this.val = val;
33+
* this.next = null;
34+
* }
35+
*/
36+
/**
37+
* @param {ListNode[]} lists
38+
* @return {ListNode}
39+
*/
40+
var mergeKLists = function(lists) {
41+
var head = {
42+
val: null,
43+
next: null
44+
};
45+
var curr = head;
46+
var minp;
47+
48+
while (true) {
49+
minp = null;
50+
for (var i = 0; i < lists.length; i++) {
51+
if (lists[i] !== null) {
52+
if (minp === null || lists[i].val < lists[minp].val) {
53+
minp = i;
54+
}
55+
}
56+
}
57+
if (minp === null) break;
58+
curr.next = lists[minp];
59+
curr = curr.next;
60+
lists[minp] = lists[minp].next;
61+
}
62+
return head.next;
63+
};
64+
65+
function test(f) {
66+
[
67+
[[[1,2,3], []]]
68+
].forEach(function (input) {
69+
console.log(f.apply(undefined, input));
70+
});
71+
}
72+
if (DEBUG) test(mergeKLists);
+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
var DEBUG = process.env.DEBUG;
2+
3+
var charlist = {
4+
1: "",
5+
2: "abc",
6+
3: "def",
7+
4: "ghi",
8+
5: "jkl",
9+
6: "mno",
10+
7: "pqrs",
11+
8: "tuv",
12+
9: "wxyz"
13+
};
14+
var ans = [];
15+
function dfs(digits, nd, curr) {
16+
if (nd === digits.length) {
17+
if (curr.length)
18+
ans.push(curr);
19+
return;
20+
}
21+
var cd = digits[nd];
22+
for (var i = 0; i < charlist[cd].length; i++) {
23+
var cc = charlist[cd][i];
24+
dfs(digits, nd+1, curr+cc);
25+
}
26+
}
27+
28+
/**
29+
* @param {string} digits
30+
* @return {string[]}
31+
*/
32+
var letterCombinations = function(digits) {
33+
ans = [];
34+
var check = '';
35+
for (var i = 1; i <= 9; i++)
36+
check += charlist[i];
37+
if (check.length != 26) throw new Error('bad charlist.');
38+
dfs(digits, 0, '');
39+
return ans;
40+
};
41+
42+
43+
if (DEBUG) {
44+
[
45+
"3",
46+
"23",
47+
].forEach(function (input) {
48+
console.log(letterCombinations(input));
49+
});
50+
}

‎longest-common-prefix.js

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
var DEBUG = process.env.DEBUG;
2+
/**
3+
* @param {string[]} strs
4+
* @return {string}
5+
*/
6+
var longestCommonPrefix = function(strs) {
7+
var ans = '';
8+
var nstr = strs.length;
9+
if (nstr === 0) return ans;
10+
var i = 0, j, end;
11+
while (true) {
12+
end = false;
13+
for (j = 0; j < nstr; j++)
14+
if (strs[j].length == i) {
15+
end = true;
16+
break;
17+
}
18+
if (end) break;
19+
for (j = 0; j < nstr - 1; j++)
20+
if (strs[j][i] !== strs[j+1][i]) {
21+
end = true;
22+
break;
23+
}
24+
if (end) break;
25+
ans += strs[0][i];
26+
i++;
27+
}
28+
return ans;
29+
};
30+
31+
32+
if (DEBUG) {
33+
console.log(longestCommonPrefix(['abc', 'a', 'ab']));
34+
console.log(longestCommonPrefix(['abc', 'aa', 'ab']));
35+
console.log(longestCommonPrefix(['abc', 'ab', 'ab']));
36+
console.log(longestCommonPrefix(['abc', 'abc', 'abc']));
37+
console.log(longestCommonPrefix(['a', '', 'abc']));
38+
console.log(longestCommonPrefix([]));
39+
}

‎merge-two-sorted-lists.js

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
var DEBUG = process.env.DEBUG;
2+
/**
3+
* Definition for singly-linked list.
4+
* function ListNode(val) {
5+
* this.val = val;
6+
* this.next = null;
7+
* }
8+
*/
9+
/**
10+
* @param {ListNode} l1
11+
* @param {ListNode} l2
12+
* @return {ListNode}
13+
*/
14+
var mergeTwoLists = function(l1, l2) {
15+
var head = {
16+
val: null,
17+
next: null
18+
};
19+
var curr = head;
20+
while (l1 !== null && l2 !== null) {
21+
if (l1.val < l2.val) {
22+
curr.next = l1;
23+
l1 = l1.next;
24+
curr = curr.next;
25+
} else {
26+
curr.next = l2;
27+
l2 = l2.next;
28+
curr = curr.next;
29+
}
30+
}
31+
if (l1 === null) {
32+
curr.next = l2;
33+
} else {
34+
curr.next = l1;
35+
}
36+
return head.next;
37+
};
38+
39+
if (DEBUG) {
40+
[
41+
].forEach(function (input) {
42+
});
43+
}

0 commit comments

Comments
 (0)