Skip to content

Commit 09ef724

Browse files
committed
refact: review
1 parent 13fc8ec commit 09ef724

File tree

63 files changed

+293
-310
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+293
-310
lines changed

JavaScript/0008. String to Integer (atoi).js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
* @param {string} str
4242
* @return {number}
4343
*/
44-
function myAtoi1(str) {
44+
const myAtoi1 = (str) => {
4545
// (abc) capture group
4646
//
4747
// [abc] any of a, b, or c
@@ -66,9 +66,9 @@ function myAtoi1(str) {
6666
if (n > max) return max;
6767

6868
return n;
69-
}
69+
};
7070

71-
function myAtoi(str) {
71+
const myAtoi = (str) => {
7272
const s = str.replace(/\s*/, '').match(/^[-+\d]*/);
7373
const n = parseInt(s);
7474

@@ -82,4 +82,4 @@ function myAtoi(str) {
8282
}
8383

8484
return 0;
85-
}
85+
};

JavaScript/0014. Longest Common Prefix.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ function longestCommonPrefix1(strs) {
3333
const str0 = strs[0];
3434
for (let i = 0; i < str0.length; i++) {
3535
const c = str0[i];
36-
for (let s of strs) {
36+
for (const s of strs) {
3737
if (s[i] !== c) return str0.slice(0, i);
3838
}
3939
}
@@ -56,7 +56,7 @@ function longestCommonPrefix(strs) {
5656
}
5757

5858
let minLen = Infinity;
59-
for (let s of strs) {
59+
for (const s of strs) {
6060
minLen = Math.min(minLen, s.length);
6161
}
6262

JavaScript/0017. Letter Combinations of a Phone Number.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
// M is the number of digits in the input that maps to 4 letters (e.g. 7, 9), and N+M is the total number digits in the input.
2222
//
2323
// Space O(3^N * 4^M) since one has to keep O(3^N * 4^M) solutions.
24-
function letterCombinations(digits) {
24+
const letterCombinations = (digits) => {
2525
if (digits == null || digits.length === 0) return [];
2626

2727
const map = {
@@ -36,17 +36,17 @@ function letterCombinations(digits) {
3636
};
3737

3838
const res = [];
39-
function go(i, s) {
39+
const go = (i, s) => {
4040
if (i === digits.length) {
4141
res.push(s);
4242
return;
4343
}
4444

45-
for (let c of map[digits[i]]) {
45+
for (const c of map[digits[i]]) {
4646
go(i + 1, s + c);
4747
}
48-
}
48+
};
4949

5050
go(0, '');
5151
return res;
52-
}
52+
};

JavaScript/0019. Remove Nth Node From End of List.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
/** Two pointers slow and fast */
2727
// Time O(n)
2828
// Space O(1)
29-
function removeNthFromEnd(head, n) {
29+
const removeNthFromEnd = (head, n) => {
3030
const preHead = new ListNode(null); // for case n = 1, to remove the last node in the list to avoid slow.next is null in slow.next.next
3131
preHead.next = head;
3232

@@ -44,4 +44,4 @@ function removeNthFromEnd(head, n) {
4444

4545
slow.next = slow.next.next;
4646
return preHead.next;
47-
}
47+
};

JavaScript/0022. Generate Parentheses.js

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@
2020
// Space O((2^2n) * n)
2121
//
2222
// We can generate all 2^2n sequences of '(' and ')' characters. Then, we will check if each one is valid.
23-
function generateParenthesis1(n) {
23+
const generateParenthesis1 = (n) => {
2424
let res = [];
2525

26-
function generate(arr) {
26+
const generate = (arr) => {
2727
if (arr.length === 2 * n) {
2828
if (valid(arr)) {
2929
res.push(arr.join(''));
@@ -36,50 +36,50 @@ function generateParenthesis1(n) {
3636
generate(arr);
3737
arr.pop();
3838
}
39-
}
39+
};
4040

41-
function valid(arr) {
41+
const valid = (arr) => {
4242
let bal = 0;
43-
for (let c of arr) {
43+
for (const c of arr) {
4444
if (c === '(') bal += 1;
4545
else bal -= 1;
4646
if (bal < 0) return false
4747
}
4848
return bal === 0;
49-
}
49+
};
5050

5151
generate([]);
5252
return res;
53-
}
53+
};
5454

5555
/** 2) Backtracking */
5656
// The complexity analysis rests on understanding how many elements there are in generateParenthesis(n). It turns out
5757
// this is the n-th Catalan number 1 / n+1 (2n n), which is bounded asymptotically by 4^n / (n * sqrt(n))
5858
// Time O(4^n / sqrt(n)). Each valid sequence has at most n steps during the backtracking procedure.
5959
// Space O(4^n / sqrt(n)). As described above, and using O(n) space to store the sequence.
6060

61-
function generateParenthesis2(n) {
61+
const generateParenthesis2 = (n) => {
6262
const res = [];
6363

64-
function go(l, r, s) {
64+
const go = (l, r, s) => {
6565
if (s.length === 2 * n) {
6666
res.push(s);
6767
return;
6868
}
6969

7070
if (l < n) go(l + 1, r, s + '(');
7171
if (r < l) go(l, r + 1, s + ')');
72-
}
72+
};
7373

7474
go(0, 0, '');
7575
return res;
76-
}
76+
};
7777

7878
/** 3) Backtracking, similar to 2) */
79-
function generateParenthesis3(n) {
79+
const generateParenthesis3 = (n) => {
8080
const res = [];
8181

82-
function go(l, r, s) { // l: left remaining, r: right remaining
82+
const go = (l, r, s) => { // l: left remaining, r: right remaining
8383
if (l > r) return; // Check valid by the number of '(' should be always >= ')'
8484

8585
if (l === 0 && r === 0) {
@@ -89,11 +89,11 @@ function generateParenthesis3(n) {
8989

9090
if (l > 0) go(l - 1, r, s + '(');
9191
if (r > 0) go(l, r - 1, s + ')');
92-
}
92+
};
9393

9494
go(n, n, '');
9595
return res;
96-
}
96+
};
9797

9898
/** 4) Closure number */
9999
// Time O(4^n / sqrt(n))
@@ -109,18 +109,18 @@ function generateParenthesis3(n) {
109109
//
110110
// For each closure number i, we know the starting and ending brackets must be at index 0 and 2*i + 1. Then, the
111111
// 2*i elements between must be a valid sequence, plus the rest of the elements must be a valid sequence.
112-
function generateParenthesis(n) {
112+
const generateParenthesis = (n) => {
113113
const res = [];
114114
if (n === 0) {
115115
res.push('');
116116
} else {
117117
for (let i = 0; i < n; i++) {
118-
for (let l of generateParenthesis(i)) {
119-
for (let r of generateParenthesis(n - 1 - i)) {
118+
for (const l of generateParenthesis(i)) {
119+
for (const r of generateParenthesis(n - 1 - i)) {
120120
res.push('(' + l + ')' + r);
121121
}
122122
}
123123
}
124124
}
125125
return res;
126-
}
126+
};

JavaScript/0028. Implement strStr().js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,32 +25,31 @@
2525
*/
2626

2727
/** 1) Cheating */
28-
function strStr1(haystack, needle) {
28+
const strStr1 = (haystack, needle) => {
29+
if (needle == null || needle === '') return 0;
2930
return haystack.indexOf(needle);
30-
}
31+
};
3132

3233
/** 2) Brute force */
33-
function strStr2(haystack, needle) {
34+
const strStr2 = (haystack, needle) => {
35+
if (needle == null || needle === '') return 0;
3436
for (let i = 0; i < haystack.length - needle.length + 1; i++) {
3537
if (haystack.substr(i, needle.length) === needle) return i;
3638
}
37-
3839
return -1;
39-
}
40+
};
4041

4142
/** 3) Same to 2) */
42-
function strStr(haystack, needle) {
43+
const strStr = (haystack, needle) => {
4344
if (needle == null || needle === '') return 0;
44-
4545
for (let i = 0; i < haystack.length - needle.length + 1; i++) {
4646
for (let j = 0; j < needle.length; j++) {
4747
if (haystack[i + j] !== needle[j]) break;
4848
if (j === needle.length - 1) return i;
4949
}
5050
}
51-
5251
return -1;
53-
}
52+
};
5453

5554
/** 4) KMP */
5655
// https://www.zhihu.com/question/21923021

JavaScript/0029. Divide Two Integers.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@
2525
*/
2626

2727
/** 1) Cheating */
28-
function divide1(dividend, divisor) {
28+
const divide1 = (dividend, divisor) => {
2929
if (dividend === -(2 ** 31) && divisor === -1) return 2 ** 31 - 1;
3030
return ~~(dividend / divisor);
31-
}
31+
};
3232

3333
/** 2) */
3434
// https://leetcode.com/problems/divide-two-integers/discuss/13407/Detailed-Explained-8ms-C++-solution
35-
function divide(dividend, divisor) {
35+
const divide = (dividend, divisor) => {
3636
if (dividend === -(2 ** 31) && divisor === -1) return 2 ** 31 - 1;
3737

3838
const sign = Math.sign(dividend) * Math.sign(divisor);
@@ -56,6 +56,5 @@ function divide(dividend, divisor) {
5656
res += multiple;
5757
dividend -= tmp;
5858
}
59-
6059
return sign * res;
61-
}
60+
};

JavaScript/0038. Count and Say.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,15 @@
2727
* @param {number} n
2828
* @return {string}
2929
*/
30-
function countAndSay(n) {
30+
const countAndSay = (n) => {
3131
let res = '1';
3232
for (let i = 1; i < n; i++) {
3333
res = say(res);
3434
}
3535
return res;
36-
}
36+
};
3737

38-
function say(s) {
38+
const say = (s) => {
3939
let res = '';
4040
let count = 0;
4141
let num = s[0];
@@ -49,6 +49,5 @@ function say(s) {
4949
num = s[i];
5050
}
5151
}
52-
5352
return res + count + num;
54-
}
53+
};

JavaScript/0046. Permutations.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@
2727
// Let's do a rough estimation of the result: N! <= ∑(k = 1 to N) (N! / (N - k)!) = ∑(k = 1 to N) P(N, k) <= N * N!,
2828
// i.e. the algorithm performs better than O(N * N!) and a bit slower than N!.
2929
// Space O(N!) since one has to keep N! solutions.
30-
function permute(nums) {
30+
const permute = (nums) => {
3131
const res = [];
3232

33-
function go(curr, rest) {
33+
const go = (curr, rest) => {
3434
if (!rest.length) {
3535
res.push(curr);
3636
return;
@@ -43,8 +43,8 @@ function permute(nums) {
4343
[...rest.slice(0, i), ...rest.slice(i + 1)],
4444
);
4545
}
46-
}
46+
};
4747

4848
go([], nums);
4949
return res;
50-
}
50+
};

0 commit comments

Comments
 (0)