diff --git a/LeetcodeProblems/Algorithms/2Sum.js b/LeetcodeProblems/Algorithms/2Sum.js index 11e7606..3cf3682 100644 --- a/LeetcodeProblems/Algorithms/2Sum.js +++ b/LeetcodeProblems/Algorithms/2Sum.js @@ -32,7 +32,7 @@ var twoSum = function (nums, target) { let map = {}; for (let i = 0; i < nums.length; i++) { const sum = target - nums[i]; - if (map[parseInt(sum)] != 0) { + if (sum in map) { return [map[sum], i]; } else { map[nums[i]] = i; @@ -43,8 +43,8 @@ var twoSum = function (nums, target) { //Another method var twoSum2 = function (nums, target) { for (let i = 0; i < nums.length; i++) { - for (let j = i + 1; j < nums.length; i++) { - if (nums[1] + nums[j] === target) { + for (let j = i + 1; j < nums.length; j++) { + if (nums[i] + nums[j] === target) { return [i, j]; } } diff --git a/LeetcodeProblems/Algorithms/3SumClosest.js b/LeetcodeProblems/Algorithms/3SumClosest.js index 271821a..22f0734 100644 --- a/LeetcodeProblems/Algorithms/3SumClosest.js +++ b/LeetcodeProblems/Algorithms/3SumClosest.js @@ -30,31 +30,31 @@ Constraints: * @param {number} target * @return {number} */ - var threeSumClosest = function(nums, target) { +var threeSumClosest = function(nums, target) { let mid = 1; let right = nums.length - 1; let currentSum = nums[0] + nums[mid] + nums[right]; let closest = currentSum; - nums.sort(function(a,b) {return a - b}) + nums.sort(function(a,b) {return a - b;}); for(var left = 0 ; left < nums.length - 1; left++) { - mid = left + 1; - right = nums.length - 1; + mid = left + 1; + right = nums.length - 1; - while(mid < right) { - currentSum = nums[left] + nums[mid] + nums[right]; + while(mid < right) { + currentSum = nums[left] + nums[mid] + nums[right]; - if(Math.abs(target - currentSum) < Math.abs(target - closest)) { - closest = currentSum; - } + if(Math.abs(target - currentSum) < Math.abs(target - closest)) { + closest = currentSum; + } - if(currentSum > target) { - right--; - } else { - mid++; - } + if(currentSum > target) { + right--; + } else { + mid++; } + } } return closest; diff --git a/LeetcodeProblems/Algorithms/Container_With_Most_Water.js b/LeetcodeProblems/Algorithms/Container_With_Most_Water.js index 4b6098f..90b8373 100644 --- a/LeetcodeProblems/Algorithms/Container_With_Most_Water.js +++ b/LeetcodeProblems/Algorithms/Container_With_Most_Water.js @@ -24,18 +24,18 @@ Output: 1 * @param {number[]} height * @return {number} */ - var maxArea = function(height) { +var maxArea = function(height) { let left = 0; let right = height.length - 1; let maxArea = calculateArea(left, right, height); while(left < right) { - if(height[left] < height[right]) { - left++ - } else { - right--; - } - maxArea = Math.max(maxArea, calculateArea(left, right, height)) + if(height[left] < height[right]) { + left++; + } else { + right--; + } + maxArea = Math.max(maxArea, calculateArea(left, right, height)); } return maxArea; }; @@ -44,6 +44,6 @@ var calculateArea = function(x, y, height) { let minHeight = height[x] > height[y] ? height[y] : height[x]; let width = y -x; return (width * minHeight); -} +}; module.exports.maxArea = maxArea; \ No newline at end of file diff --git a/LeetcodeProblems/Algorithms/Find_Anagrams.js b/LeetcodeProblems/Algorithms/Find_Anagrams.js index 1d201b0..5624329 100644 --- a/LeetcodeProblems/Algorithms/Find_Anagrams.js +++ b/LeetcodeProblems/Algorithms/Find_Anagrams.js @@ -30,51 +30,51 @@ The substring with start index = 2 is "ab", which is an anagram of "ab". * @param {string} p * @return {number[]} */ - var findAnagrams = function(s, p) { - if(s.length < p.length) { return [] } +var findAnagrams = function(s, p) { + if(s.length < p.length) { return []; } let start = 0; let end = p.length - 1; let hashBuild = {}; let countLeft = p.length; - let anagrams = [] + let anagrams = []; for(let e = 0; e < p.length; e++) { - hashBuild[p[e]] = hashBuild[p[e]] !== undefined ? hashBuild[p[e]] + 1 : 1; + hashBuild[p[e]] = hashBuild[p[e]] !== undefined ? hashBuild[p[e]] + 1 : 1; } for(let i = start; i < end; i++) { - if(hashBuild[s[i]] !== undefined) { - hashBuild[s[i]] = hashBuild[s[i]] - 1; - if(hashBuild[s[i]] >= 0) { - countLeft--; - } + if(hashBuild[s[i]] !== undefined) { + hashBuild[s[i]] = hashBuild[s[i]] - 1; + if(hashBuild[s[i]] >= 0) { + countLeft--; } + } } while(end < s.length) { - // check left - if(hashBuild[s[end]] !== undefined) { - hashBuild[s[end]] = hashBuild[s[end]] - 1; - if(hashBuild[s[end]] >= 0) { - countLeft--; - } - if(countLeft == 0) { - anagrams.push(start); - } + // check left + if(hashBuild[s[end]] !== undefined) { + hashBuild[s[end]] = hashBuild[s[end]] - 1; + if(hashBuild[s[end]] >= 0) { + countLeft--; } + if(countLeft == 0) { + anagrams.push(start); + } + } - // check right - if(hashBuild[s[start]] !== undefined) { - hashBuild[s[start]] = hashBuild[s[start]] + 1; - if(hashBuild[s[start]] >= 1) { - countLeft++; - } + // check right + if(hashBuild[s[start]] !== undefined) { + hashBuild[s[start]] = hashBuild[s[start]] + 1; + if(hashBuild[s[start]] >= 1) { + countLeft++; } + } - // slide window - end++; - start++; + // slide window + end++; + start++; } return anagrams; diff --git a/LeetcodeProblems/Algorithms/Find_Subarrays_With_Equal_Sum.js b/LeetcodeProblems/Algorithms/Find_Subarrays_With_Equal_Sum.js index 3981e04..fa28d07 100644 --- a/LeetcodeProblems/Algorithms/Find_Subarrays_With_Equal_Sum.js +++ b/LeetcodeProblems/Algorithms/Find_Subarrays_With_Equal_Sum.js @@ -35,7 +35,7 @@ Note that even though the subarrays have the same content, the two subarrays are * @param {number[]} nums * @return {boolean} */ - var findSubarrays = function (nums) { +var findSubarrays = function (nums) { const sumsSeen = new Set(); for (let i = 0; i < nums.length - 1; i++) { diff --git a/LeetcodeProblems/Algorithms/Happy_Number.js b/LeetcodeProblems/Algorithms/Happy_Number.js index 4d6bf3b..df3e996 100644 --- a/LeetcodeProblems/Algorithms/Happy_Number.js +++ b/LeetcodeProblems/Algorithms/Happy_Number.js @@ -33,17 +33,17 @@ Output: false * @return {boolean} */ var isHappy = function(n) { - return checkHappyNumber(n); + return checkHappyNumber(n); }; function checkHappyNumber(n){ - strNumber = n.toString(); - splitNumber = strNumber.split(""); - if(splitNumber.length <= 1){ - return (n <= 1)? true:false; - } - const digit = splitNumber.reduce((a,b)=> parseInt(a) + Math.pow(parseInt(b),2),0); - return checkHappyNumber(digit) + let strNumber = n.toString(); + let splitNumber = strNumber.split(""); + if(splitNumber.length <= 1){ + return (n <= 1)? true:false; + } + const digit = splitNumber.reduce((a,b)=> parseInt(a) + Math.pow(parseInt(b),2),0); + return checkHappyNumber(digit); } module.exports.isHappy = isHappy; diff --git a/LeetcodeProblems/Algorithms/Longest_Common_Prefix.js b/LeetcodeProblems/Algorithms/Longest_Common_Prefix.js index a992179..2468131 100644 --- a/LeetcodeProblems/Algorithms/Longest_Common_Prefix.js +++ b/LeetcodeProblems/Algorithms/Longest_Common_Prefix.js @@ -21,13 +21,13 @@ Explanation: There is no common prefix among the input strings. * @param {string[]} strs * @return {string} */ - var longestCommonPrefix = function(strs) { +var longestCommonPrefix = function(strs) { if(strs.length === 0) return ""; return strs.reduce((result, curr)=>{ - let i = 0; - while(result[i] && curr[i] && result[i] === curr[i]) i++; - return result.slice(0, i); + let i = 0; + while(result[i] && curr[i] && result[i] === curr[i]) i++; + return result.slice(0, i); }); }; diff --git a/LeetcodeProblems/Algorithms/Longest_Substring.js b/LeetcodeProblems/Algorithms/Longest_Substring.js index a6430f1..cc6726c 100644 --- a/LeetcodeProblems/Algorithms/Longest_Substring.js +++ b/LeetcodeProblems/Algorithms/Longest_Substring.js @@ -32,8 +32,8 @@ s consists of English letters, digits, symbols and spaces. * @param {string} s * @return {number} */ - var lengthOfLongestSubstring = function(s) { - if(s.length == 0) { return 0 } +var lengthOfLongestSubstring = function(s) { + if(s.length == 0) { return 0; } var repeatedChars = new Set(); var maxLength = 1; @@ -45,15 +45,15 @@ s consists of English letters, digits, symbols and spaces. while(end + 1 < s.length && start < s.length) { if(repeatedChars.has(s.charAt(end + 1))) { if(repeatedChars.has(s.charAt(start))) { - currentMaxLength--; - repeatedChars.delete(s.charAt(start)) + currentMaxLength--; + repeatedChars.delete(s.charAt(start)); } - start++; + start++; } else { repeatedChars.add(s.charAt(end + 1)); currentMaxLength++; if(currentMaxLength > maxLength) { - maxLength = currentMaxLength; + maxLength = currentMaxLength; } end++; } diff --git a/LeetcodeProblems/Algorithms/Max_Consecutive_Ones_III.js b/LeetcodeProblems/Algorithms/Max_Consecutive_Ones_III.js index 7766668..5344c66 100644 --- a/LeetcodeProblems/Algorithms/Max_Consecutive_Ones_III.js +++ b/LeetcodeProblems/Algorithms/Max_Consecutive_Ones_III.js @@ -24,18 +24,18 @@ var longestOnes = function(nums, k) { let end = 0; let maxWindow = 0; while(start < nums.length && end < nums.length) { - if(k > 0 || nums[end] == 1) { - if(nums[end] == 0) { k--; } - maxWindow = Math.max(maxWindow, end - start + 1); - end++; - } else { // k = 0 and nums[end] == 0 - while(k == 0 && start < nums.length) { - if(nums[start] == 0) { - k++; - } - start++; - } + if(k > 0 || nums[end] == 1) { + if(nums[end] == 0) { k--; } + maxWindow = Math.max(maxWindow, end - start + 1); + end++; + } else { // k = 0 and nums[end] == 0 + while(k == 0 && start < nums.length) { + if(nums[start] == 0) { + k++; + } + start++; } + } } return maxWindow; diff --git a/LeetcodeProblems/Algorithms/Maximise_Hour_Glass_Sum.js b/LeetcodeProblems/Algorithms/Maximise_Hour_Glass_Sum.js index 124d4a2..55bfd3f 100644 --- a/LeetcodeProblems/Algorithms/Maximise_Hour_Glass_Sum.js +++ b/LeetcodeProblems/Algorithms/Maximise_Hour_Glass_Sum.js @@ -31,18 +31,20 @@ Explanation: There is only one hourglass in the matrix, with the sum: 1 + 2 + 3 * @param {number[][]} grid * @return {number} */ - var maxSum = function(grid) { +var maxSum = function(grid) { const m = grid.length; const n = grid[0].length; if(m<3 || n < 3) { - return 0; + return 0; } let max = 0; for(let i = 0; i a-b); let i = 0, j = nums.length - 1; let max = -Infinity; while (i < j) { - max = Math.max(max, nums[i++] + nums[j--]); + max = Math.max(max, nums[i++] + nums[j--]); } return max; }; diff --git a/LeetcodeProblems/Algorithms/Minimum_Add_To_Make_Parentheses_Valid.js b/LeetcodeProblems/Algorithms/Minimum_Add_To_Make_Parentheses_Valid.js index 9181d20..3f88dc1 100644 --- a/LeetcodeProblems/Algorithms/Minimum_Add_To_Make_Parentheses_Valid.js +++ b/LeetcodeProblems/Algorithms/Minimum_Add_To_Make_Parentheses_Valid.js @@ -33,15 +33,15 @@ var minAddToMakeValid = function(s) { var extraParClosing = 0; for(let i = 0; i < s.length; i++) { - if(s.charAt(i) == "(") { - opening++; - } else if(s.charAt(i) == ")") { - if(opening == 0) { - extraParClosing++; - } else { - opening--;; - } - } + if(s.charAt(i) == "(") { + opening++; + } else if(s.charAt(i) == ")") { + if(opening == 0) { + extraParClosing++; + } else { + opening--; + } + } } return extraParClosing + opening; }; @@ -52,18 +52,19 @@ var minAddToMakeValidUsingQueue = function(s) { var extraParClosing = 0; for(let i = 0; i < s.length; i++) { - if(s.charAt(i) == "(") { - queue.push(s.charAt(i)) - } else if(s.charAt(i) == ")") { - if(queue.length > 0) { - queue.pop(); - } else { - extraParClosing++; - } - } + if(s.charAt(i) == "(") { + queue.push(s.charAt(i)); + } else if(s.charAt(i) == ")") { + if(queue.length > 0) { + queue.pop(); + } else { + extraParClosing++; + } + } } return extraParClosing + queue.length; }; -module.exports.minAddToMakeValid = minAddToMakeValid; \ No newline at end of file +module.exports.minAddToMakeValid = minAddToMakeValid; +module.exports.minAddToMakeValidUsingQueue = minAddToMakeValidUsingQueue; \ No newline at end of file diff --git a/LeetcodeProblems/Algorithms/Minimum_Size_Subarray.js b/LeetcodeProblems/Algorithms/Minimum_Size_Subarray.js index dcaa489..481531e 100644 --- a/LeetcodeProblems/Algorithms/Minimum_Size_Subarray.js +++ b/LeetcodeProblems/Algorithms/Minimum_Size_Subarray.js @@ -24,8 +24,8 @@ Output: 0 * @param {number[]} nums * @return {number} */ - var minSubArrayLength = function(target, nums) { - if(nums.length == 0) { return 0 } +var minSubArrayLength = function(target, nums) { + if(nums.length == 0) { return 0; } let start = 0; let end = 0; @@ -34,20 +34,20 @@ Output: 0 let currentWindow = 1; while(start < nums.length && end < nums.length) { - currentWindow = (end + 1 - start) - if(currentSum >= target || (minWindow != 0 && currentWindow > minWindow) ) { - if(minWindow == 0 || minWindow > currentWindow ) { - minWindow = currentWindow; - if(minWindow == 1) { return 1 }; - } - currentSum -= nums[start]; - start++; - } else { - end++; - if(end < nums.length) { - currentSum += nums[end]; - } + currentWindow = (end + 1 - start); + if(currentSum >= target || (minWindow != 0 && currentWindow > minWindow) ) { + if(minWindow == 0 || minWindow > currentWindow ) { + minWindow = currentWindow; + if(minWindow == 1) { return 1; } } + currentSum -= nums[start]; + start++; + } else { + end++; + if(end < nums.length) { + currentSum += nums[end]; + } + } } return minWindow; diff --git a/LeetcodeProblems/Algorithms/Minimum_Window_Substring.js b/LeetcodeProblems/Algorithms/Minimum_Window_Substring.js index d3b5bd5..b58892f 100644 --- a/LeetcodeProblems/Algorithms/Minimum_Window_Substring.js +++ b/LeetcodeProblems/Algorithms/Minimum_Window_Substring.js @@ -73,59 +73,59 @@ var getHash = function (t) { // Solution 2 // Similar idea code slightly different; var buildHash = function(t) { - let hash = {}; - let occ = 0; - for(var i = 0; i < t.length; i++) { - occ = hash[t[i]] == undefined ? 0 : hash[t[i]]; - hash[t[i]] = occ + 1; - } - return hash; -} + let hash = {}; + let occ = 0; + for(var i = 0; i < t.length; i++) { + occ = hash[t[i]] == undefined ? 0 : hash[t[i]]; + hash[t[i]] = occ + 1; + } + return hash; +}; var minWindow2 = function(s, t) { - var hashT = buildHash(t); - var start = 0; - var end = 0; - var countLeft = t.length; - var minWindow = ""; - var minWindowLeft = -1; - var maxWindowRight = -1; - - while(start < s.length && end < s.length) { - if(countLeft > 0) { // window does not contain all elements - if(hashT[s[end]] !== undefined) { - hashT[s[end]] = hashT[s[end]] - 1; - if(hashT[s[end]] >= 0) { - countLeft--; - } - } + var hashT = buildHash(t); + var start = 0; + var end = 0; + var countLeft = t.length; + var minWindowLeft = -1; + var maxWindowRight = -1; + + while(start < s.length && end < s.length) { + if(countLeft > 0) { // window does not contain all elements + if(hashT[s[end]] !== undefined) { + hashT[s[end]] = hashT[s[end]] - 1; + if(hashT[s[end]] >= 0) { + countLeft--; + } + } - if(countLeft > 0) { - end++; - } - } else { // found window - if(minWindowLeft == -1 || ((maxWindowRight - minWindowLeft + 1) > (end - start + 1)) ) { - minWindowLeft = start; - maxWindowRight = end; - } - if(hashT[s[start]] !== undefined) { - hashT[s[start]] = hashT[s[start]] + 1; - if(hashT[s[start]] > 0) { - countLeft++; - end++; - } - } - start++; + if(countLeft > 0) { + end++; + } + } else { // found window + if(minWindowLeft == -1 || ((maxWindowRight - minWindowLeft + 1) > (end - start + 1)) ) { + minWindowLeft = start; + maxWindowRight = end; + } + if(hashT[s[start]] !== undefined) { + hashT[s[start]] = hashT[s[start]] + 1; + if(hashT[s[start]] > 0) { + countLeft++; + end++; } + } + start++; } + } - if(minWindowLeft > -1) { - return s.substring(minWindowLeft, maxWindowRight + 1); - } + if(minWindowLeft > -1) { + return s.substring(minWindowLeft, maxWindowRight + 1); + } - return ""; + return ""; }; module.exports.minWindow = minWindow; +module.exports.minWindow2 = minWindow2; \ No newline at end of file diff --git a/LeetcodeProblems/Algorithms/Next_Permutation.js b/LeetcodeProblems/Algorithms/Next_Permutation.js index e125d7f..30e496a 100644 --- a/LeetcodeProblems/Algorithms/Next_Permutation.js +++ b/LeetcodeProblems/Algorithms/Next_Permutation.js @@ -33,37 +33,37 @@ Output: [1,5,1] * @return {void} Do not return anything, modify nums in-place instead. */ - var nextPermutation = function(nums) { +var nextPermutation = function(nums) { let k = nums.length - 2; while ( k >= 0 && nums[k] >= nums[k + 1]) { - --k; + --k; } if (k === -1) { - reverse(nums, 0, nums.length-1); - return; + reverse(nums, 0, nums.length-1); + return; } for (let l = nums.length - 1; l > k; l--) { - if (nums[l] > nums[k]) { - swap(nums, k, l); - break; - } + if (nums[l] > nums[k]) { + swap(nums, k, l); + break; + } } reverse(nums, k + 1, nums.length -1); }; function swap(arr, a, b) { - let temp = arr[a]; - arr[a] = arr[b]; - arr[b] = temp; + let temp = arr[a]; + arr[a] = arr[b]; + arr[b] = temp; } function reverse(nums, start ,end) { while(start < end) { - swap(nums, start, end); - start++; - end--; + swap(nums, start, end); + start++; + end--; } } diff --git a/LeetcodeProblems/Algorithms/Permutations_In_String.js b/LeetcodeProblems/Algorithms/Permutations_In_String.js index 17ea56c..8ddfb40 100644 --- a/LeetcodeProblems/Algorithms/Permutations_In_String.js +++ b/LeetcodeProblems/Algorithms/Permutations_In_String.js @@ -26,9 +26,9 @@ s1 and s2 consist of lowercase English letters. * @param {string} s2 * @return {boolean} */ - var checkInclusion = function(s1, s2) { +var checkInclusion = function(s1, s2) { if(s1.length > s2.length) { - return false + return false; } let start = 0; @@ -37,37 +37,37 @@ s1 and s2 consist of lowercase English letters. let hashBuild = {}; for(var i = 0; i < s1.length; i++) { - hashBuild[s1[i]] = (hashBuild[s1[i]] || 0) + 1; + hashBuild[s1[i]] = (hashBuild[s1[i]] || 0) + 1; } for(var j = start; j < end; j++) { // TODO: didn't count upper bound - if(hashBuild[s2[j]] !== undefined) { - hashBuild[s2[j]] = hashBuild[s2[j]] - 1; - if(hashBuild[s2[j]] >= 0) { - countLeft--; - } + if(hashBuild[s2[j]] !== undefined) { + hashBuild[s2[j]] = hashBuild[s2[j]] - 1; + if(hashBuild[s2[j]] >= 0) { + countLeft--; } + } } while(end < s2.length) { - if(hashBuild[s2[end]] !== undefined) { - hashBuild[s2[end]] = hashBuild[s2[end]] - 1; - if(hashBuild[s2[end]] >= 0) { - countLeft--; - } + if(hashBuild[s2[end]] !== undefined) { + hashBuild[s2[end]] = hashBuild[s2[end]] - 1; + if(hashBuild[s2[end]] >= 0) { + countLeft--; } + } - if(countLeft == 0) { return true } + if(countLeft == 0) { return true; } - if(hashBuild[s2[start]] !== undefined) { - hashBuild[s2[start]] = hashBuild[s2[start]] + 1; - if(hashBuild[s2[start]] >= 1) { - countLeft++; - } + if(hashBuild[s2[start]] !== undefined) { + hashBuild[s2[start]] = hashBuild[s2[start]] + 1; + if(hashBuild[s2[start]] >= 1) { + countLeft++; } + } - start++; - end++; + start++; + end++; } return false; diff --git a/LeetcodeProblems/Algorithms/Shuffle_String.js b/LeetcodeProblems/Algorithms/Shuffle_String.js index e0ccd21..bf3395f 100644 --- a/LeetcodeProblems/Algorithms/Shuffle_String.js +++ b/LeetcodeProblems/Algorithms/Shuffle_String.js @@ -25,9 +25,9 @@ Explanation: After shuffling, each character remains in its position. * @return {string} */ var restoreString = function(s, indices) { - let arrshuffle = []; - indices.forEach((sindex, index) => arrshuffle[sindex] = s.charAt(index)) - return arrshuffle.join('') + let arrshuffle = []; + indices.forEach((sindex, index) => arrshuffle[sindex] = s.charAt(index)); + return arrshuffle.join(""); }; module.exports.restoreString = restoreString; \ No newline at end of file diff --git a/LeetcodeProblems/Algorithms/Time_Needed_Rearrange_Binary_String.js b/LeetcodeProblems/Algorithms/Time_Needed_Rearrange_Binary_String.js index 87e5b89..603a914 100644 --- a/LeetcodeProblems/Algorithms/Time_Needed_Rearrange_Binary_String.js +++ b/LeetcodeProblems/Algorithms/Time_Needed_Rearrange_Binary_String.js @@ -36,12 +36,14 @@ s[i] is either '0' or '1'. * @param {string} s * @return {number} */ - var secondsToRemoveOccurrences = function(s) { +var secondsToRemoveOccurrences = function(s) { let result = 0; - while (true) { - const replaced = s.replaceAll('01', '10'); + for(;;) { + const replaced = s.replaceAll("01", "10"); if (s === replaced) return result; s = replaced; result += 1; } -}; \ No newline at end of file +}; + +module.exports.secondsToRemoveOccurrences = secondsToRemoveOccurrences; diff --git a/LeetcodeProblems/Algorithms/Top_K_Frequent_Elements.js b/LeetcodeProblems/Algorithms/Top_K_Frequent_Elements.js index 5180f8c..80770cb 100644 --- a/LeetcodeProblems/Algorithms/Top_K_Frequent_Elements.js +++ b/LeetcodeProblems/Algorithms/Top_K_Frequent_Elements.js @@ -31,24 +31,24 @@ Constraints: * @param {number} k * @return {number[]} */ - var topKFrequent = function(nums, k) { +var topKFrequent = function(nums, k) { const freqMap = new Map(); const bucket = []; const result = []; for(let num of nums) { - freqMap.set(num, (freqMap.get(num) || 0) + 1); + freqMap.set(num, (freqMap.get(num) || 0) + 1); } for(let [num, freq] of freqMap) { - bucket[freq] = (bucket[freq] || new Set()).add(num); + bucket[freq] = (bucket[freq] || new Set()).add(num); } for(let i = bucket.length-1; i >= 0; i--) { - if(bucket[i]) { - result.push(...bucket[i]); - if(result.length === k) break; - } + if(bucket[i]) { + result.push(...bucket[i]); + if(result.length === k) break; + } } return result; }; diff --git a/LeetcodeProblemsTests/Algorithms/2Sum_Test.js b/LeetcodeProblemsTests/Algorithms/2Sum_Test.js index 1cf3897..132b3db 100644 --- a/LeetcodeProblemsTests/Algorithms/2Sum_Test.js +++ b/LeetcodeProblemsTests/Algorithms/2Sum_Test.js @@ -2,18 +2,16 @@ const assert = require("assert"); const twoSum = require("../../LeetcodeProblems/Algorithms/2Sum").twoSum; const twoSum2 = require("../../LeetcodeProblems/Algorithms/2Sum").twoSum2; + var test = function () { assert.deepEqual([0,1], twoSum([2,7,11,15], 9)); assert.deepEqual([1,2], twoSum([3,2,4], 6)); assert.deepEqual([0,1], twoSum([3,3], 6)); -}; - -var test2 = function () { + assert.deepEqual([0,1], twoSum2([2,7,11,15], 9)); assert.deepEqual([1,2], twoSum2([3,2,4], 6)); assert.deepEqual([0,1], twoSum2([3,3], 6)); }; -module.exports.test = test; -module.exports.test2 = test2; +module.exports.test = test; diff --git a/LeetcodeProblemsTests/Algorithms/Backspace_String_Compare_Test.js b/LeetcodeProblemsTests/Algorithms/Backspace_String_Compare_Test.js index 29d0757..22fbc39 100644 --- a/LeetcodeProblemsTests/Algorithms/Backspace_String_Compare_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Backspace_String_Compare_Test.js @@ -1,6 +1,6 @@ -const assert = require('assert'); -const backspaceCompare = require('../../LeetcodeProblems/Algorithms/Backspace_String_Compare').backspaceCompare; -const backspaceCompare2 = require('../../LeetcodeProblems/Algorithms/Backspace_String_Compare').backspaceCompare2; +const assert = require("assert"); +const backspaceCompare = require("../../LeetcodeProblems/Algorithms/Backspace_String_Compare").backspaceCompare; +const backspaceCompare2 = require("../../LeetcodeProblems/Algorithms/Backspace_String_Compare").backspaceCompare2; function test() { assert.equal(backspaceCompare("ab#c", "ad#c"), true); // true diff --git a/LeetcodeProblemsTests/Algorithms/Best_Time_To_Buy_And_Sell_Stock_II_Test.js b/LeetcodeProblemsTests/Algorithms/Best_Time_To_Buy_And_Sell_Stock_II_Test.js index 5af376c..c3207fb 100644 --- a/LeetcodeProblemsTests/Algorithms/Best_Time_To_Buy_And_Sell_Stock_II_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Best_Time_To_Buy_And_Sell_Stock_II_Test.js @@ -1,5 +1,5 @@ -const assert = require('assert'); -const maxProfit = require('../../LeetcodeProblems/Algorithms/Best_Time_To_Buy_And_Sell_Stock_II').maxProfit; +const assert = require("assert"); +const maxProfit = require("../../LeetcodeProblems/Algorithms/Best_Time_To_Buy_And_Sell_Stock_II").maxProfit; function test() { assert.equal(maxProfit([7,1,5,3,6,4]), 7); diff --git a/LeetcodeProblemsTests/Algorithms/Binary_Gap_Test.js b/LeetcodeProblemsTests/Algorithms/Binary_Gap_Test.js index 2754af7..6025822 100644 --- a/LeetcodeProblemsTests/Algorithms/Binary_Gap_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Binary_Gap_Test.js @@ -1,5 +1,5 @@ -const assert = require('assert'); -const binaryGap = require('../../LeetcodeProblems/Algorithms/Binary_Gap').binaryGap; +const assert = require("assert"); +const binaryGap = require("../../LeetcodeProblems/Algorithms/Binary_Gap").binaryGap; function test() { assert.equal(binaryGap(22), 2); // 10110 diff --git a/LeetcodeProblemsTests/Algorithms/Clone_Graph_Test.js b/LeetcodeProblemsTests/Algorithms/Clone_Graph_Test.js index ea8b26b..928d068 100644 --- a/LeetcodeProblemsTests/Algorithms/Clone_Graph_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Clone_Graph_Test.js @@ -1,5 +1,5 @@ var test = function() { // TODO -} +}; module.exports.test = test; diff --git a/LeetcodeProblemsTests/Algorithms/Coin_Change_Test.js b/LeetcodeProblemsTests/Algorithms/Coin_Change_Test.js index 4629e79..df5ae45 100644 --- a/LeetcodeProblemsTests/Algorithms/Coin_Change_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Coin_Change_Test.js @@ -1,5 +1,5 @@ -const assert = require('assert'); -const coinChange = require('../../LeetcodeProblems/Algorithms/Coin_Change').coinChange; +const assert = require("assert"); +const coinChange = require("../../LeetcodeProblems/Algorithms/Coin_Change").coinChange; function test() { assert.equal(coinChange([], 3), -1); diff --git a/LeetcodeProblemsTests/Algorithms/Construct_Binary_Tree_from_Preorder_and_Inorder_Traversal_Test.js b/LeetcodeProblemsTests/Algorithms/Construct_Binary_Tree_from_Preorder_and_Inorder_Traversal_Test.js index 93b7aed..1ddea51 100644 --- a/LeetcodeProblemsTests/Algorithms/Construct_Binary_Tree_from_Preorder_and_Inorder_Traversal_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Construct_Binary_Tree_from_Preorder_and_Inorder_Traversal_Test.js @@ -1,5 +1,5 @@ -const assert = require('assert'); -var buildTree = require('../../LeetcodeProblems/Algorithms/Construct_Binary_Tree_from_Preorder_and_Inorder_Traversal').buildTree; +// const assert = require("assert"); +var buildTree = require("../../LeetcodeProblems/Algorithms/Construct_Binary_Tree_from_Preorder_and_Inorder_Traversal").buildTree; function test() { // TODO diff --git a/LeetcodeProblemsTests/Algorithms/Container_With_Most_Water_Test.js b/LeetcodeProblemsTests/Algorithms/Container_With_Most_Water_Test.js index 4006962..cb08735 100644 --- a/LeetcodeProblemsTests/Algorithms/Container_With_Most_Water_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Container_With_Most_Water_Test.js @@ -1,5 +1,5 @@ -const assert = require('assert'); -const maxArea = require('../../LeetcodeProblems/Algorithms/Container_With_Most_Water_Test').maxArea; +const assert = require("assert"); +const maxArea = require("../../LeetcodeProblems/Algorithms/Container_With_Most_Water").maxArea; function test() { assert.equal(49, maxArea([1,8,6,2,5,4,8,3,7])); diff --git a/LeetcodeProblemsTests/Algorithms/Deletion_Distance_Test.js b/LeetcodeProblemsTests/Algorithms/Deletion_Distance_Test.js index 0cfdb27..d1ae378 100644 --- a/LeetcodeProblemsTests/Algorithms/Deletion_Distance_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Deletion_Distance_Test.js @@ -1,7 +1,7 @@ -const assert = require('assert'); -var deletionDistance = require('../../LeetcodeProblems/Algorithms/Deletion_Distance').deletionDistance; -var deletionDistance2 = require('../../LeetcodeProblems/Algorithms/Deletion_Distance').deletionDistance2; -var deletionDistanceDP = require('../../LeetcodeProblems/Algorithms/Deletion_Distance').deletionDistanceDP; +const assert = require("assert"); +var deletionDistance = require("../../LeetcodeProblems/Algorithms/Deletion_Distance").deletionDistance; +var deletionDistance2 = require("../../LeetcodeProblems/Algorithms/Deletion_Distance").deletionDistance2; +var deletionDistanceDP = require("../../LeetcodeProblems/Algorithms/Deletion_Distance").deletionDistanceDP; function test() { assert.equal(deletionDistance("dog", "frog"), 3); diff --git a/LeetcodeProblemsTests/Algorithms/Design_Circular_Deque_Test.js b/LeetcodeProblemsTests/Algorithms/Design_Circular_Deque_Test.js index 40f0b0e..cf8bc81 100644 --- a/LeetcodeProblemsTests/Algorithms/Design_Circular_Deque_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Design_Circular_Deque_Test.js @@ -1,5 +1,5 @@ -const assert = require('assert'); -var MyCircularDeque = require('../../LeetcodeProblems/Algorithms/Design_Circular_Deque').MyCircularDeque; +const assert = require("assert"); +var MyCircularDeque = require("../../LeetcodeProblems/Algorithms/Design_Circular_Deque").MyCircularDeque; var test = function() { const obj = new MyCircularDeque(3); @@ -12,6 +12,6 @@ var test = function() { assert.equal(obj.deleteLast(), true); assert.equal(obj.insertFront(4), true); assert.equal(obj.getFront(), 4); -} +}; module.exports.test = test; diff --git a/LeetcodeProblemsTests/Algorithms/Edit_Distance_Test.js b/LeetcodeProblemsTests/Algorithms/Edit_Distance_Test.js index f190880..2810420 100644 --- a/LeetcodeProblemsTests/Algorithms/Edit_Distance_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Edit_Distance_Test.js @@ -1,6 +1,6 @@ -const assert = require('assert'); -var minDistance = require('../../LeetcodeProblems/Algorithms/Edit_Distance').minDistance; -var minDistance2 = require('../../LeetcodeProblems/Algorithms/Edit_Distance').minDistance2; +const assert = require("assert"); +var minDistance = require("../../LeetcodeProblems/Algorithms/Edit_Distance").minDistance; +var minDistance2 = require("../../LeetcodeProblems/Algorithms/Edit_Distance").minDistance2; function test() { assert.equal(minDistance("ros", "horse"), 3); diff --git a/LeetcodeProblemsTests/Algorithms/Escape_The_Ghosts_Test.js b/LeetcodeProblemsTests/Algorithms/Escape_The_Ghosts_Test.js index ec0f67d..4225fbf 100644 --- a/LeetcodeProblemsTests/Algorithms/Escape_The_Ghosts_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Escape_The_Ghosts_Test.js @@ -1,6 +1,6 @@ -const assert = require('assert'); -var escapeGhosts = require('../../LeetcodeProblems/Algorithms/Escape_The_Ghosts').escapeGhosts; +const assert = require("assert"); +var escapeGhosts = require("../../LeetcodeProblems/Algorithms/Escape_The_Ghosts").escapeGhosts; function test() { assert.equal(escapeGhosts([[1, 0], [0, 3]], [0, 1]), true); diff --git a/LeetcodeProblemsTests/Algorithms/Find_Anagrams_Test.js b/LeetcodeProblemsTests/Algorithms/Find_Anagrams_Test.js index 504e136..3b90a57 100644 --- a/LeetcodeProblemsTests/Algorithms/Find_Anagrams_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Find_Anagrams_Test.js @@ -1,6 +1,6 @@ -const assert = require('assert'); -var findAnagrams = require('../../LeetcodeProblems/Algorithms/Find_Anagrams').findAnagrams; +const assert = require("assert"); +var findAnagrams = require("../../LeetcodeProblems/Algorithms/Find_Anagrams").findAnagrams; function test() { assert.deepEqual([], findAnagrams("AA", "BB")); diff --git a/LeetcodeProblemsTests/Algorithms/Find_Subarrays_With_Equal_Sum_Test.js b/LeetcodeProblemsTests/Algorithms/Find_Subarrays_With_Equal_Sum_Test.js index 09f692e..956a327 100644 --- a/LeetcodeProblemsTests/Algorithms/Find_Subarrays_With_Equal_Sum_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Find_Subarrays_With_Equal_Sum_Test.js @@ -1,10 +1,10 @@ -const assert = require('assert'); -const findSubarrays = require('../../LeetcodeProblems/Algorithms/Find_Subarrays_With_Equal_Sum').findSubarrays; +const assert = require("assert"); +const findSubarrays = require("../../LeetcodeProblems/Algorithms/Find_Subarrays_With_Equal_Sum").findSubarrays; var test = function () { assert.equal(findSubarrays([4,2,4]), true); assert.equal(findSubarrays([1,2,3,4,5]), false); assert.equal(findSubarrays([0,0,0]), true); -} +}; module.exports.test = test; diff --git a/LeetcodeProblemsTests/Algorithms/Flood_Fill_Test.js b/LeetcodeProblemsTests/Algorithms/Flood_Fill_Test.js index 36a29bb..447795f 100644 --- a/LeetcodeProblemsTests/Algorithms/Flood_Fill_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Flood_Fill_Test.js @@ -1,5 +1,5 @@ -const assert = require('assert'); -const floodFill = require('../../LeetcodeProblems/Algorithms/Flood_Fill').floodFill; +const assert = require("assert"); +const floodFill = require("../../LeetcodeProblems/Algorithms/Flood_Fill").floodFill; function test() { assert.deepEqual( diff --git a/LeetcodeProblemsTests/Algorithms/Generate_Parenthesis_Test.js b/LeetcodeProblemsTests/Algorithms/Generate_Parenthesis_Test.js index ea8b26b..928d068 100644 --- a/LeetcodeProblemsTests/Algorithms/Generate_Parenthesis_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Generate_Parenthesis_Test.js @@ -1,5 +1,5 @@ var test = function() { // TODO -} +}; module.exports.test = test; diff --git a/LeetcodeProblemsTests/Algorithms/Group_Anagrams_Test.js b/LeetcodeProblemsTests/Algorithms/Group_Anagrams_Test.js index be15de9..c871e7e 100644 --- a/LeetcodeProblemsTests/Algorithms/Group_Anagrams_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Group_Anagrams_Test.js @@ -1,11 +1,11 @@ -const assert = require('assert'); -const groupAnagrams = require('../../LeetcodeProblems/Algorithms/Group_Anagrams').groupAnagrams; +const assert = require("assert"); +const groupAnagrams = require("../../LeetcodeProblems/Algorithms/Group_Anagrams").groupAnagrams; function test() { assert.deepEqual( groupAnagrams(["eat", "tea", "tan", "ate", "nat", "bat"]), - [ [ 'eat', 'tea', 'ate' ], [ 'tan', 'nat' ], [ 'bat' ] ] - ) + [ [ "eat", "tea", "ate" ], [ "tan", "nat" ], [ "bat" ] ] + ); } module.exports.test = test; diff --git a/LeetcodeProblemsTests/Algorithms/Happy_Number_Test.js b/LeetcodeProblemsTests/Algorithms/Happy_Number_Test.js index 13d2013..fa4544d 100644 --- a/LeetcodeProblemsTests/Algorithms/Happy_Number_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Happy_Number_Test.js @@ -1,9 +1,9 @@ -const assert = require('assert'); -const isHappy = require('../../LeetcodeProblems/Algorithms/Happy_Number').isHappy; +const assert = require("assert"); +const isHappy = require("../../LeetcodeProblems/Algorithms/Happy_Number").isHappy; var test = function () { assert.equal(isHappy(19),true); assert.equal(isHappy(2),false); -} +}; module.exports.test = test; \ No newline at end of file diff --git a/LeetcodeProblemsTests/Algorithms/Implement_stack_using_queues_Test.js b/LeetcodeProblemsTests/Algorithms/Implement_stack_using_queues_Test.js index cdc8e7d..8e3fb3a 100644 --- a/LeetcodeProblemsTests/Algorithms/Implement_stack_using_queues_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Implement_stack_using_queues_Test.js @@ -1,5 +1,5 @@ -const assert = require('assert'); -const MyStack = require('../../LeetcodeProblems/Algorithms/Implement_stack_using_queues').MyStack; +const assert = require("assert"); +const MyStack = require("../../LeetcodeProblems/Algorithms/Implement_stack_using_queues").MyStack; var test = function () { var myStack = new MyStack(); @@ -14,6 +14,6 @@ var test = function () { assert.equal(myStack.pop(), 1); assert.equal(myStack.pop(), 2); assert.equal(myStack.pop(), 3); -} +}; module.exports.test = test; diff --git a/LeetcodeProblemsTests/Algorithms/Kth_Largest_Element_in_an_Array_Test.js b/LeetcodeProblemsTests/Algorithms/Kth_Largest_Element_in_an_Array_Test.js index a21900d..156d2e1 100644 --- a/LeetcodeProblemsTests/Algorithms/Kth_Largest_Element_in_an_Array_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Kth_Largest_Element_in_an_Array_Test.js @@ -1,5 +1,5 @@ -const assert = require('assert'); -const findKthLargest = require('../../LeetcodeProblems/Algorithms/Kth_Largest_Element_in_an_Array').findKthLargest; +const assert = require("assert"); +const findKthLargest = require("../../LeetcodeProblems/Algorithms/Kth_Largest_Element_in_an_Array").findKthLargest; function test() { assert.equal(findKthLargest([3,2,1,5,6,4], 2), 5); diff --git a/LeetcodeProblemsTests/Algorithms/Linked_List_Cycle_II_Test.js b/LeetcodeProblemsTests/Algorithms/Linked_List_Cycle_II_Test.js index 0206e8b..5014106 100644 --- a/LeetcodeProblemsTests/Algorithms/Linked_List_Cycle_II_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Linked_List_Cycle_II_Test.js @@ -1,13 +1,13 @@ -const assert = require('assert'); -var ListNode = require('../../UtilsClasses/ListNode').ListNode; -const detectCycle = require('../../LeetcodeProblems/Algorithms/Linked_List_Cycle_II').detectCycle; +const assert = require("assert"); +var ListNode = require("../../UtilsClasses/ListNode").ListNode; +const detectCycle = require("../../LeetcodeProblems/Algorithms/Linked_List_Cycle_II").detectCycle; var test = function() { const cycle = buildCycle(); var list = cycle.list; var nodeCycle = cycle.nodeCycle; assert.equal(detectCycle(list), nodeCycle); -} +}; function buildCycle() { var node1 = ListNode.linkenList([1,2,3,4,5]); diff --git a/LeetcodeProblemsTests/Algorithms/Longest_Common_Prefix_Test.js b/LeetcodeProblemsTests/Algorithms/Longest_Common_Prefix_Test.js index d1d5e46..92c6c7a 100644 --- a/LeetcodeProblemsTests/Algorithms/Longest_Common_Prefix_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Longest_Common_Prefix_Test.js @@ -1,6 +1,6 @@ -const assert = require('assert'); -const longestCommonPrefix = require('../../LeetcodeProblems/Algorithms/Longest_Common_Prefix').longestCommonPrefix; +const assert = require("assert"); +const longestCommonPrefix = require("../../LeetcodeProblems/Algorithms/Longest_Common_Prefix").longestCommonPrefix; function test() { assert.equal(longestCommonPrefix(["flower","flow","flight"]), "fl"); diff --git a/LeetcodeProblemsTests/Algorithms/Longest_Consecutive_Sequence_Test.js b/LeetcodeProblemsTests/Algorithms/Longest_Consecutive_Sequence_Test.js index 6ad441d..00cc6ed 100644 --- a/LeetcodeProblemsTests/Algorithms/Longest_Consecutive_Sequence_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Longest_Consecutive_Sequence_Test.js @@ -1,5 +1,5 @@ -const assert = require('assert'); -const longestConsecutive = require('../../LeetcodeProblems/Algorithms/Longest_Consecutive_Sequence').longestConsecutive; +const assert = require("assert"); +const longestConsecutive = require("../../LeetcodeProblems/Algorithms/Longest_Consecutive_Sequence").longestConsecutive; function test() { assert.equal(longestConsecutive([100, 1, 200, 3, 2, 400, 201]), 3); diff --git a/LeetcodeProblemsTests/Algorithms/Longest_Palindromic_Substring_Test.js b/LeetcodeProblemsTests/Algorithms/Longest_Palindromic_Substring_Test.js index 1b61abc..cab1b1b 100644 --- a/LeetcodeProblemsTests/Algorithms/Longest_Palindromic_Substring_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Longest_Palindromic_Substring_Test.js @@ -1,5 +1,5 @@ -const assert = require('assert'); -const longestPalindrome = require('../../LeetcodeProblems/Algorithms/Longest_Palindromic_Substring').longestPalindrome; +const assert = require("assert"); +const longestPalindrome = require("../../LeetcodeProblems/Algorithms/Longest_Palindromic_Substring").longestPalindrome; function test() { assert.equal(longestPalindrome("pabcdcbte"), "bcdcb"); diff --git a/LeetcodeProblemsTests/Algorithms/Longest_Substring_Test.js b/LeetcodeProblemsTests/Algorithms/Longest_Substring_Test.js index 09c073e..f4a873e 100644 --- a/LeetcodeProblemsTests/Algorithms/Longest_Substring_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Longest_Substring_Test.js @@ -1,5 +1,5 @@ -const assert = require('assert'); -const lengthOfLongestSubstring = require('../../LeetcodeProblems/Algorithms/Longest_Substring').lengthOfLongestSubstring; +const assert = require("assert"); +const lengthOfLongestSubstring = require("../../LeetcodeProblems/Algorithms/Longest_Substring").lengthOfLongestSubstring; function test() { assert.equal(4, lengthOfLongestSubstring("abcdbcd")); diff --git a/LeetcodeProblemsTests/Algorithms/Lowest_Common_Ancestor_of_a_Binary_Tree_Test.js b/LeetcodeProblemsTests/Algorithms/Lowest_Common_Ancestor_of_a_Binary_Tree_Test.js index 0ec8727..045f9ba 100644 --- a/LeetcodeProblemsTests/Algorithms/Lowest_Common_Ancestor_of_a_Binary_Tree_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Lowest_Common_Ancestor_of_a_Binary_Tree_Test.js @@ -1,7 +1,7 @@ -const assert = require('assert'); -var TreeNode = require('../../UtilsClasses/TreeNode').TreeNode; -const lowestCommonAncestor = require('../../LeetcodeProblems/Algorithms/Lowest_Common_Ancestor_of_a_Binary_Tree').lowestCommonAncestor; -const lowestCommonAncestor2 = require('../../LeetcodeProblems/Algorithms/Lowest_Common_Ancestor_of_a_Binary_Tree').lowestCommonAncestor2; +// const assert = require("assert"); +var TreeNode = require("../../UtilsClasses/TreeNode").TreeNode; +const lowestCommonAncestor = require("../../LeetcodeProblems/Algorithms/Lowest_Common_Ancestor_of_a_Binary_Tree").lowestCommonAncestor; +const lowestCommonAncestor2 = require("../../LeetcodeProblems/Algorithms/Lowest_Common_Ancestor_of_a_Binary_Tree").lowestCommonAncestor2; var test = function() { var root = new TreeNode(3); @@ -34,6 +34,6 @@ var test = function() { console.log(lowestCommonAncestor2(root, left, tempRight.right)); console.log(lowestCommonAncestor2(root, left, right)); -} +}; module.exports.test = test; diff --git a/LeetcodeProblemsTests/Algorithms/Majority_Element_Test.js b/LeetcodeProblemsTests/Algorithms/Majority_Element_Test.js index 494d641..b9420d2 100644 --- a/LeetcodeProblemsTests/Algorithms/Majority_Element_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Majority_Element_Test.js @@ -1,5 +1,5 @@ -const assert = require('assert'); -const majorityElement = require('../../LeetcodeProblems/Algorithms/Majority_Element').majorityElement; +const assert = require("assert"); +const majorityElement = require("../../LeetcodeProblems/Algorithms/Majority_Element").majorityElement; function test() { assert.equal(majorityElement([2,2,3]), 2); @@ -7,4 +7,4 @@ function test() { assert.equal(majorityElement([1,1,1,2,3,45,1,2,4,1,1]), 1); } -module.exports.test = test +module.exports.test = test; diff --git a/LeetcodeProblemsTests/Algorithms/Max_Area_Of_Island_Test.js b/LeetcodeProblemsTests/Algorithms/Max_Area_Of_Island_Test.js index 69bf4e1..b8269e6 100644 --- a/LeetcodeProblemsTests/Algorithms/Max_Area_Of_Island_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Max_Area_Of_Island_Test.js @@ -1,5 +1,5 @@ -const assert = require('assert'); -const { maxAreaOfIsland } = require('../../LeetcodeProblems/Algorithms/Max_Area_Of_Island'); +const assert = require("assert"); +const { maxAreaOfIsland } = require("../../LeetcodeProblems/Algorithms/Max_Area_Of_Island"); function test1() { var matrix = [ @@ -11,7 +11,7 @@ function test1() { [0,0,0,0,0,0,0,0,0,0,1,0,0], [0,0,0,0,0,0,0,1,1,1,0,0,0], [0,0,0,0,0,0,0,1,1,0,0,0,0] - ] + ]; assert.strictEqual(maxAreaOfIsland(matrix), 6); } @@ -24,7 +24,7 @@ function test2() { [0, 1, 0, 0, 1], [0, 1, 1, 0, 1], [0, 0, 0, 0, 0], - ] + ]; assert.strictEqual(maxAreaOfIsland(matrix), 5); } @@ -37,7 +37,7 @@ function test3() { [0, 1, 1, 1, 1], [0, 0, 1, 0, 0], [0, 0, 0, 0, 0], - ] + ]; assert.strictEqual(maxAreaOfIsland(matrix), 11); } @@ -48,4 +48,4 @@ function test() { test3(); } -module.exports.test = test +module.exports.test = test; diff --git a/LeetcodeProblemsTests/Algorithms/Max_Consecutive_Ones_III_Test.js b/LeetcodeProblemsTests/Algorithms/Max_Consecutive_Ones_III_Test.js index 96b6986..ef4eb81 100644 --- a/LeetcodeProblemsTests/Algorithms/Max_Consecutive_Ones_III_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Max_Consecutive_Ones_III_Test.js @@ -1,5 +1,5 @@ -const assert = require('assert'); -const longestOnes = require('../../LeetcodeProblems/Algorithms/Max_Consecutive_Ones_III').longestOnes; +const assert = require("assert"); +const longestOnes = require("../../LeetcodeProblems/Algorithms/Max_Consecutive_Ones_III").longestOnes; function test() { assert.equal(1, longestOnes([1], 1)); diff --git a/LeetcodeProblemsTests/Algorithms/Maximal_Square_Test.js b/LeetcodeProblemsTests/Algorithms/Maximal_Square_Test.js index 2aa2a6e..11a0764 100644 --- a/LeetcodeProblemsTests/Algorithms/Maximal_Square_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Maximal_Square_Test.js @@ -1,5 +1,5 @@ -const assert = require('assert'); -const maximalSquare = require('../../LeetcodeProblems/Algorithms/Maximal_Square').maximalSquare; +const assert = require("assert"); +const maximalSquare = require("../../LeetcodeProblems/Algorithms/Maximal_Square").maximalSquare; function test() { assert.equal(maximalSquare([["1","0"]]), 1); diff --git a/LeetcodeProblemsTests/Algorithms/Maximise_Hour_Glass_Sum.js b/LeetcodeProblemsTests/Algorithms/Maximise_Hour_Glass_Sum.js index 066b941..f728cfd 100644 --- a/LeetcodeProblemsTests/Algorithms/Maximise_Hour_Glass_Sum.js +++ b/LeetcodeProblemsTests/Algorithms/Maximise_Hour_Glass_Sum.js @@ -1,9 +1,9 @@ -const assert = require('assert'); -const maxSum = require('../../LeetcodeProblems/Algorithms/Maximise_Hour_Glass_Sum').maxSum; +const assert = require("assert"); +const maxSum = require("../../LeetcodeProblems/Algorithms/Maximise_Hour_Glass_Sum").maxSum; function test() { assert.equal(maxSum([[6,2,1,3],[4,2,1,5],[9,2,8,7],[4,1,2,9]]), 30); assert.equal(maxSum([[1,2,3],[4,5,6],[7,8,9]]), 35); } -module.exports.test = test +module.exports.test = test; diff --git a/LeetcodeProblemsTests/Algorithms/Maximun_Subarray_Test.js b/LeetcodeProblemsTests/Algorithms/Maximun_Subarray_Test.js index aeb6f0d..68fd57e 100644 --- a/LeetcodeProblemsTests/Algorithms/Maximun_Subarray_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Maximun_Subarray_Test.js @@ -1,5 +1,5 @@ -const assert = require('assert'); -const maxSubArray = require('../../LeetcodeProblems/Algorithms/Maximun_Subarray').maxSubArray; +const assert = require("assert"); +const maxSubArray = require("../../LeetcodeProblems/Algorithms/Maximun_Subarray").maxSubArray; function test() { assert.equal(maxSubArray([]), 0); diff --git a/LeetcodeProblemsTests/Algorithms/Min_Stack_Test.js b/LeetcodeProblemsTests/Algorithms/Min_Stack_Test.js index 16fb7c6..5cbddda 100644 --- a/LeetcodeProblemsTests/Algorithms/Min_Stack_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Min_Stack_Test.js @@ -1,5 +1,5 @@ -const assert = require('assert'); -const MinStack = require('../../LeetcodeProblems/Algorithms/Min_Stack').MinStack; +const assert = require("assert"); +const MinStack = require("../../LeetcodeProblems/Algorithms/Min_Stack").MinStack; function test() { var minStack = new MinStack(); diff --git a/LeetcodeProblemsTests/Algorithms/Minimize_Maximum_Pair_Sum_In_Array_Test.js b/LeetcodeProblemsTests/Algorithms/Minimize_Maximum_Pair_Sum_In_Array_Test.js index 240edd6..3180536 100644 --- a/LeetcodeProblemsTests/Algorithms/Minimize_Maximum_Pair_Sum_In_Array_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Minimize_Maximum_Pair_Sum_In_Array_Test.js @@ -1,10 +1,10 @@ -const assert = require('assert'); -const minPairSum = require('../../LeetcodeProblems/Algorithms/Minimize_Maximum_Pair_Sum_In_Array').minPairSum; +const assert = require("assert"); +const minPairSum = require("../../LeetcodeProblems/Algorithms/Minimize_Maximum_Pair_Sum_In_Array").minPairSum; var test = function () { assert.equal(minPairSum([3,5,2,3]), 7); assert.equal(minPairSum([3,5,4,2,4,6]), 8); assert.equal(minPairSum([1,1,1,1]), 2); -} +}; module.exports.test = test; diff --git a/LeetcodeProblemsTests/Algorithms/Minimum_Add_To_Make_Parentheses_Valid_Test.js b/LeetcodeProblemsTests/Algorithms/Minimum_Add_To_Make_Parentheses_Valid_Test.js index b175736..69baf4d 100644 --- a/LeetcodeProblemsTests/Algorithms/Minimum_Add_To_Make_Parentheses_Valid_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Minimum_Add_To_Make_Parentheses_Valid_Test.js @@ -1,5 +1,5 @@ -const assert = require('assert'); -const minAddToMakeValid = require('../../LeetcodeProblems/Algorithms/Minimum_Add_To_Make_Parentheses_Valid').minAddToMakeValid; +const assert = require("assert"); +const minAddToMakeValid = require("../../LeetcodeProblems/Algorithms/Minimum_Add_To_Make_Parentheses_Valid").minAddToMakeValid; var test = function() { assert.strictEqual(1, minAddToMakeValid("()(")); diff --git a/LeetcodeProblemsTests/Algorithms/Minimum_Size_Subarray_Test.js b/LeetcodeProblemsTests/Algorithms/Minimum_Size_Subarray_Test.js index 7d2c4d1..a09502e 100644 --- a/LeetcodeProblemsTests/Algorithms/Minimum_Size_Subarray_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Minimum_Size_Subarray_Test.js @@ -1,5 +1,5 @@ -const assert = require('assert'); -const minSubArrayLength = require('../../LeetcodeProblems/Algorithms/Minimum_Size_Subarray').minSubArrayLength; +const assert = require("assert"); +const minSubArrayLength = require("../../LeetcodeProblems/Algorithms/Minimum_Size_Subarray").minSubArrayLength; function test() { assert.equal(0, minSubArrayLength(10, [])); diff --git a/LeetcodeProblemsTests/Algorithms/Minimum_Window_Substring_Test.js b/LeetcodeProblemsTests/Algorithms/Minimum_Window_Substring_Test.js index d18350e..e580710 100644 --- a/LeetcodeProblemsTests/Algorithms/Minimum_Window_Substring_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Minimum_Window_Substring_Test.js @@ -1,5 +1,5 @@ -const assert = require('assert'); -const minWindow = require('../../LeetcodeProblems/Algorithms/Minimum_Window_Substring').minWindow; +const assert = require("assert"); +const minWindow = require("../../LeetcodeProblems/Algorithms/Minimum_Window_Substring").minWindow; function test() { assert.equal(minWindow("ADOBECODEBANC", "ABC"), "BANC"); diff --git a/LeetcodeProblemsTests/Algorithms/NQueens_Test.js b/LeetcodeProblemsTests/Algorithms/NQueens_Test.js index 9387762..42d95ff 100644 --- a/LeetcodeProblemsTests/Algorithms/NQueens_Test.js +++ b/LeetcodeProblemsTests/Algorithms/NQueens_Test.js @@ -1,5 +1,5 @@ -const assert = require('assert'); -const solveNQueens = require('../../LeetcodeProblems/Algorithms/NQueens').solveNQueens; +// const assert = require("assert"); +const solveNQueens = require("../../LeetcodeProblems/Algorithms/NQueens").solveNQueens; // TODO: Add assertions @@ -7,7 +7,7 @@ var test = function() { printMatrixes(solveNQueens(4), 4); printMatrixes(solveNQueens(5), 5); printMatrixes(solveNQueens(6), 6); -} +}; var printMatrixes = function(matrixes, n) { console.log("Start solution of n: " + n); @@ -15,7 +15,7 @@ var printMatrixes = function(matrixes, n) { printMatrix(matrixes[i]); } console.log("End solution of n: " + n); -} +}; var printMatrix = function(matrix) { console.log("------------"); @@ -23,6 +23,6 @@ var printMatrix = function(matrix) { console.log(matrix[i]); } console.log("------------"); -} +}; module.exports.test = test; diff --git a/LeetcodeProblemsTests/Algorithms/Next_Permutation_Test.js b/LeetcodeProblemsTests/Algorithms/Next_Permutation_Test.js index e14a5e0..ec47f8c 100644 --- a/LeetcodeProblemsTests/Algorithms/Next_Permutation_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Next_Permutation_Test.js @@ -1,5 +1,5 @@ -const assert = require('assert'); -const nextPermutation = require('../../LeetcodeProblems/Algorithms/Next_Permutation').nextPermutation; +const assert = require("assert"); +const nextPermutation = require("../../LeetcodeProblems/Algorithms/Next_Permutation").nextPermutation; function test() { let test1 = [1,2,3]; diff --git a/LeetcodeProblemsTests/Algorithms/Number_of_Islands_Test.js b/LeetcodeProblemsTests/Algorithms/Number_of_Islands_Test.js index cd07f85..1a46197 100644 --- a/LeetcodeProblemsTests/Algorithms/Number_of_Islands_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Number_of_Islands_Test.js @@ -1,5 +1,5 @@ -const assert = require('assert'); -const numIslands = require('../../LeetcodeProblems/Algorithms/Number_of_Islands').numIslands; +const assert = require("assert"); +const numIslands = require("../../LeetcodeProblems/Algorithms/Number_of_Islands").numIslands; function test() { assert.equal(numIslands([[1]]), 1); diff --git a/LeetcodeProblemsTests/Algorithms/Number_of_Segments_in_a_String_Test.js b/LeetcodeProblemsTests/Algorithms/Number_of_Segments_in_a_String_Test.js index e78cd3e..13a982f 100644 --- a/LeetcodeProblemsTests/Algorithms/Number_of_Segments_in_a_String_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Number_of_Segments_in_a_String_Test.js @@ -1,5 +1,5 @@ -const assert = require('assert'); -const countSegments = require('../../LeetcodeProblems/Algorithms/Number_of_Segments_in_a_String').countSegments; +const assert = require("assert"); +const countSegments = require("../../LeetcodeProblems/Algorithms/Number_of_Segments_in_a_String").countSegments; function test() { assert.equal(countSegments(" "), 0); diff --git a/LeetcodeProblemsTests/Algorithms/Permutations_II_Test.js b/LeetcodeProblemsTests/Algorithms/Permutations_II_Test.js index 53f4449..e464f80 100644 --- a/LeetcodeProblemsTests/Algorithms/Permutations_II_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Permutations_II_Test.js @@ -1,30 +1,30 @@ -const assert = require('assert'); -const permuteUnique = require('../../LeetcodeProblems/Algorithms/Permutations_II').permuteUnique; +const assert = require("assert"); +const permuteUnique = require("../../LeetcodeProblems/Algorithms/Permutations_II").permuteUnique; function test() { assert.deepEqual( permuteUnique([1,1,2]), - [ [ '1', '1', '2' ], [ '1', '2', '1' ], [ '2', '1', '1' ] ] + [ [ "1", "1", "2" ], [ "1", "2", "1" ], [ "2", "1", "1" ] ] ); assert.deepEqual( permuteUnique([1,3,2,1]), [ - [ '1', '1', '2', '3' ], - [ '1', '1', '3', '2' ], - [ '1', '2', '1', '3' ], - [ '1', '2', '3', '1' ], - [ '1', '3', '1', '2' ], - [ '1', '3', '2', '1' ], - [ '2', '1', '1', '3' ], - [ '2', '1', '3', '1' ], - [ '2', '3', '1', '1' ], - [ '3', '1', '1', '2' ], - [ '3', '1', '2', '1' ], - [ '3', '2', '1', '1' ] + [ "1", "1", "2", "3" ], + [ "1", "1", "3", "2" ], + [ "1", "2", "1", "3" ], + [ "1", "2", "3", "1" ], + [ "1", "3", "1", "2" ], + [ "1", "3", "2", "1" ], + [ "2", "1", "1", "3" ], + [ "2", "1", "3", "1" ], + [ "2", "3", "1", "1" ], + [ "3", "1", "1", "2" ], + [ "3", "1", "2", "1" ], + [ "3", "2", "1", "1" ] ] ); assert.deepEqual(permuteUnique([]), [ [] ]); - assert.deepEqual(permuteUnique([1,1]), [ [ '1', '1' ] ]); + assert.deepEqual(permuteUnique([1,1]), [ [ "1", "1" ] ]); } module.exports.test = test; diff --git a/LeetcodeProblemsTests/Algorithms/Permutations_In_String_Test.js b/LeetcodeProblemsTests/Algorithms/Permutations_In_String_Test.js index 33b4dbf..f8d4536 100644 --- a/LeetcodeProblemsTests/Algorithms/Permutations_In_String_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Permutations_In_String_Test.js @@ -1,5 +1,5 @@ -const assert = require('assert'); -const checkInclusion = require('../../LeetcodeProblems/Algorithms/Permutations_In_String').checkInclusion; +const assert = require("assert"); +const checkInclusion = require("../../LeetcodeProblems/Algorithms/Permutations_In_String").checkInclusion; function test() { assert.equal(false, checkInclusion("abc", "ab")); diff --git a/LeetcodeProblemsTests/Algorithms/Permutations_Test.js b/LeetcodeProblemsTests/Algorithms/Permutations_Test.js index 8fe85e0..f3f338e 100644 --- a/LeetcodeProblemsTests/Algorithms/Permutations_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Permutations_Test.js @@ -1,5 +1,5 @@ -const assert = require('assert'); -const permute = require('../../LeetcodeProblems/Algorithms/Permutations').permute; +const assert = require("assert"); +const permute = require("../../LeetcodeProblems/Algorithms/Permutations").permute; function test() { assert.deepEqual(permute([]), [ [] ]); diff --git a/LeetcodeProblemsTests/Algorithms/Permutations_With_Duplicates_Test.js b/LeetcodeProblemsTests/Algorithms/Permutations_With_Duplicates_Test.js index ecbdf12..984192b 100644 --- a/LeetcodeProblemsTests/Algorithms/Permutations_With_Duplicates_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Permutations_With_Duplicates_Test.js @@ -1,5 +1,5 @@ -const assert = require('assert'); -const subsetWithoutDuplicates = require('../../LeetcodeProblems/Algorithms/Permutations_With_Duplicates').subsetWithoutDuplicates; +const assert = require("assert"); +const subsetWithoutDuplicates = require("../../LeetcodeProblems/Algorithms/Permutations_With_Duplicates").subsetWithoutDuplicates; var test = function() { assert.deepEqual( @@ -19,6 +19,6 @@ var test = function() { [ 3, 2, 1, 1 ] ] ); -} +}; module.exports.test = test; diff --git a/LeetcodeProblemsTests/Algorithms/Permutations_Without_Duplicates_Test.js b/LeetcodeProblemsTests/Algorithms/Permutations_Without_Duplicates_Test.js index 60169e9..379b627 100644 --- a/LeetcodeProblemsTests/Algorithms/Permutations_Without_Duplicates_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Permutations_Without_Duplicates_Test.js @@ -1,5 +1,5 @@ -const assert = require('assert'); -const subsetWithoutDuplicates = require('../../LeetcodeProblems/Algorithms/Permutations_Without_Duplicates').subsetWithoutDuplicates; +const assert = require("assert"); +const subsetWithoutDuplicates = require("../../LeetcodeProblems/Algorithms/Permutations_Without_Duplicates").subsetWithoutDuplicates; var test = function() { assert.deepEqual( @@ -13,6 +13,6 @@ var test = function() { [ 3, 2, 1 ] ] ); -} +}; module.exports.test = test; diff --git a/LeetcodeProblemsTests/Algorithms/Regular_Expression_Matching_Test.js b/LeetcodeProblemsTests/Algorithms/Regular_Expression_Matching_Test.js index ffcf28d..95bca74 100644 --- a/LeetcodeProblemsTests/Algorithms/Regular_Expression_Matching_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Regular_Expression_Matching_Test.js @@ -1,5 +1,5 @@ -const assert = require('assert'); -const isMatch = require('../../LeetcodeProblems/Algorithms/Regular_Expression_Matching').isMatch; +const assert = require("assert"); +const isMatch = require("../../LeetcodeProblems/Algorithms/Regular_Expression_Matching").isMatch; var test = function() { assert.equal(isMatch("aa", "a"), false); @@ -8,6 +8,6 @@ var test = function() { assert.equal(isMatch("ab", ".*"), true); assert.equal(isMatch("aab", "c*a*b"), true); assert.equal(isMatch("mississippi", "mis*is*p*."), false); -} +}; module.exports.test = test; diff --git a/LeetcodeProblemsTests/Algorithms/Remove_Invalid_Parentheses_Test.js b/LeetcodeProblemsTests/Algorithms/Remove_Invalid_Parentheses_Test.js index a0a1a48..d345205 100644 --- a/LeetcodeProblemsTests/Algorithms/Remove_Invalid_Parentheses_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Remove_Invalid_Parentheses_Test.js @@ -1,11 +1,11 @@ -const assert = require('assert'); -const removeInvalidParentheses = require('../../LeetcodeProblems/Algorithms/Remove_Invalid_Parentheses').removeInvalidParentheses; +const assert = require("assert"); +const removeInvalidParentheses = require("../../LeetcodeProblems/Algorithms/Remove_Invalid_Parentheses").removeInvalidParentheses; var test = function() { assert.equal(removeInvalidParentheses("))))(()"), "()"); assert.equal(removeInvalidParentheses("(()"), "()"); assert.equal(removeInvalidParentheses("(d))()"), "(d)()"); assert.equal(removeInvalidParentheses("(())"), "(())"); -} +}; module.exports.test = test; diff --git a/LeetcodeProblemsTests/Algorithms/Restore_IP_Addresses_Test.js b/LeetcodeProblemsTests/Algorithms/Restore_IP_Addresses_Test.js index f7d1c4a..9cf9ceb 100644 --- a/LeetcodeProblemsTests/Algorithms/Restore_IP_Addresses_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Restore_IP_Addresses_Test.js @@ -1,9 +1,9 @@ -const assert = require('assert'); -const restoreIpAddresses = require('../../LeetcodeProblems/Algorithms/Restore_IP_Addresses').restoreIpAddresses; +const assert = require("assert"); +const restoreIpAddresses = require("../../LeetcodeProblems/Algorithms/Restore_IP_Addresses").restoreIpAddresses; var test = function() { - assert.deepEqual(restoreIpAddresses("010010"), [ '0.10.0.10', '0.100.1.0']); - assert.deepEqual(restoreIpAddresses("25525511135"), [ '255.255.11.135', '255.255.111.35' ]); -} + assert.deepEqual(restoreIpAddresses("010010"), [ "0.10.0.10", "0.100.1.0"]); + assert.deepEqual(restoreIpAddresses("25525511135"), [ "255.255.11.135", "255.255.111.35" ]); +}; module.exports.test = test; diff --git a/LeetcodeProblemsTests/Algorithms/Reverse_Integer_Test.js b/LeetcodeProblemsTests/Algorithms/Reverse_Integer_Test.js index a599cf3..95da676 100644 --- a/LeetcodeProblemsTests/Algorithms/Reverse_Integer_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Reverse_Integer_Test.js @@ -1,11 +1,11 @@ -const assert = require('assert'); -const reverseInteger = require('../../LeetcodeProblems/Algorithms/Reverse_Integer').reverseInteger; +const assert = require("assert"); +const reverseInteger = require("../../LeetcodeProblems/Algorithms/Reverse_Integer").reverseInteger; var test = function() { assert.equal(reverseInteger(123), 321); assert.equal(reverseInteger(-123), -321); assert.equal(reverseInteger(120), 21); -} +}; module.exports.test = test; diff --git a/LeetcodeProblemsTests/Algorithms/Reverse_String_II_Test.js b/LeetcodeProblemsTests/Algorithms/Reverse_String_II_Test.js index e86446d..9fae9d4 100644 --- a/LeetcodeProblemsTests/Algorithms/Reverse_String_II_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Reverse_String_II_Test.js @@ -1,11 +1,11 @@ -const assert = require('assert'); -const reverseStr = require('../../LeetcodeProblems/Algorithms/Reverse_String_II').reverseStr; +const assert = require("assert"); +const reverseStr = require("../../LeetcodeProblems/Algorithms/Reverse_String_II").reverseStr; var test = function() { assert.equal(reverseStr("abcdefg", 2), "bacdfeg"); assert.equal(reverseStr("abcdefg", 3), "cbadefg"); assert.equal(reverseStr("abcdefg", 1), "abcdefg"); assert.equal(reverseStr("abcdefg", 0), "abcdefg"); -} +}; module.exports.test = test; diff --git a/LeetcodeProblemsTests/Algorithms/Same_Tree_Test.js b/LeetcodeProblemsTests/Algorithms/Same_Tree_Test.js index ea8b26b..928d068 100644 --- a/LeetcodeProblemsTests/Algorithms/Same_Tree_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Same_Tree_Test.js @@ -1,5 +1,5 @@ var test = function() { // TODO -} +}; module.exports.test = test; diff --git a/LeetcodeProblemsTests/Algorithms/SearchIng_Rotated_Sorted_Array_Test.js b/LeetcodeProblemsTests/Algorithms/SearchIng_Rotated_Sorted_Array_Test.js index 1e45c57..866496c 100644 --- a/LeetcodeProblemsTests/Algorithms/SearchIng_Rotated_Sorted_Array_Test.js +++ b/LeetcodeProblemsTests/Algorithms/SearchIng_Rotated_Sorted_Array_Test.js @@ -1,8 +1,8 @@ -const assert = require('assert'); -const search = require('../../LeetcodeProblems/Algorithms/SearchIng_Rotated_Sorted_Array').search; +const assert = require("assert"); +const search = require("../../LeetcodeProblems/Algorithms/SearchIng_Rotated_Sorted_Array").search; var test = function() { assert.equal(search([4,5,6,7,0,1,2], 5), 1); -} +}; module.exports.test = test; diff --git a/LeetcodeProblemsTests/Algorithms/Search_a_2D_Matrix_II_Test.js b/LeetcodeProblemsTests/Algorithms/Search_a_2D_Matrix_II_Test.js index f7b27f8..933f5dd 100644 --- a/LeetcodeProblemsTests/Algorithms/Search_a_2D_Matrix_II_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Search_a_2D_Matrix_II_Test.js @@ -1,5 +1,5 @@ -const assert = require('assert'); -const searchMatrix = require('../../LeetcodeProblems/Algorithms/Search_a_2D_Matrix_II').searchMatrix; +const assert = require("assert"); +const searchMatrix = require("../../LeetcodeProblems/Algorithms/Search_a_2D_Matrix_II").searchMatrix; const matrix1 = [ [1,4,7, 11,15], @@ -13,6 +13,6 @@ var test = function() { assert.equal(searchMatrix(matrix1, 5), true); assert.equal(searchMatrix(matrix1, 0), false); assert.equal(searchMatrix(matrix1, 15), true); -} +}; module.exports.test = test; diff --git a/LeetcodeProblemsTests/Algorithms/Search_a_2D_Matrix_Test.js b/LeetcodeProblemsTests/Algorithms/Search_a_2D_Matrix_Test.js index f1e2856..f5e7380 100644 --- a/LeetcodeProblemsTests/Algorithms/Search_a_2D_Matrix_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Search_a_2D_Matrix_Test.js @@ -1,5 +1,5 @@ -const assert = require('assert'); -const searchMatrix = require('../../LeetcodeProblems/Algorithms/Search_a_2D_Matrix').searchMatrix; +const assert = require("assert"); +const searchMatrix = require("../../LeetcodeProblems/Algorithms/Search_a_2D_Matrix").searchMatrix; var test = function() { assert.equal(searchMatrix([], 0), false); @@ -7,6 +7,6 @@ var test = function() { assert.equal(searchMatrix([[1], [3]], 1), true); const matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,50]]; assert.equal(searchMatrix(matrix, 3), true); -} +}; module.exports.test = test; diff --git a/LeetcodeProblemsTests/Algorithms/Set_Matrix_Zeroes_Test.js b/LeetcodeProblemsTests/Algorithms/Set_Matrix_Zeroes_Test.js index 732c4f1..98709fe 100644 --- a/LeetcodeProblemsTests/Algorithms/Set_Matrix_Zeroes_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Set_Matrix_Zeroes_Test.js @@ -1,11 +1,11 @@ -const assert = require('assert'); -const setZeroes = require('../../LeetcodeProblems/Algorithms/Set_Matrix_Zeroes').setZeroes; +const assert = require("assert"); +const setZeroes = require("../../LeetcodeProblems/Algorithms/Set_Matrix_Zeroes").setZeroes; var test = function() { assert.deepEqual( setZeroes([[1,1,1],[1,0,1],[1,1,1]]), [[1, 0, 1], [0, 0, 0], [1, 0, 1]] ); -} +}; module.exports.test = test; diff --git a/LeetcodeProblemsTests/Algorithms/Shuffle_String_Test.js b/LeetcodeProblemsTests/Algorithms/Shuffle_String_Test.js index b53eef9..cb37d91 100644 --- a/LeetcodeProblemsTests/Algorithms/Shuffle_String_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Shuffle_String_Test.js @@ -1,10 +1,10 @@ -const assert = require('assert'); -const restoreString = require('../../LeetcodeProblems/Algorithms/Shuffle_String').restoreString; +const assert = require("assert"); +const restoreString = require("../../LeetcodeProblems/Algorithms/Shuffle_String").restoreString; var test = function () { - assert.equal(restoreString('codeleet',[4,5,6,7,0,2,1,3]),'leetcode'); - assert.equal(restoreString('abc',[0,1,2]),'abc'); - assert.equal(restoreString('hacktoberfest',[9,10,11,12,4,5,6,7,8,0,1,2,3]),'festtoberhack'); -} + assert.equal(restoreString("codeleet",[4,5,6,7,0,2,1,3]),"leetcode"); + assert.equal(restoreString("abc",[0,1,2]),"abc"); + assert.equal(restoreString("hacktoberfest",[9,10,11,12,4,5,6,7,8,0,1,2,3]),"festtoberhack"); +}; module.exports.test = test; \ No newline at end of file diff --git a/LeetcodeProblemsTests/Algorithms/Simplify_Path_Test.js b/LeetcodeProblemsTests/Algorithms/Simplify_Path_Test.js index d5d27cd..8815e3d 100644 --- a/LeetcodeProblemsTests/Algorithms/Simplify_Path_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Simplify_Path_Test.js @@ -1,5 +1,5 @@ -const assert = require('assert'); -const simplifyPath = require('../../LeetcodeProblems/Algorithms/Simplify_Path').simplifyPath; +const assert = require("assert"); +const simplifyPath = require("../../LeetcodeProblems/Algorithms/Simplify_Path").simplifyPath; var test = function() { assert.equal(simplifyPath("/../c"), "/c"); @@ -7,7 +7,7 @@ var test = function() { assert.equal(simplifyPath("/home/"), "/home"); // => "/home" assert.equal(simplifyPath("/a/./b/../../c/"), "/c"); // => "/c" assert.equal(simplifyPath("/a/../../b/../c//.//"), "/c"); // => "/c" - assert.equal(simplifyPath("/a//b////c/d//././/.."), "/a/b/c") // => "/a/b/c" -} + assert.equal(simplifyPath("/a//b////c/d//././/.."), "/a/b/c"); // => "/a/b/c" +}; module.exports.test = test; diff --git a/LeetcodeProblemsTests/Algorithms/Spiral_Matrix_Test.js b/LeetcodeProblemsTests/Algorithms/Spiral_Matrix_Test.js index 7ffe0f8..ff56856 100644 --- a/LeetcodeProblemsTests/Algorithms/Spiral_Matrix_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Spiral_Matrix_Test.js @@ -1,17 +1,17 @@ -const assert = require('assert'); -const spiralOrder = require('../../LeetcodeProblems/Algorithms/Spiral_Matrix').spiralOrder; +const assert = require("assert"); +const spiralOrder = require("../../LeetcodeProblems/Algorithms/Spiral_Matrix").spiralOrder; var test = function() { const matrix = [ - [ 1, 2, 3 ], - [ 4, 5, 6 ], - [ 7, 8, 9 ] - ] + [ 1, 2, 3 ], + [ 4, 5, 6 ], + [ 7, 8, 9 ] + ]; assert.deepEqual( spiralOrder(matrix), [1, 2, 3, 6, 9, 8, 7, 4, 5] - ) -} + ); +}; module.exports.test = test; diff --git a/LeetcodeProblemsTests/Algorithms/Subarray_Sum_Equals_K_Test.js b/LeetcodeProblemsTests/Algorithms/Subarray_Sum_Equals_K_Test.js index b7697a6..1b3481b 100644 --- a/LeetcodeProblemsTests/Algorithms/Subarray_Sum_Equals_K_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Subarray_Sum_Equals_K_Test.js @@ -1,11 +1,11 @@ -const assert = require('assert'); -const subarraySum = require('../../LeetcodeProblems/Algorithms/Subarray_Sum_Equals_K').subarraySum; +const assert = require("assert"); +const subarraySum = require("../../LeetcodeProblems/Algorithms/Subarray_Sum_Equals_K").subarraySum; var test = function() { assert.strictEqual(subarraySum([1,1,1], 2), 2); assert.strictEqual(subarraySum([1], 0), 0); assert.strictEqual(subarraySum([0], 0), 1); assert.strictEqual(subarraySum([0,0,0,0,0], 0), 15); -} +}; module.exports.test = test; diff --git a/LeetcodeProblemsTests/Algorithms/Subsets_Test.js b/LeetcodeProblemsTests/Algorithms/Subsets_Test.js index 63bad19..c175b83 100644 --- a/LeetcodeProblemsTests/Algorithms/Subsets_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Subsets_Test.js @@ -1,5 +1,5 @@ -const assert = require('assert'); -const subsets = require('../../LeetcodeProblems/Algorithms/Subsets').subsets; +const assert = require("assert"); +const subsets = require("../../LeetcodeProblems/Algorithms/Subsets").subsets; function test() { assert.deepEqual(subsets([]), [[]]); diff --git a/LeetcodeProblemsTests/Algorithms/Sum_Of_Square_Numbers_Test.js b/LeetcodeProblemsTests/Algorithms/Sum_Of_Square_Numbers_Test.js index c4f642b..26c033a 100644 --- a/LeetcodeProblemsTests/Algorithms/Sum_Of_Square_Numbers_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Sum_Of_Square_Numbers_Test.js @@ -1,5 +1,5 @@ -const assert = require('assert'); -const judgeSquareSum = require('../../LeetcodeProblems/Algorithms/Sum_Of_Square_Numbers').judgeSquareSum; +const assert = require("assert"); +const judgeSquareSum = require("../../LeetcodeProblems/Algorithms/Sum_Of_Square_Numbers").judgeSquareSum; var test = function() { assert.strictEqual(judgeSquareSum(0), true); @@ -8,6 +8,6 @@ var test = function() { assert.strictEqual(judgeSquareSum(16), true); assert.strictEqual(judgeSquareSum(24), false); assert.strictEqual(judgeSquareSum(25), true); -} +}; module.exports.test = test; diff --git a/LeetcodeProblemsTests/Algorithms/Swap_Nodes_In_Pairs_Test.js b/LeetcodeProblemsTests/Algorithms/Swap_Nodes_In_Pairs_Test.js index 9a8e8f9..06df05e 100644 --- a/LeetcodeProblemsTests/Algorithms/Swap_Nodes_In_Pairs_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Swap_Nodes_In_Pairs_Test.js @@ -1,13 +1,13 @@ -const assert = require('assert'); -const ListNode = require('../../UtilsClasses/ListNode').ListNode; -const ListNodeTestHelper = require('../../UtilsClasses/ListNodeTestHelper'); -const swapPairs = require('../../LeetcodeProblems/Algorithms/Swap_Nodes_In_Pairs').swapPairs; +// const assert = require("assert"); +const ListNode = require("../../UtilsClasses/ListNode").ListNode; +const ListNodeTestHelper = require("../../UtilsClasses/ListNodeTestHelper"); +const swapPairs = require("../../LeetcodeProblems/Algorithms/Swap_Nodes_In_Pairs").swapPairs; var test = function () { ListNodeTestHelper.assertList(swapPairs(ListNode.linkenList([1,2,3,4])), [2,1,4,3]); ListNodeTestHelper.assertList(swapPairs(ListNode.linkenList([])), []); ListNodeTestHelper.assertList(swapPairs(ListNode.linkenList([1])), [1]); ListNodeTestHelper.assertList(swapPairs(ListNode.linkenList([1,2])), [2, 1]); -} +}; module.exports.test = test; diff --git a/LeetcodeProblemsTests/Algorithms/Symmetric_Tree_Test.js b/LeetcodeProblemsTests/Algorithms/Symmetric_Tree_Test.js index ea8b26b..928d068 100644 --- a/LeetcodeProblemsTests/Algorithms/Symmetric_Tree_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Symmetric_Tree_Test.js @@ -1,5 +1,5 @@ var test = function() { // TODO -} +}; module.exports.test = test; diff --git a/LeetcodeProblemsTests/Algorithms/Tic_Tac_Toe_Test.js b/LeetcodeProblemsTests/Algorithms/Tic_Tac_Toe_Test.js index b9656ab..d1d98ec 100644 --- a/LeetcodeProblemsTests/Algorithms/Tic_Tac_Toe_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Tic_Tac_Toe_Test.js @@ -1,14 +1,14 @@ -const TicTacToe = require('../../LeetcodeProblems/Algorithms/Tic_Tac_Toe').TicTacToe; +const TicTacToe = require("../../LeetcodeProblems/Algorithms/Tic_Tac_Toe").TicTacToe; var test = function() { - ticTacToe = new TicTacToe(); + let ticTacToe = new TicTacToe(); ticTacToe.isBoardFull(); ticTacToe.addToken(0, 1, "X"); ticTacToe.printBoard(); var iter = 0; while(iter < 8) { - const str = (iter % 2 == 0) ? "0" : "X" + const str = (iter % 2 == 0) ? "0" : "X"; ticTacToe.makeMove(str); iter++; } @@ -18,6 +18,6 @@ var test = function() { ticTacToe.addToken(0,0,"X"); ticTacToe.printBoard(); -} +}; module.exports.test = test; diff --git a/LeetcodeProblemsTests/Algorithms/Time_Needed_Rearrange_Binary_String_Test.js b/LeetcodeProblemsTests/Algorithms/Time_Needed_Rearrange_Binary_String_Test.js index c4e2f00..a51e1bf 100644 --- a/LeetcodeProblemsTests/Algorithms/Time_Needed_Rearrange_Binary_String_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Time_Needed_Rearrange_Binary_String_Test.js @@ -1,9 +1,9 @@ -const assert = require('assert'); -const secondsToRemoveOccurrences = require('../../LeetcodeProblems/Algorithms/Time_Needed_Rearrange_Binary_String').secondsToRemoveOccurrences; +const assert = require("assert"); +const secondsToRemoveOccurrences = require("../../LeetcodeProblems/Algorithms/Time_Needed_Rearrange_Binary_String").secondsToRemoveOccurrences; function test() { assert.equal(secondsToRemoveOccurrences("0110101"), 4); - assert.equal(secondsToRemoveOccurrences("11100"), 0) -}; + assert.equal(secondsToRemoveOccurrences("11100"), 0); +} -module.exports.test = test +module.exports.test = test; diff --git a/LeetcodeProblemsTests/Algorithms/Top_K_Frequent_Elements_Test.js b/LeetcodeProblemsTests/Algorithms/Top_K_Frequent_Elements_Test.js index 5900f68..5fc89a1 100644 --- a/LeetcodeProblemsTests/Algorithms/Top_K_Frequent_Elements_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Top_K_Frequent_Elements_Test.js @@ -1,10 +1,10 @@ -const assert = require('assert'); -const topKFrequent = require('../../LeetcodeProblems/Algorithms/Top_K_Frequent_Elements').topKFrequent; +const assert = require("assert"); +const topKFrequent = require("../../LeetcodeProblems/Algorithms/Top_K_Frequent_Elements").topKFrequent; var test = function () { assert.deepEqual(topKFrequent([1,1,1,2,2,3], 2).sort(), [1,2]); assert.deepEqual(topKFrequent([7,8,9,8,9,8], 2).sort(), [8,9]); assert.deepEqual(topKFrequent([1,1,1,1], 1).sort(), [1]); -} +}; module.exports.test = test; diff --git a/LeetcodeProblemsTests/Algorithms/Unique_Binary_Search_Trees_Test.js b/LeetcodeProblemsTests/Algorithms/Unique_Binary_Search_Trees_Test.js index 9e66ece..5a10c4a 100644 --- a/LeetcodeProblemsTests/Algorithms/Unique_Binary_Search_Trees_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Unique_Binary_Search_Trees_Test.js @@ -1,7 +1,7 @@ -const assert = require('assert'); -const numTrees1 = require('../../LeetcodeProblems/Algorithms/Unique_Binary_Search_Trees').numTrees1; -const numTrees2 = require('../../LeetcodeProblems/Algorithms/Unique_Binary_Search_Trees').numTrees2; -const numTrees3 = require('../../LeetcodeProblems/Algorithms/Unique_Binary_Search_Trees').numTrees3; +const assert = require("assert"); +const numTrees1 = require("../../LeetcodeProblems/Algorithms/Unique_Binary_Search_Trees").numTrees1; +const numTrees2 = require("../../LeetcodeProblems/Algorithms/Unique_Binary_Search_Trees").numTrees2; +const numTrees3 = require("../../LeetcodeProblems/Algorithms/Unique_Binary_Search_Trees").numTrees3; var test = function () { assert.strictEqual(numTrees1(1), 1); @@ -18,6 +18,6 @@ var test = function () { assert.strictEqual(numTrees3(2), 2); assert.strictEqual(numTrees3(3), 5); assert.strictEqual(numTrees3(5), 42); -} +}; module.exports.test = test; diff --git a/LeetcodeProblemsTests/Algorithms/Unique_Paths_Test.js b/LeetcodeProblemsTests/Algorithms/Unique_Paths_Test.js index 05d106a..a67592a 100644 --- a/LeetcodeProblemsTests/Algorithms/Unique_Paths_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Unique_Paths_Test.js @@ -1,7 +1,7 @@ -const assert = require('assert'); -const uniquePaths1 = require('../../LeetcodeProblems/Algorithms/Unique_Paths').uniquePaths1; -const uniquePaths2 = require('../../LeetcodeProblems/Algorithms/Unique_Paths').uniquePaths2; -const uniquePaths3 = require('../../LeetcodeProblems/Algorithms/Unique_Paths').uniquePaths3; +const assert = require("assert"); +const uniquePaths1 = require("../../LeetcodeProblems/Algorithms/Unique_Paths").uniquePaths1; +const uniquePaths2 = require("../../LeetcodeProblems/Algorithms/Unique_Paths").uniquePaths2; +const uniquePaths3 = require("../../LeetcodeProblems/Algorithms/Unique_Paths").uniquePaths3; var test = function() { assert.strictEqual(uniquePaths1(10,4), 220); @@ -12,6 +12,6 @@ var test = function() { assert.strictEqual(uniquePaths3(10,4), 220); assert.strictEqual(uniquePaths3(3,2), 3); -} +}; module.exports.test = test; diff --git a/LeetcodeProblemsTests/Algorithms/Valid_Parentheses_Test.js b/LeetcodeProblemsTests/Algorithms/Valid_Parentheses_Test.js index 8e54762..a110c7b 100644 --- a/LeetcodeProblemsTests/Algorithms/Valid_Parentheses_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Valid_Parentheses_Test.js @@ -1,5 +1,5 @@ -const assert = require('assert'); -const isValid = require('../../LeetcodeProblems/Algorithms/Valid_Parentheses').isValid; +const assert = require("assert"); +const isValid = require("../../LeetcodeProblems/Algorithms/Valid_Parentheses").isValid; var test = function () { assert.strictEqual(isValid(""), true); @@ -7,6 +7,6 @@ var test = function () { assert.strictEqual(isValid("([)]"), false); assert.strictEqual(isValid("{[()]}{[()]}"), true); assert.strictEqual(isValid("{[())()]}"), false); -} +}; module.exports.test = test; diff --git a/LeetcodeProblemsTests/Algorithms/Verify_Preorder_Serialization_of_a_Binary_Tree_Test.js b/LeetcodeProblemsTests/Algorithms/Verify_Preorder_Serialization_of_a_Binary_Tree_Test.js index 3dd59ef..ad60f96 100644 --- a/LeetcodeProblemsTests/Algorithms/Verify_Preorder_Serialization_of_a_Binary_Tree_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Verify_Preorder_Serialization_of_a_Binary_Tree_Test.js @@ -1,5 +1,5 @@ -const assert = require('assert'); -const isValidSerialization = require('../../LeetcodeProblems/Algorithms/Verify_Preorder_Serialization_of_a_Binary_Tree').isValidSerialization; +const assert = require("assert"); +const isValidSerialization = require("../../LeetcodeProblems/Algorithms/Verify_Preorder_Serialization_of_a_Binary_Tree").isValidSerialization; var test = function() { assert.strictEqual(isValidSerialization(""), true); diff --git a/LeetcodeProblemsTests/Algorithms/merge_k_sorted_lists_Test.js b/LeetcodeProblemsTests/Algorithms/merge_k_sorted_lists_Test.js index 4c563de..0db1c51 100644 --- a/LeetcodeProblemsTests/Algorithms/merge_k_sorted_lists_Test.js +++ b/LeetcodeProblemsTests/Algorithms/merge_k_sorted_lists_Test.js @@ -1,7 +1,7 @@ -const assert = require('assert'); -const ListNodeTestHelper = require('../../UtilsClasses/ListNodeTestHelper'); -var ListNode = require('../../UtilsClasses/ListNode').ListNode; -const mergeKLists = require('../../LeetcodeProblems/Algorithms/merge_k_sorted_lists').mergeKLists; +const assert = require("assert"); +const ListNodeTestHelper = require("../../UtilsClasses/ListNodeTestHelper"); +var ListNode = require("../../UtilsClasses/ListNode").ListNode; +const mergeKLists = require("../../LeetcodeProblems/Algorithms/merge_k_sorted_lists").mergeKLists; function test() { assert.deepEqual(mergeKLists([]), null);