Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 7dd7399

Browse files
Reformat problems
1 parent e1ea6d2 commit 7dd7399

23 files changed

+319
-195
lines changed

LeetcodeProblems/Add_Two_Numbers.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Explanation: 342 + 465 = 807.
2222
* }
2323
*/
2424

25-
var ListNode = require('../utilsClasses/ListNode').ListNode;
25+
var ListNode = require('../UtilsClasses/ListNode').ListNode;
2626

2727
/**
2828
* @param {ListNode} l1

LeetcodeProblems/Construct_Binary_Tree_from_Preorder_and_Inorder_Traversal.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Return the following binary tree:
2828
* }
2929
*/
3030

31-
var TreeNode = require('../utilsClasses/TreeNode').TreeNode;
31+
var TreeNode = require('../UtilsClasses/TreeNode').TreeNode;
3232

3333
/**
3434
* @param {number[]} preorder

LeetcodeProblems/Flood_Fill.js

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ An image is represented by a 2-D array of integers, each integer representing th
66
77
Given a coordinate (sr, sc) representing the starting pixel (row and column) of the flood fill, and a pixel value newColor, "flood fill" the image.
88
9-
To perform a "flood fill", consider the starting pixel, plus any pixels connected 4-directionally to the starting pixel of the same color as the starting pixel, plus any pixels connected 4-directionally to those pixels (also with the same color as the starting pixel), and so on. Replace the color of all of the aforementioned pixels with the newColor.
9+
To perform a "flood fill", consider the starting pixel, plus any pixels connected 4-directionally to the starting pixel of the same color as the starting pixel,
10+
plus any pixels connected 4-directionally to those pixels (also with the same color as the starting pixel), and so on.
11+
Replace the color of all of the aforementioned pixels with the newColor.
1012
1113
At the end, return the modified image.
1214
@@ -25,34 +27,31 @@ Note:
2527
The length of image and image[0] will be in the range [1, 50].
2628
The given starting pixel will satisfy 0 <= sr < image.length and 0 <= sc < image[0].length.
2729
The value of each color in image[i][j] and newColor will be an integer in [0, 65535].
28-
2930
*/
3031

3132
var floodFill = function(image, sr, sc, newColor) {
3233
var oldColor = image[sr][sc];
3334

34-
if(newColor == oldColor) {
35-
return image;
36-
}
35+
if(newColor == oldColor)
36+
return image;
3737

3838
image[sr][sc] = newColor;
3939

40-
if(sr > 0 && image[sr - 1][sc] == oldColor) {
41-
floodFill(image, sr - 1, sc, newColor); //Left
42-
}
43-
if(sc > 0 && image[sr][sc - 1] == oldColor) {
44-
floodFill(image, sr, sc - 1, newColor); //Up
45-
}
46-
if(sr < image.length - 1 && image[sr + 1][sc] == oldColor) {
47-
floodFill(image, sr + 1, sc, newColor); //Down
48-
}
49-
if(sc < image[0].length - 1 && image[sr][sc + 1] == oldColor) {
50-
floodFill(image, sr, sc + 1, newColor); // Right
51-
}
40+
if(sr > 0 && image[sr - 1][sc] == oldColor)
41+
floodFill(image, sr - 1, sc, newColor); //Left
42+
43+
if(sc > 0 && image[sr][sc - 1] == oldColor)
44+
floodFill(image, sr, sc - 1, newColor); //Up
45+
46+
if(sr < image.length - 1 && image[sr + 1][sc] == oldColor)
47+
floodFill(image, sr + 1, sc, newColor); //Down
48+
49+
if(sc < image[0].length - 1 && image[sr][sc + 1] == oldColor)
50+
floodFill(image, sr, sc + 1, newColor); // Right
51+
5252
return image;
5353
};
5454

55-
5655
function main() {
5756
console.log(floodFill([[1,1,1],[1,1,0],[1,0,1]], 1, 1, 2))
5857
}

LeetcodeProblems/Generate_Parentheses.js renamed to LeetcodeProblems/Generate_Parenthesis.js

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ For example, given n = 3, a solution set is:
1616
*/
1717

1818
// ************************************************ Approach1 ************************************************
19-
var generateParenthesesApproach1 = function(n) {
20-
if(n === 0) { return [] };
19+
var generateParenthesisApproach1 = function(n) {
20+
if(n === 0)
21+
return [];
2122

2223
var str = "(".repeat(n);
2324
sol = [];
@@ -26,12 +27,6 @@ var generateParenthesesApproach1 = function(n) {
2627
return sol;
2728
};
2829

29-
/*
30-
@param {string} str contains the string generated.
31-
@param {number} position Current position of the string where new parenthesis would be added.
32-
@param {string} leftParenthesis Amount for parenthesis left to be added.
33-
@param {[string]} sol array that contains the solution found so far.
34-
*/
3530
var genParAux = function(str, position, leftParentheses, sol) {
3631
if(position === str.length) {
3732
var ret = str + ")".repeat(leftParentheses);
@@ -40,7 +35,8 @@ var genParAux = function(str, position, leftParentheses, sol) {
4035
}
4136

4237
genParAux(str, position + 1, leftParentheses + 1, sol); // Don't insert anything
43-
if(leftParentheses === 0) { return; }
38+
if(leftParentheses === 0)
39+
return;
4440

4541
for(var i = 1; i <= leftParentheses; i++) {
4642
var parString = ")".repeat(i);
@@ -50,8 +46,9 @@ var genParAux = function(str, position, leftParentheses, sol) {
5046
}
5147

5248
// ************************************************ Approach2 ************************************************
53-
var generateParenthesesApproach2 = function(n) {
54-
if(n === 0) { return [] };
49+
var generateParenthesisApproach2 = function(n) {
50+
if(n === 0)
51+
return [];
5552

5653
var sol = [];
5754
genParAuxApproach2("", 0, 0, 0, n * 2, sol)
@@ -60,16 +57,17 @@ var generateParenthesesApproach2 = function(n) {
6057

6158
var genParAuxApproach2 = function(str, leftPar, rightPar, index, totalCharCount, sol) {
6259
if(index === totalCharCount) {
63-
if(rightPar === leftPar) {
60+
if(rightPar === leftPar)
6461
sol.push(str);
65-
}
62+
6663
return;
6764
}
6865

6966
var strLeft = insertAt(str, index, "(");
7067
genParAuxApproach2(strLeft, leftPar + 1, rightPar, index + 1, totalCharCount, sol);
7168

72-
if(rightPar === leftPar) { return; }
69+
if(rightPar === leftPar)
70+
return;
7371

7472
var strRight = insertAt(str, index, ")");
7573
genParAuxApproach2(strRight, leftPar, rightPar + 1, index + 1, totalCharCount, sol);
@@ -82,14 +80,14 @@ var insertAt = function(str, position, value) {
8280
function main() {
8381
console.log("Approach 1");
8482
[0, 1, 2, 3].forEach(function(elem) {
85-
console.log(`${elem}: ${generateParenthesesApproach2(elem)}`);
83+
console.log(`${elem}: ${generateParenthesisApproach2(elem)}`);
8684
})
8785

8886
console.log("-------------");
8987

9088
console.log("Approach 2");
9189
[0, 1, 2, 3].forEach(function(elem) {
92-
console.log(`${elem}: ${generateParenthesesApproach2(elem)}`);
90+
console.log(`${elem}: ${generateParenthesisApproach2(elem)}`);
9391
})
9492
}
9593

LeetcodeProblems/Group_Anagrams.js

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,27 +23,26 @@ var groupAnagrams = function(strs) {
2323
var ret = [];
2424
var hashMap = {};
2525
for(var i = 0; i < strs.length; i++) {
26-
const elem = strs[i];
27-
const elemSorted = sortString(strs[i]);
28-
29-
if(hashMap[elemSorted]) {
30-
hashMap[elemSorted].push(elem);
31-
} else {
32-
hashMap[elemSorted] = [elem];
33-
}
26+
const elem = strs[i];
27+
const elemSorted = sortString(strs[i]);
28+
29+
if(hashMap[elemSorted]) {
30+
hashMap[elemSorted].push(elem);
31+
} else {
32+
hashMap[elemSorted] = [elem];
33+
}
3434
}
3535

36-
for(key in hashMap) {
36+
for(key in hashMap)
3737
ret.push(hashMap[key]);
38-
}
38+
3939

4040
return ret;
4141
};
4242

4343
var sortString = function(str) {
44-
if(str.length === 0) {
45-
return str;
46-
}
44+
if(str.length === 0)
45+
return str;
4746

4847
return str.split("").sort().join("");
4948
}

LeetcodeProblems/Linked_List_Cycle_II.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Follow up:
1010
Can you solve it without using extra space?
1111
*/
1212

13-
var ListNode = require('../utilsClasses/ListNode').ListNode;
13+
var ListNode = require('../UtilsClasses/ListNode').ListNode;
1414

1515
// Optimal solution
1616
/**
@@ -42,19 +42,19 @@ var detectCycle = function(head) {
4242

4343
// Naiver solution using a Set
4444
var detectCycle2 = function(head) {
45-
if(head === null || head.next === null) {
46-
return null;
47-
}
48-
var setNodes = new Set();
49-
var iter = head;
50-
while(iter !== null) {
51-
if(setNodes.has(iter)) {
52-
return iter;
53-
}
54-
setNodes.add(iter);
55-
iter = iter.next
56-
}
45+
if(head === null || head.next === null) {
5746
return null;
47+
}
48+
var setNodes = new Set();
49+
var iter = head;
50+
while(iter !== null) {
51+
if(setNodes.has(iter)) {
52+
return iter;
53+
}
54+
setNodes.add(iter);
55+
iter = iter.next
56+
}
57+
return null;
5858
};
5959

6060
var main = function() {

LeetcodeProblems/Longest_Consecutive_Sequence.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ var longestConsecutive = function(nums) {
3030
var number = nums[i];
3131
if(setNums.has(number)) {
3232
setNums.delete(number);
33+
3334
var prevNum = number - 1;
3435
while(setNums.has(prevNum)){
3536
currentCons++;

LeetcodeProblems/Longest_Palindromic_Substring.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ var longestPalindrome = function(str) {
3232
currentPalStart = i - 1;
3333
var currentPal = 2;
3434
var iter = 1;
35-
while(i - iter - 1 >= 0 && i + iter < str.length &&
36-
str.charAt(i - iter - 1) == str.charAt(i + iter)) {
35+
while(i - iter - 1 >= 0 && i + iter < str.length && str.charAt(i - iter - 1) == str.charAt(i + iter)) {
3736
currentPalStart = i - iter - 1;
3837
iter++;
3938
currentPal += 2;

LeetcodeProblems/Lowest_Common_Ancestor_of_a_Binary_Tree.js

Lines changed: 69 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/
44
55
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.
66
7-
According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes p and q as the lowest node in T that has both p and q as descendants (where we allow a node to be a descendant of itself).”
7+
According to the definition of LCA on Wikipedia:
8+
“The lowest common ancestor is defined between two nodes p and q as the lowest node in T that has both p and q as descendants
9+
(where we allow a node to be a descendant of itself).”
810
911
Given the following binary tree: root = [3,5,1,6,2,0,8,null,null,7,4]
1012
@@ -32,6 +34,8 @@ All of the nodes' values will be unique.
3234
p and q are different and both values will exist in the binary tree.
3335
*/
3436

37+
var TreeNode = require('../UtilsClasses/TreeNode').TreeNode;
38+
3539
// Solution 1
3640
var lowestCommonAncestor = function(root, p, q) {
3741
if(root === null)
@@ -48,40 +52,78 @@ var lowestCommonAncestor = function(root, p, q) {
4852
return left !== null ? left : right;
4953
};
5054

51-
5255
// Solution 2
53-
var lowestCommonAncestor1 = function(root, p, q) {
54-
const pathToP = pathTo(root, p.val);
55-
const pathToQ = pathTo(root, q.val);
56-
57-
if(pathToP === null || pathToQ === null)
58-
return null;
59-
60-
if(pathToP.length === 1)
61-
return pathToP[0];
62-
if(pathToQ.length === 1)
63-
return pathToQ[0];
56+
var lowestCommonAncestor2 = function(root, p, q) {
57+
var pathToP = pathTo(root, p.val);
58+
var pathToQ = pathTo(root, q.val);
59+
60+
if(pathToP.length === 0 || pathToQ === 0)
61+
return null;
6462

6563
var iter = 0;
66-
while(pathToP[iter + 1] === pathToQ[iter + 1])
67-
iter++
64+
while(iter < pathToP.length - 1 && iter < pathToQ.length - 1 && pathToP[iter + 1] === pathToQ[iter + 1]) {
65+
if(root.left !== null && root.left.val === pathToP[iter + 1]) {
66+
root = root.left;
67+
} else {
68+
root = root.right;
69+
}
70+
iter++;
71+
}
6872

69-
return pathToP[iter];
73+
return root;
7074
};
7175

72-
var pathTo = function(root, a) {
76+
var pathTo = function(root, value) {
7377
if(root === null)
74-
return null;
75-
if(root.val === a)
76-
return [root.val];
78+
return [];
79+
80+
var list = [root.val];
81+
if(root.val === value)
82+
return list;
7783

78-
const left = pathTo(root.left, a);
79-
if (left !== null)
80-
return [root.val].concat(left);
84+
const left = pathTo(root.left, value);
85+
if (left.length > 0)
86+
return list.concat(left);
8187

82-
const right = pathTo(root.right, a);
83-
if(right !== null)
84-
return [root.val].concat(right);
88+
const right = pathTo(root.right, value);
89+
if(right.length > 0)
90+
return list.concat(right);
8591

86-
return null;
92+
return [];
8793
}
94+
95+
96+
var main = function() {
97+
var root = new TreeNode(3);
98+
99+
var right = new TreeNode(1);
100+
right.left = new TreeNode(0);
101+
right.right = new TreeNode(8);
102+
root.right = right;
103+
104+
var left = new TreeNode(5);
105+
left.left = new TreeNode(6);
106+
107+
var tempRight = new TreeNode(2);
108+
tempRight.left = new TreeNode(7);
109+
tempRight.right = new TreeNode(4);
110+
left.right = tempRight;
111+
112+
root.left = left;
113+
114+
// _______3______
115+
// / \
116+
// ___5__ ___1__
117+
// / \ / \
118+
// 6 _2 0 8
119+
// / \
120+
// 7 4
121+
122+
console.log(lowestCommonAncestor(root, left, tempRight.right));
123+
console.log(lowestCommonAncestor(root, left, right));
124+
125+
console.log(lowestCommonAncestor2(root, left, tempRight.right));
126+
console.log(lowestCommonAncestor2(root, left, right));
127+
}
128+
129+
module.exports.main = main;

0 commit comments

Comments
 (0)