Skip to content

Commit bcd7b5c

Browse files
Run Prettier Batch 5
1 parent 53600e1 commit bcd7b5c

10 files changed

+186
-201
lines changed

LeetcodeProblems/Algorithms/Lowest_Common_Ancestor_of_a_Binary_Tree.js

+26-32
Original file line numberDiff line numberDiff line change
@@ -34,64 +34,58 @@ 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-
38-
var TreeNode = require('../../UtilsClasses/TreeNode').TreeNode;
37+
var TreeNode = require("../../UtilsClasses/TreeNode").TreeNode;
3938

4039
// Solution 1
41-
var lowestCommonAncestor = function(root, p, q) {
42-
if(root === null)
43-
return root;
44-
if(p.val === root.val)
45-
return p;
46-
if(q.val === root.val)
47-
return q;
40+
var lowestCommonAncestor = function (root, p, q) {
41+
if (root === null) return root;
42+
if (p.val === root.val) return p;
43+
if (q.val === root.val) return q;
4844

4945
const left = lowestCommonAncestor(root.left, p, q);
5046
const right = lowestCommonAncestor(root.right, p, q);
51-
if(left !== null && right !== null)
52-
return root;
47+
if (left !== null && right !== null) return root;
5348
return left !== null ? left : right;
5449
};
5550

5651
// Solution 2
57-
var lowestCommonAncestor2 = function(root, p, q) {
52+
var lowestCommonAncestor2 = function (root, p, q) {
5853
var pathToP = pathTo(root, p.val);
5954
var pathToQ = pathTo(root, q.val);
6055

61-
if(pathToP.length === 0 || pathToQ === 0)
62-
return null;
56+
if (pathToP.length === 0 || pathToQ === 0) return null;
6357

6458
var iter = 0;
65-
while(iter < pathToP.length - 1 && iter < pathToQ.length - 1 && pathToP[iter + 1] === pathToQ[iter + 1]) {
66-
if(root.left !== null && root.left.val === pathToP[iter + 1]) {
67-
root = root.left;
59+
while (
60+
iter < pathToP.length - 1 &&
61+
iter < pathToQ.length - 1 &&
62+
pathToP[iter + 1] === pathToQ[iter + 1]
63+
) {
64+
if (root.left !== null && root.left.val === pathToP[iter + 1]) {
65+
root = root.left;
6866
} else {
69-
root = root.right;
67+
root = root.right;
7068
}
7169
iter++;
7270
}
73-
71+
7472
return root;
7573
};
7674

77-
var pathTo = function(root, value) {
78-
if(root === null)
79-
return [];
80-
75+
var pathTo = function (root, value) {
76+
if (root === null) return [];
77+
8178
var list = [root.val];
82-
if(root.val === value)
83-
return list;
79+
if (root.val === value) return list;
8480

8581
const left = pathTo(root.left, value);
86-
if (left.length > 0)
87-
return list.concat(left);
82+
if (left.length > 0) return list.concat(left);
83+
84+
const right = pathTo(root.right, value);
85+
if (right.length > 0) return list.concat(right);
8886

89-
const right = pathTo(root.right, value);
90-
if(right.length > 0)
91-
return list.concat(right);
92-
9387
return [];
94-
}
88+
};
9589

9690
module.exports.lowestCommonAncestor = lowestCommonAncestor;
9791
module.exports.lowestCommonAncestor2 = lowestCommonAncestor2;

LeetcodeProblems/Algorithms/Spiral_Matrix.js

+18-20
Original file line numberDiff line numberDiff line change
@@ -24,47 +24,45 @@ Input:
2424
Output: [1,2,3,4,8,12,11,10,9,5,6,7]
2525
*/
2626

27-
2827
/**
2928
* @param {number[][]} matrix
3029
* @return {number[]}
3130
*/
3231

33-
var spiralOrder = function(matrix) {
34-
if(matrix.length === 0)
35-
return [];
36-
37-
var retArray = [];
38-
const rowLength = matrix.length;
39-
const colLength = matrix[0].length;
40-
const countRectangles = Math.ceil(Math.min(colLength, rowLength)/2)
41-
for(var i = 0; i < countRectangles; i++)
42-
printRect(matrix, i, rowLength, colLength, retArray);
32+
var spiralOrder = function (matrix) {
33+
if (matrix.length === 0) return [];
34+
35+
var retArray = [];
36+
const rowLength = matrix.length;
37+
const colLength = matrix[0].length;
38+
const countRectangles = Math.ceil(Math.min(colLength, rowLength) / 2);
39+
for (var i = 0; i < countRectangles; i++)
40+
printRect(matrix, i, rowLength, colLength, retArray);
4341

44-
return retArray;
42+
return retArray;
4543
};
4644

47-
var printRect = function(matrix, i, rowLength, colLength, retArray) {
45+
var printRect = function (matrix, i, rowLength, colLength, retArray) {
4846
const firstRow = i;
4947
const firstCol = i;
5048
const lastRow = rowLength - i - 1;
5149
const lastCol = colLength - i - 1;
52-
53-
for(var col = firstCol; col <= lastCol; col++) {
50+
51+
for (var col = firstCol; col <= lastCol; col++) {
5452
retArray.push(matrix[firstRow][col]);
5553
}
56-
for(var row = firstRow + 1; row <= lastRow; row++) {
54+
for (var row = firstRow + 1; row <= lastRow; row++) {
5755
retArray.push(matrix[row][lastCol]);
5856
}
59-
if(firstRow === lastRow || firstCol === lastCol) {
57+
if (firstRow === lastRow || firstCol === lastCol) {
6058
return;
6159
}
62-
for(var col = lastCol - 1; col >= firstCol; col--) {
60+
for (var col = lastCol - 1; col >= firstCol; col--) {
6361
retArray.push(matrix[lastRow][col]);
6462
}
65-
for(var row = lastRow - 1; row > firstRow; row--) {
63+
for (var row = lastRow - 1; row > firstRow; row--) {
6664
retArray.push(matrix[row][firstCol]);
6765
}
68-
}
66+
};
6967

7068
module.exports.spiralOrder = spiralOrder;

LeetcodeProblems/Algorithms/Sum_Of_Square_Numbers.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,17 @@ Output: False
1818
* @param {number} c
1919
* @return {boolean}
2020
*/
21-
var judgeSquareSum = function(c) {
21+
var judgeSquareSum = function (c) {
2222
var iter = 0;
2323
var set = new Set();
24-
while(iter ** 2 <= c) {
24+
while (iter ** 2 <= c) {
2525
var square = iter * iter;
26-
if(square * 2 === c || set.has(c - square))
27-
return true;
26+
if (square * 2 === c || set.has(c - square)) return true;
2827

2928
set.add(square);
3029
iter++;
3130
}
32-
31+
3332
return false;
3433
};
3534

LeetcodeProblems/Algorithms/Swap_Nodes_In_Pairs.js

+16-17
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,24 @@ You may not modify the values in the list's nodes, only nodes itself may be chan
2424
* @param {ListNode} head
2525
* @return {ListNode}
2626
*/
27-
var swapPairs = function(head) {
28-
if(head === null || head.next === null)
29-
return head
30-
var previous = null;
31-
var current = head;
32-
var following = (head.next != null) ? head.next.next : null;
27+
var swapPairs = function (head) {
28+
if (head === null || head.next === null) return head;
29+
var previous = null;
30+
var current = head;
31+
var following = head.next != null ? head.next.next : null;
3332
head = head.next;
34-
35-
while(current !== null && current.next !== null) {
36-
var next = current.next;
37-
next.next = current;
38-
if(previous != null)
39-
previous.next = next;
40-
current.next = following;
41-
previous = current;
42-
current = following;
43-
following = (current !== null && current.next != null) ? current.next.next : null;
33+
34+
while (current !== null && current.next !== null) {
35+
var next = current.next;
36+
next.next = current;
37+
if (previous != null) previous.next = next;
38+
current.next = following;
39+
previous = current;
40+
current = following;
41+
following =
42+
current !== null && current.next != null ? current.next.next : null;
4443
}
45-
44+
4645
return head;
4746
};
4847

LeetcodeProblems/Algorithms/Symmetric_Tree.js

+12-11
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,20 @@ Bonus points if you could solve it both recursively and iteratively.
3333
* @param {TreeNode} root
3434
* @return {boolean}
3535
*/
36-
var isSymmetric = function(root) {
37-
if(root === null)
38-
return true;
36+
var isSymmetric = function (root) {
37+
if (root === null) return true;
3938
return isSymetricAux(root.left, root.right);
4039
};
4140

42-
var isSymetricAux = function(root1, root2) {
43-
if(root1 === null)
44-
return root2 === null;
45-
46-
if(root2 === null || root1.val !== root2.val) {
41+
var isSymetricAux = function (root1, root2) {
42+
if (root1 === null) return root2 === null;
43+
44+
if (root2 === null || root1.val !== root2.val) {
4745
return false;
4846
}
49-
50-
return isSymetricAux(root1.left, root2.right) && isSymetricAux(root1.right, root2.left);
51-
}
47+
48+
return (
49+
isSymetricAux(root1.left, root2.right) &&
50+
isSymetricAux(root1.right, root2.left)
51+
);
52+
};

LeetcodeProblems/Algorithms/Tic_Tac_Toe.js

+21-18
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
21
class TicTacToe {
32
constructor() {
43
this.matrix = [];
5-
for(var i = 0; i < 3; i++) {
4+
for (var i = 0; i < 3; i++) {
65
this.matrix[i] = [];
7-
for(var j = 0; j < 3; j++) {
6+
for (var j = 0; j < 3; j++) {
87
this.matrix[i][j] = "-";
98
}
109
}
11-
};
10+
}
1211

1312
validToken(token) {
1413
if (token == "X" || token == "0" || token == "-") {
@@ -20,41 +19,45 @@ class TicTacToe {
2019

2120
addToken(x, y, token) {
2221
// Check valid positions
23-
if(this.validToken(token)) {
22+
if (this.validToken(token)) {
2423
this.matrix[x][y] = token;
2524
}
26-
};
25+
}
2726

2827
printBoard() {
2928
console.log("-------");
30-
for(var row = 0; row < 3; row ++) {
31-
console.log(this.matrix[row][0] + "|"
32-
+ this.matrix[row][1] + "|"
33-
+ this.matrix[row][2]); // Check new line;
29+
for (var row = 0; row < 3; row++) {
30+
console.log(
31+
this.matrix[row][0] +
32+
"|" +
33+
this.matrix[row][1] +
34+
"|" +
35+
this.matrix[row][2]
36+
); // Check new line;
3437
}
3538
console.log("------- \n");
3639
}
3740

3841
isBoardFull() {
39-
for(var row = 0; row < 3; row ++) {
40-
for(var col = 0; col < 3; col ++) {
41-
if(this.matrix[row][col] === "-") {
42+
for (var row = 0; row < 3; row++) {
43+
for (var col = 0; col < 3; col++) {
44+
if (this.matrix[row][col] === "-") {
4245
console.log("Is not full");
4346
return false;
4447
}
45-
}
48+
}
4649
}
4750
console.log("Is full");
4851
return true;
4952
}
5053

5154
makeMove(str) {
52-
if(this.isBoardFull()) {
55+
if (this.isBoardFull()) {
5356
throw "Error Board is Full";
5457
}
55-
for(var row = 0; row < 3; row ++) {
56-
for(var col = 0; col < 3; col ++) {
57-
if(this.matrix[row][col] === "-") {
58+
for (var row = 0; row < 3; row++) {
59+
for (var col = 0; col < 3; col++) {
60+
if (this.matrix[row][col] === "-") {
5861
this.addToken(row, col, str);
5962
return true;
6063
}

0 commit comments

Comments
 (0)