Skip to content

Commit df2f337

Browse files
Added eslint
1 parent b64dd4c commit df2f337

27 files changed

+1653
-40
lines changed

.eslintrc.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
module.exports = {
2+
"env": {
3+
"node": true,
4+
"es2021": true
5+
},
6+
7+
"extends": "eslint:recommended",
8+
"parserOptions": {
9+
"ecmaVersion": "latest",
10+
"sourceType": "module"
11+
},
12+
"rules": {
13+
"indent": [
14+
"error",
15+
2
16+
],
17+
"linebreak-style": [
18+
"error",
19+
"unix"
20+
],
21+
"quotes": [
22+
"error",
23+
"double"
24+
],
25+
"semi": [
26+
"error",
27+
"always"
28+
]
29+
}
30+
};

LeetcodeProblems/Algorithms/Add_Two_Numbers.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ var addTwoNumbers = function (l1, l2) {
4141

4242
const head = number;
4343
while (l1 !== null || l2 !== null) {
44-
var elem = carry;
44+
elem = carry;
4545
if (l1 !== null) {
4646
elem += l1.val;
4747
l1 = l1.next;

LeetcodeProblems/Algorithms/Backspace_String_Compare.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,11 @@ Can you solve it in O(N) time and O(1) space?
4040
var backspaceCompare = function (S, T) {
4141
var iterS = S.length - 1;
4242
var iterT = T.length - 1;
43-
43+
var countBack = 0;
44+
4445
while (iterS >= 0 || iterT >= 0) {
4546
if (iterS >= 0 && S.charAt(iterS) === "#") {
46-
var countBack = 0;
47+
countBack = 0;
4748
while (iterS >= 0 && (countBack > 0 || S[iterS] === "#")) {
4849
if (iterS >= 0 && S[iterS] === "#") {
4950
countBack++;
@@ -54,7 +55,7 @@ var backspaceCompare = function (S, T) {
5455
iterS--;
5556
}
5657
} else if (iterT >= 0 && T.charAt(iterT) === "#") {
57-
var countBack = 0;
58+
countBack = 0;
5859
while (iterT >= 0 && (countBack > 0 || T[iterT] === "#")) {
5960
if (iterT >= 0 && T[iterT] === "#") {
6061
countBack++;
@@ -85,7 +86,7 @@ var backspaceCompare2 = function (S, T) {
8586
}
8687

8788
var stackT = [];
88-
for (var i = 0; i < T.length; i++) {
89+
for (i = 0; i < T.length; i++) {
8990
if (T.charAt(i) === "#") stackT.shift();
9091
else stackT.unshift(T.charAt(i));
9192
}

LeetcodeProblems/Algorithms/Clone_Graph.js

+5
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ You don't need to understand the serialization to solve the problem.
4040
* }
4141
*/
4242

43+
class UndirectedGraphNode {} //TODO: Define me
44+
4345
// SOLUTION 1 Using DFS
4446
/**
4547
* @param {UndirectedGraphNode} graph
@@ -93,3 +95,6 @@ var cloneGraphBFS = function (graph) {
9395

9496
return copyReturn;
9597
};
98+
99+
module.exports.cloneGraph = cloneGraph;
100+
module.exports.cloneGraphBFS = cloneGraphBFS;

LeetcodeProblems/Algorithms/Coin_Change.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ var coinChange = function (coins, amount) {
2525
for (var i = 0; i <= amount; i++) memo[i] = Number.POSITIVE_INFINITY;
2626

2727
memo[0] = 0;
28-
for (var i = 0; i < coins.length; i++) {
28+
for (i = 0; i < coins.length; i++) {
2929
const coin = coins[i];
3030
for (var j = coin; j < memo.length; j++)
3131
memo[j] = min2(memo[j], memo[j - coin] + 1);
@@ -105,3 +105,5 @@ var min = function (a, b, c) {
105105
};
106106

107107
module.exports.coinChange = coinChange;
108+
module.exports.coinChange1 = coinChange1;
109+
module.exports.coinChange2 = coinChange2;

LeetcodeProblems/Algorithms/Construct_Binary_Tree_from_Preorder_and_Inorder_Traversal.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ var buildTree = function (preorder, inorder) {
4040
inorder === null ||
4141
preorder.length !== inorder.length
4242
)
43-
return nil;
43+
return null;
4444

4545
return buildTreeAux(
4646
preorder,

LeetcodeProblems/Algorithms/Edit_Distance.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ var minDistance = function (word1, word2) {
4141
}
4242
}
4343

44-
for (var i = 1; i <= word1.length; i++) {
45-
for (var j = 1; j <= word2.length; j++) {
44+
for (i = 1; i <= word1.length; i++) {
45+
for (j = 1; j <= word2.length; j++) {
4646
if (word1.charAt(i - 1) === word2.charAt(j - 1)) {
4747
matrix[i][j] = matrix[i - 1][j - 1];
4848
} else {
@@ -90,7 +90,7 @@ var minDistanceAux = function (word1, word2, iter1, iter2) {
9090
);
9191
};
9292

93-
var min = function (a, b, c) {
93+
min = function (a, b, c) {
9494
if (a < b) return a < c ? a : c;
9595

9696
return b < c ? b : c;

LeetcodeProblems/Algorithms/Escape_The_Ghosts.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ The number of ghosts will not exceed 100.
4444
*/
4545
var escapeGhosts = function (ghosts, target) {
4646
var distancePacman = getDistance([0, 0], target);
47-
for (ghost in ghosts) {
47+
for (var ghost in ghosts) {
4848
const distanceGhost = getDistance(ghosts[ghost], target);
4949
if (distancePacman > distanceGhost) return false;
5050
}

LeetcodeProblems/Algorithms/Generate_Parenthesis.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ var generateParenthesisApproach1 = function (n) {
2020
if (n === 0) return [];
2121

2222
var str = "(".repeat(n);
23-
sol = [];
23+
var sol = [];
2424

2525
genParAux(str, 0, 0, sol);
2626
return sol;
@@ -93,4 +93,5 @@ var insertAt = function (str, position, value) {
9393
return str.slice(0, position) + value + str.slice(position);
9494
};
9595

96+
module.exports.generateParenthesisApproach1 = generateParenthesisApproach1;
9697
module.exports.generateParenthesisApproach2 = generateParenthesisApproach2;

LeetcodeProblems/Algorithms/Group_Anagrams.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ var groupAnagrams = function (strs) {
3333
}
3434
}
3535

36-
for (key in hashMap) ret.push(hashMap[key]);
36+
for (var key in hashMap) ret.push(hashMap[key]);
3737

3838
return ret;
3939
};

LeetcodeProblems/Algorithms/Implement_stack_using_queues.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,15 @@ class MyStack {
4242
if (this.q1.length === 0 && this.q2.length === 0) return null;
4343

4444
if (this.q1.length > 0) {
45+
var elem;
4546
while (this.q1.length > 1) {
46-
var elem = this.q1.shift();
47+
elem = this.q1.shift();
4748
this.q2.push(elem);
4849
}
4950
return this.q1.shift();
5051
} else {
5152
while (this.q2.length > 1) {
52-
var elem = this.q2.shift();
53+
elem = this.q2.shift();
5354
this.q1.push(elem);
5455
}
5556
return this.q2.shift();
@@ -58,13 +59,13 @@ class MyStack {
5859

5960
top() {
6061
if (this.q1.length === 0 && this.q2.length === 0) return null;
61-
62+
var elem;
6263
if (this.q1.length > 0) {
63-
var elem = this.pop();
64+
elem = this.pop();
6465
this.q2.push(elem);
6566
return elem;
6667
} else {
67-
var elem = this.pop();
68+
elem = this.pop();
6869
this.q1.push(elem);
6970
return elem;
7071
}

LeetcodeProblems/Algorithms/Kth_Largest_Element_in_an_Array.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ var findKthLargest = function (nums, k) {
2626
heapify(nums, nums.length, i);
2727
}
2828

29-
for (var i = nums.length - 1; i >= nums.length - k - 1 && i >= 0; i--) {
29+
for (i = nums.length - 1; i >= nums.length - k - 1 && i >= 0; i--) {
3030
swap(nums, 0, i);
3131
heapify(nums, i, 0);
3232
}

LeetcodeProblems/Algorithms/Linked_List_Cycle_II.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ Follow up:
1010
Can you solve it without using extra space?
1111
*/
1212

13-
var ListNode = require("../../UtilsClasses/ListNode").ListNode;
14-
1513
// Optimal solution
1614
/**
1715
* @param {ListNode} head
@@ -57,3 +55,4 @@ var detectCycle2 = function (head) {
5755
};
5856

5957
module.exports.detectCycle = detectCycle;
58+
module.exports.detectCycle2 = detectCycle2;

LeetcodeProblems/Algorithms/Longest_Consecutive_Sequence.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ var longestConsecutive = function (nums) {
2424

2525
var cons = 1;
2626
var currentCons = 1;
27-
for (var i = 0; i < nums.length; i++) {
27+
for (i = 0; i < nums.length; i++) {
2828
var number = nums[i];
2929
if (setNums.has(number)) {
3030
setNums.delete(number);

LeetcodeProblems/Algorithms/Longest_Palindromic_Substring.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,13 @@ var longestPalindrome = function (str) {
2525
var maxPal = 1;
2626
var posPalStart = 0;
2727
var currentPalStart = 0;
28+
var iter = 1;
2829

2930
for (var i = 1; i < str.length; i++) {
3031
if (str.charAt(i - 1) == str.charAt(i)) {
3132
currentPalStart = i - 1;
3233
var currentPal = 2;
33-
var iter = 1;
34+
iter = 1;
3435
while (
3536
i - iter - 1 >= 0 &&
3637
i + iter < str.length &&
@@ -47,10 +48,10 @@ var longestPalindrome = function (str) {
4748
}
4849
}
4950

50-
for (var i = 1; i < str.length - 1; i++) {
51+
for (i = 1; i < str.length - 1; i++) {
5152
if (str.charAt(i - 1) == str.charAt(i + 1)) {
5253
currentPal = 1;
53-
var iter = 1;
54+
iter = 1;
5455
while (
5556
i - iter >= 0 &&
5657
i + iter < str.length &&

LeetcodeProblems/Algorithms/Lowest_Common_Ancestor_of_a_Binary_Tree.js

-2
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ All of the nodes' values will be unique.
3434
p and q are different and both values will exist in the binary tree.
3535
*/
3636

37-
var TreeNode = require("../../UtilsClasses/TreeNode").TreeNode;
38-
3937
// Solution 1
4038
var lowestCommonAncestor = function (root, p, q) {
4139
if (root === null) return root;

LeetcodeProblems/Algorithms/Same_Tree.js

+2
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,5 @@ var isSameTree = function (p, q) {
5353

5454
return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
5555
};
56+
57+
module.exports.isSameTree = isSameTree;

LeetcodeProblems/Algorithms/Set_Matrix_Zeroes.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,10 @@ var setZeroes = function (matrix) {
8080
}
8181
}
8282

83-
for (var i = 0; i < matrix.length; i++)
83+
for (i = 0; i < matrix.length; i++)
8484
if (matrix[i][pivotCol] === 0 && i !== pivotRow) fillRow(matrix, i);
8585

86-
for (var i = 0; i < matrix[0].length; i++)
86+
for (i = 0; i < matrix[0].length; i++)
8787
if (matrix[pivotRow][i] === 0 && i !== pivotCol) fillCol(matrix, i);
8888

8989
fillCol(matrix, pivotCol);

LeetcodeProblems/Algorithms/Simplify_Path.js

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ var simplifyPath = function (path) {
4545
}
4646
}
4747
var ret = "";
48+
var elem;
4849

4950
while (queue.length > 0) {
5051
elem = queue.shift();

LeetcodeProblems/Algorithms/Spiral_Matrix.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,10 @@ var printRect = function (matrix, i, rowLength, colLength, retArray) {
5757
if (firstRow === lastRow || firstCol === lastCol) {
5858
return;
5959
}
60-
for (var col = lastCol - 1; col >= firstCol; col--) {
60+
for (col = lastCol - 1; col >= firstCol; col--) {
6161
retArray.push(matrix[lastRow][col]);
6262
}
63-
for (var row = lastRow - 1; row > firstRow; row--) {
63+
for (row = lastRow - 1; row > firstRow; row--) {
6464
retArray.push(matrix[row][firstCol]);
6565
}
6666
};

LeetcodeProblems/Algorithms/Subarray_Sum_Equals_K.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ var subarraySum2 = function (nums, k) {
4545
}
4646

4747
var ret = 0;
48-
for (var i = 0; i < sums.length - 1; i++) {
48+
for (i = 0; i < sums.length - 1; i++) {
4949
for (var j = i + 1; j < sums.length; j++) {
5050
if (sums[j] - sums[i] === k) ret++;
5151
}
@@ -55,3 +55,4 @@ var subarraySum2 = function (nums, k) {
5555
};
5656

5757
module.exports.subarraySum = subarraySum;
58+
module.exports.subarraySum2 = subarraySum2;

LeetcodeProblems/Algorithms/Subsets.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@ Output:
2323
*/
2424

2525
var subsets = function (nums) {
26-
var ret = [];
27-
28-
subsetByPosition = function (nums, position, current) {
26+
var subsetByPosition = function (nums, position, current) {
2927
if (position == nums.length) {
3028
return [current];
3129
}

LeetcodeProblems/Algorithms/Symmetric_Tree.js

+2
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,5 @@ var isSymetricAux = function (root1, root2) {
5050
isSymetricAux(root1.right, root2.left)
5151
);
5252
};
53+
54+
module.exports.isSymmetric = isSymmetric;

LeetcodeProblems/Algorithms/Unique_Paths.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,5 +98,5 @@ var uniquePaths3 = function (m, n) {
9898
};
9999

100100
module.exports.uniquePaths1 = uniquePaths1;
101-
module.exports.uniquePaths2 = uniquePaths1;
101+
module.exports.uniquePaths2 = uniquePaths2;
102102
module.exports.uniquePaths3 = uniquePaths3;

LeetcodeProblems/Algorithms/merge_k_sorted_lists.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,12 @@ Input:
1515
Output: 1->1->2->3->4->4->5->6
1616
*/
1717

18-
const ListNodeTestHelper = require("../../utilsClasses/ListNodeTestHelper");
19-
20-
var ListNode = require("../../utilsClasses/ListNode").ListNode;
21-
2218
var mergeKLists = function (lists) {
2319
if (lists.length === 0) return null;
2420

2521
var queue = [];
22+
var list1;
23+
var list2;
2624
for (var i = 0; i < lists.length; i++) queue.push(lists[i]);
2725

2826
while (queue.length > 1) {

0 commit comments

Comments
 (0)